Skip to content

Commit ac6efc7

Browse files
committed
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 ae996a3 commit ac6efc7

File tree

9 files changed

+57
-21
lines changed

9 files changed

+57
-21
lines changed

src/phpDocumentor/Reflection/Php/Argument.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ public function getType(): ?Type
8686
*/
8787
public function getDefault(bool $asString = true)
8888
{
89+
if ($this->default === null) {
90+
return null;
91+
}
92+
8993
if ($asString) {
9094
trigger_error(
9195
'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
@@ -81,6 +81,10 @@ public function __construct(
8181
*/
8282
public function getValue(bool $asString = true)
8383
{
84+
if ($this->value === null) {
85+
return null;
86+
}
87+
8488
if ($asString) {
8589
trigger_error(
8690
'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
@@ -90,6 +90,10 @@ public function getEndLocation(): Location
9090
*/
9191
public function getValue(bool $asString = true)
9292
{
93+
if ($this->value === null) {
94+
return null;
95+
}
96+
9397
if ($asString) {
9498
trigger_error(
9599
'The enum case value will become of type Expression by default',

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,27 @@ protected function doCreate(
7575
]
7676
);
7777

78-
$default = $object->default !== null ? $this->valueConverter->prettyPrintExpr($object->default) : null;
79-
if ($this->valueConverter instanceof ExpressionPrinter) {
80-
$default = new Expression($default, $this->valueConverter->getParts());
81-
}
82-
8378
$method->addArgument(
8479
new ArgumentDescriptor(
8580
(string) $object->var->name,
8681
(new Type())->fromPhpParser($object->type),
87-
$default,
82+
$this->determineDefault($object),
8883
$object->byRef,
8984
$object->variadic
9085
)
9186
);
9287
}
88+
89+
private function determineDefault(Param $value): ?Expression
90+
{
91+
$expression = $value->default !== null ? $this->valueConverter->prettyPrintExpr($value->default) : null;
92+
if ($this->valueConverter instanceof ExpressionPrinter) {
93+
$expression = new Expression($expression, $this->valueConverter->getParts());
94+
}
95+
if (is_string($expression)) {
96+
$expression = new Expression($expression, []);
97+
}
98+
99+
return $expression;
100+
}
93101
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,12 @@ protected function doCreate(
7676

7777
$iterator = new PropertyIterator($object);
7878
foreach ($iterator as $stmt) {
79-
$default = $object->default !== null ? $this->valueConverter->prettyPrintExpr($object->default) : null;
80-
if ($this->valueConverter instanceof ExpressionPrinter) {
81-
$default = new Expression($default, $this->valueConverter->getParts());
82-
}
83-
8479
$propertyContainer->addProperty(
8580
new PropertyDescriptor(
8681
$stmt->getFqsen(),
8782
$this->buildVisibility($stmt),
8883
$this->createDocBlock($stmt->getDocComment(), $context->getTypeContext()),
89-
$default,
84+
$this->determineDefault($stmt),
9085
$stmt->isStatic(),
9186
new Location($stmt->getLine()),
9287
new Location($stmt->getEndLine()),
@@ -97,6 +92,21 @@ protected function doCreate(
9792
}
9893
}
9994

95+
private function determineDefault(PropertyIterator $value): ?Expression
96+
{
97+
$expression = $value->getDefault() !== null
98+
? $this->valueConverter->prettyPrintExpr($value->getDefault())
99+
: null;
100+
if ($this->valueConverter instanceof ExpressionPrinter) {
101+
$expression = new Expression($expression, $this->valueConverter->getParts());
102+
}
103+
if (is_string($expression)) {
104+
$expression = new Expression($expression, []);
105+
}
106+
107+
return $expression;
108+
}
109+
100110
/**
101111
* Converts the visibility of the property to a valid Visibility object.
102112
*/

src/phpDocumentor/Reflection/Php/Property.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public function __construct(
9090
*/
9191
public function getDefault(bool $asString = true)
9292
{
93+
if ($this->default === null) {
94+
return null;
95+
}
96+
9397
if ($asString) {
9498
trigger_error(
9599
'The Default value will become of type Expression by default',

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Mockery as m;
1717
use phpDocumentor\Reflection\Fqsen;
1818
use phpDocumentor\Reflection\Php\Argument as ArgumentDescriptor;
19+
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
1920
use phpDocumentor\Reflection\Php\Method as MethodElement;
2021
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
2122
use PhpParser\Node\Expr\Variable;
@@ -40,7 +41,7 @@ class ArgumentTest extends TestCase
4041
{
4142
protected function setUp(): void
4243
{
43-
$this->fixture = new Argument(new PrettyPrinter());
44+
$this->fixture = new Argument(new ExpressionPrinter());
4445
}
4546

4647
/**

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@
3333
use function next;
3434

3535
/**
36-
* @uses \phpDocumentor\Reflection\Php\Factory\PropertyIterator
37-
* @uses \phpDocumentor\Reflection\Php\Property
36+
* @uses PropertyIterator
37+
* @uses PropertyDescriptor
3838
* @uses \phpDocumentor\Reflection\Php\Visibility
39-
* @uses \phpDocumentor\Reflection\Php\ProjectFactoryStrategies
40-
* @uses \phpDocumentor\Reflection\Php\Factory\Type
39+
* @uses ProjectFactoryStrategies
40+
* @uses Type
4141
*
4242
* @covers \phpDocumentor\Reflection\Php\Factory\Property
4343
* @covers \phpDocumentor\Reflection\Php\Factory\AbstractFactory
44+
*
45+
* @extends TestCase<Property>
4446
*/
4547
final class PropertyTest extends TestCase
4648
{
@@ -63,9 +65,9 @@ public function testMatches(): void
6365
/** @dataProvider visibilityProvider */
6466
public function testCreateWithVisibility(int $input, string $expectedVisibility): void
6567
{
66-
$constantStub = $this->buildPropertyMock($input);
68+
$propertyStub = $this->buildPropertyMock($input);
6769

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

7072
$property = current($class->getProperties());
7173
$this->assertProperty($property, $expectedVisibility);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
*/
2828
abstract class TestCase extends MockeryTestCase
2929
{
30-
/** @var ProjectFactoryStrategy */
31-
protected $fixture;
30+
protected ProjectFactoryStrategy $fixture;
3231

3332
public static function createContext(?Context $typeContext = null): ContextStack
3433
{

0 commit comments

Comments
 (0)