Skip to content

Commit c9567bd

Browse files
committed
support php 74 props
Signed-off-by: RJ Garcia <ragboyjr@icloud.com>
1 parent 72112e6 commit c9567bd

3 files changed

Lines changed: 85 additions & 1 deletion

File tree

src/Internal/Props/ClassProp.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\BuilderFactory;
1010
use PhpParser\Node\Expr;
1111
use PhpParser\Node\Stmt\Property;
12+
use PhpParser\PrettyPrinter;
1213

1314
/** Internal representation of a class property along with its type */
1415
final class ClassProp
@@ -54,7 +55,7 @@ public function toParam(BuilderFactory $builderFactory): Param {
5455

5556
private static function getVarDefinitionFromProperty(Property $prop): ?string {
5657
if (!$prop->getDocComment()) {
57-
return null;
58+
return $prop->type ? (new PrettyPrinter\Standard)->prettyPrint([$prop->type]) : null;
5859
}
5960

6061
$docBlock = DocBlock::stripComments($prop->getDocComment()->getText());
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
final class Test74Props
4+
{
5+
use Test74PropsStruct;
6+
7+
private int $id;
8+
private ?string $name = null;
9+
}
10+
11+
-- EXPECTED --
12+
13+
<?php
14+
15+
final class Test74Props
16+
{
17+
use Test74PropsStruct;
18+
19+
private int $id;
20+
private ?string $name = null;
21+
}
22+
23+
trait Test74PropsStruct
24+
{
25+
public function __construct(int $id, ?string $name = null)
26+
{
27+
$this->id = $id;
28+
$this->name = $name;
29+
}
30+
public static function fromValidatedArray(array $data) : self
31+
{
32+
return new self($data['id'], array_key_exists('name', $data) ? $data['name'] : null);
33+
}
34+
public function toArray() : array
35+
{
36+
return ['id' => $this->id, 'name' => $this->name];
37+
}
38+
public function id() : int
39+
{
40+
return $this->id;
41+
}
42+
public function name() : ?string
43+
{
44+
return $this->name;
45+
}
46+
public function withId(int $id) : self
47+
{
48+
$self = clone $this;
49+
$self->id = $id;
50+
return $self;
51+
}
52+
public function withName(?string $name = null) : self
53+
{
54+
$self = clone $this;
55+
$self->name = $name;
56+
return $self;
57+
}
58+
}

test/Unit/ClassPropTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,29 @@ public function converts_to_param_with_default() {
3333
$res
3434
);
3535
}
36+
37+
/** @test */
38+
public function supports_php74_typed_properties() {
39+
$prop = ClassProp::fromProperty(
40+
(new BuilderFactory())
41+
->property('acme')
42+
->setType('string')
43+
->getNode()
44+
);
45+
46+
$this->assertEquals('string', $prop->type()->toString());
47+
}
48+
49+
/** @test */
50+
public function supports_php74_typed_properties_with_doc_comments() {
51+
$prop = ClassProp::fromProperty(
52+
(new BuilderFactory())
53+
->property('acme')
54+
->setType('array')
55+
->setDocComment('/** @var int[] */')
56+
->getNode()
57+
);
58+
59+
$this->assertEquals('int[]', $prop->type()->toString());
60+
}
3661
}

0 commit comments

Comments
 (0)