Skip to content

Commit fc9863f

Browse files
mvrieljaapio
authored andcommitted
Type juggle a bit more in the factories
The conversion from PHP-Parser's node to an Expression needs to take into account whether the provided parser is an Expression parser or the usual Standard parser. If it is an ExpressionParser, we can create a real Expression out of it
1 parent 2cb3adf commit fc9863f

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

src/phpDocumentor/Reflection/Php/Argument.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function getType(): Type|null
7272
/** */
7373
public function getDefault(bool $asString = true): string|null
7474
{
75+
if ($this->default === null) {
76+
return null;
77+
}
78+
7579
if ($asString) {
7680
trigger_error(
7781
'The Default value will become of type Expression by default',

src/phpDocumentor/Reflection/Php/Constant.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public function __construct(
6767
*/
6868
public function getValue(bool $asString = true): Expression|string|null
6969
{
70+
if ($this->value === null) {
71+
return null;
72+
}
73+
7074
if ($asString) {
7175
trigger_error(
7276
'The expression value will become of type Expression by default',

src/phpDocumentor/Reflection/Php/EnumCase.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public function getEndLocation(): Location
8484
*/
8585
public function getValue(bool $asString = true): Expression|string|null
8686
{
87+
if ($this->value === null) {
88+
return null;
89+
}
90+
8791
if ($asString) {
8892
trigger_error(
8993
'The enum case value will become of type Expression by default',

src/phpDocumentor/Reflection/Php/Factory/Property.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
2121
use phpDocumentor\Reflection\Php\Expression;
2222
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
23-
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
2423
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor;
2524
use phpDocumentor\Reflection\Php\StrategyContainer;
2625
use phpDocumentor\Reflection\Php\Trait_;
@@ -77,11 +76,6 @@ protected function doCreate(
7776

7877
$iterator = new PropertyIterator($object);
7978
foreach ($iterator as $stmt) {
80-
$default = $object->default !== null ? $this->valueConverter->prettyPrintExpr($object->default) : null;
81-
if ($this->valueConverter instanceof ExpressionPrinter) {
82-
$default = new Expression($default, $this->valueConverter->getParts());
83-
}
84-
8579
$property = PropertyBuilder::create(
8680
$this->valueConverter,
8781
$this->docBlockFactory,
@@ -92,7 +86,7 @@ protected function doCreate(
9286
->visibility($stmt)
9387
->type($stmt->getType())
9488
->docblock($stmt->getDocComment())
95-
->default($default)
89+
->default($this->determineDefault($stmt))
9690
->static($stmt->isStatic())
9791
->startLocation(new Location($stmt->getLine()))
9892
->endLocation(new Location($stmt->getEndLine()))
@@ -109,8 +103,24 @@ protected function doCreate(
109103
}
110104

111105
$propertyContainer->addProperty($property);
106+
107+
}
108+
}
109+
110+
private function determineDefault(PropertyIterator $value): Expression|null
111+
{
112+
$expression = $value->getDefault() !== null
113+
? $this->valueConverter->prettyPrintExpr($value->getDefault())
114+
: null;
115+
116+
if ($this->valueConverter instanceof ExpressionPrinter) {
117+
$expression = new Expression($expression, $this->valueConverter->getParts());
118+
}
119+
120+
if (is_string($expression)) {
121+
$expression = new Expression($expression, []);
112122
}
113123

114-
return null;
124+
return $expression;
115125
}
116126
}

src/phpDocumentor/Reflection/Php/Property.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function __construct(
7676
*/
7777
public function getDefault(bool $asString = true): Expression|string|null
7878
{
79+
if ($this->default === null) {
80+
return null;
81+
}
82+
7983
if ($asString) {
8084
trigger_error(
8185
'The Default value will become of type Expression by default',

tests/unit/phpDocumentor/Reflection/Php/Factory/PropertyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function testMatches(): void
6363
#[DataProvider('visibilityProvider')]
6464
public function testCreateWithVisibility(int $input, string $expectedVisibility): void
6565
{
66-
$constantStub = $this->buildPropertyMock($input);
66+
$propertyStub = $this->buildPropertyMock($input);
6767

68-
$class = $this->performCreate($constantStub);
68+
$class = $this->performCreate($propertyStub);
6969

7070
$property = current($class->getProperties());
7171
$this->assertProperty($property, $expectedVisibility);

0 commit comments

Comments
 (0)