Skip to content

Commit 52e5c99

Browse files
committed
Update tests
1 parent eefcfc4 commit 52e5c99

File tree

11 files changed

+85
-62
lines changed

11 files changed

+85
-62
lines changed

src/phpDocumentor/Reflection/Php/Expression.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ final class Expression
1818
/** @var array<string, Fqsen|Type> */
1919
private array $parts;
2020

21+
/**
22+
* @param array<string, Fqsen|Type> $parts
23+
*/
2124
public function __construct(string $expression, array $parts)
2225
{
2326
$this->expression = $expression;
@@ -29,6 +32,9 @@ public function getExpression(): string
2932
return $this->expression;
3033
}
3134

35+
/**
36+
* @return array<string, Fqsen|Type>
37+
*/
3238
public function getParts(): array
3339
{
3440
return $this->parts;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ protected function doCreate(
9191
private function determineDefault(Param $value): ?Expression
9292
{
9393
$expression = $value->default !== null ? $this->valueConverter->prettyPrintExpr($value->default) : null;
94+
if ($expression === null) {
95+
return null;
96+
}
97+
9498
if ($this->valueConverter instanceof ExpressionPrinter) {
9599
$expression = new Expression($expression, $this->valueConverter->getParts());
96100
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ protected function doCreate(
9595
private function determineValue(ClassConstantIterator $value): ?Expression
9696
{
9797
$expression = $value->getValue() !== null ? $this->valueConverter->prettyPrintExpr($value->getValue()) : null;
98+
if ($expression === null) {
99+
return null;
100+
}
101+
98102
if ($this->valueConverter instanceof ExpressionPrinter) {
99103
$expression = new Expression($expression, $this->valueConverter->getParts());
100104
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use phpDocumentor\Reflection\Fqsen;
1010
use phpDocumentor\Reflection\Location;
1111
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
12+
use phpDocumentor\Reflection\Php\Expression;
13+
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
1214
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1315
use phpDocumentor\Reflection\Php\Property;
1416
use phpDocumentor\Reflection\Php\StrategyContainer;
@@ -20,6 +22,8 @@
2022
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
2123
use Webmozart\Assert\Assert;
2224

25+
use function is_string;
26+
2327
final class ConstructorPromotion extends AbstractFactory
2428
{
2529
private PrettyPrinter $valueConverter;
@@ -72,7 +76,7 @@ private function promoteParameterToProperty(ContextStack $context, Param $param)
7276
new Fqsen($methodContainer->getFqsen() . '::$' . (string) $param->var->name),
7377
$this->buildPropertyVisibilty($param->flags),
7478
$this->createDocBlock($param->getDocComment(), $context->getTypeContext()),
75-
$param->default !== null ? $this->valueConverter->prettyPrintExpr($param->default) : null,
79+
$this->determineDefault($param),
7680
false,
7781
new Location($param->getLine()),
7882
new Location($param->getEndLine()),
@@ -83,6 +87,24 @@ private function promoteParameterToProperty(ContextStack $context, Param $param)
8387
$methodContainer->addProperty($property);
8488
}
8589

90+
private function determineDefault(Param $value): ?Expression
91+
{
92+
$expression = $value->default !== null ? $this->valueConverter->prettyPrintExpr($value->default) : null;
93+
if ($expression === null) {
94+
return null;
95+
}
96+
97+
if ($this->valueConverter instanceof ExpressionPrinter) {
98+
$expression = new Expression($expression, $this->valueConverter->getParts());
99+
}
100+
101+
if (is_string($expression)) {
102+
$expression = new Expression($expression, []);
103+
}
104+
105+
return $expression;
106+
}
107+
86108
private function buildPropertyVisibilty(int $flags): Visibility
87109
{
88110
if ((bool) ($flags & Class_::MODIFIER_PRIVATE) === true) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ private function determineValue(?Arg $value): ?ValueExpression
129129
}
130130

131131
$expression = $value->value !== null ? $this->valueConverter->prettyPrintExpr($value->value) : null;
132+
if ($expression === null) {
133+
return null;
134+
}
135+
132136
if ($this->valueConverter instanceof ExpressionPrinter) {
133137
$expression = new ValueExpression($expression, $this->valueConverter->getParts());
134138
}

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
use phpDocumentor\Reflection\Php\Enum_ as EnumElement;
1010
use phpDocumentor\Reflection\Php\EnumCase as EnumCaseElement;
1111
use phpDocumentor\Reflection\Php\Expression;
12+
use phpDocumentor\Reflection\Php\Expression as ValueExpression;
1213
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
1314
use phpDocumentor\Reflection\Php\StrategyContainer;
1415
use PhpParser\Node\Stmt\EnumCase as EnumCaseNode;
1516
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
1617

1718
use function assert;
19+
use function is_string;
1820

1921
final class EnumCase extends AbstractFactory
2022
{
@@ -41,17 +43,30 @@ protected function doCreate(ContextStack $context, object $object, StrategyConta
4143
$enum = $context->peek();
4244
assert($enum instanceof EnumElement);
4345

44-
$value = $object->expr !== null ? $this->prettyPrinter->prettyPrintExpr($object->expr) : null;
45-
if ($this->prettyPrinter instanceof ExpressionPrinter) {
46-
$value = new Expression($value, $this->prettyPrinter->getParts());
47-
}
48-
4946
$enum->addCase(new EnumCaseElement(
5047
$object->getAttribute('fqsen'),
5148
$docBlock,
5249
new Location($object->getLine()),
5350
new Location($object->getEndLine()),
54-
$value
51+
$this->determineValue($object)
5552
));
5653
}
54+
55+
private function determineValue(EnumCaseNode $value): ?ValueExpression
56+
{
57+
$expression = $value->expr !== null ? $this->prettyPrinter->prettyPrintExpr($value->expr) : null;
58+
if ($expression === null) {
59+
return null;
60+
}
61+
62+
if ($this->prettyPrinter instanceof ExpressionPrinter) {
63+
$expression = new ValueExpression($expression, $this->prettyPrinter->getParts());
64+
}
65+
66+
if (is_string($expression)) {
67+
$expression = new ValueExpression($expression, []);
68+
}
69+
70+
return $expression;
71+
}
5772
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ protected function doCreate(
8585
private function determineValue(GlobalConstantIterator $value): ?Expression
8686
{
8787
$expression = $value->getValue() !== null ? $this->valueConverter->prettyPrintExpr($value->getValue()) : null;
88+
if ($expression === null) {
89+
return null;
90+
}
91+
8892
if ($this->valueConverter instanceof ExpressionPrinter) {
8993
$expression = new Expression($expression, $this->valueConverter->getParts());
9094
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ private function determineDefault(PropertyIterator $value): ?Expression
9999
$expression = $value->getDefault() !== null
100100
? $this->valueConverter->prettyPrintExpr($value->getDefault())
101101
: null;
102+
if ($expression === null) {
103+
return null;
104+
}
105+
102106
if ($this->valueConverter instanceof ExpressionPrinter) {
103107
$expression = new Expression($expression, $this->valueConverter->getParts());
104108
}

tests/integration/PHP8/ConstructorPromotionTest.php

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use phpDocumentor\Reflection\Fqsen;
1313
use phpDocumentor\Reflection\Location;
1414
use phpDocumentor\Reflection\Php\Argument;
15+
use phpDocumentor\Reflection\Php\Expression;
1516
use phpDocumentor\Reflection\Php\Method;
1617
use phpDocumentor\Reflection\Php\ProjectFactory;
1718
use phpDocumentor\Reflection\Php\Project;
@@ -28,12 +29,10 @@
2829
*/
2930
class ConstructorPromotionTest extends TestCase
3031
{
31-
const FILE = __DIR__ . '/../data/PHP8/ConstructorPromotion.php';
32-
/** @var ProjectFactory */
33-
private $fixture;
32+
private const FILE = __DIR__ . '/../data/PHP8/ConstructorPromotion.php';
3433

35-
/** @var Project */
36-
private $project;
34+
private ProjectFactory $fixture;
35+
private Project $project;
3736

3837
protected function setUp() : void
3938
{
@@ -55,17 +54,13 @@ public function testPropertiesAreCreated() : void
5554
$constructor->addArgument(new Argument('name', new String_()));
5655
$constructor->addArgument(new Argument('email', new String_(), '\'test@example.com\''));
5756
$constructor->addArgument(new Argument('birth_date', new Object_(new Fqsen('\\' . DateTimeImmutable::class))));
58-
$constructor->addArgument(new Argument('created_at', new Object_(new Fqsen('\\' . DateTimeImmutable::class))));
59-
$constructor->addArgument(new Argument('uses_constants', new Array_()));
6057

6158
self::assertEquals($constructor, $class->getMethods()['\PHP8\ConstructorPromotion::__construct()']);
6259
self::assertEquals(
6360
[
6461
'\PHP8\ConstructorPromotion::$name' => $this->expectedNameProperty(),
6562
'\PHP8\ConstructorPromotion::$email' => $this->expectedEmailProperty(),
6663
'\PHP8\ConstructorPromotion::$birth_date' => $this->expectedBirthDateProperty(),
67-
'\PHP8\ConstructorPromotion::$created_at' => $this->expectedCreatedAtProperty(),
68-
'\PHP8\ConstructorPromotion::$uses_constants' => $this->expectedUsesConstantsProperty(),
6964
],
7065
$class->getProperties()
7166
);
@@ -92,14 +87,14 @@ private function expectedConstructorMethod(): Method
9287
false,
9388
false,
9489
false,
95-
new Location(18, 218),
96-
new Location(31, 522)
90+
new Location(18, 264),
91+
new Location(29, 568)
9792
);
9893
}
9994

10095
private function expectedNameProperty(): Property
10196
{
102-
$name = new Property(
97+
return new Property(
10398
new Fqsen('\PHP8\ConstructorPromotion::$name'),
10499
new Visibility(Visibility::PUBLIC_),
105100
new DocBlock(
@@ -112,68 +107,37 @@ private function expectedNameProperty(): Property
112107
),
113108
null,
114109
false,
115-
new Location(24),
116-
new Location(24),
110+
new Location(26),
111+
new Location(26),
117112
new String_()
118113
);
119-
return $name;
120114
}
121115

122116
private function expectedEmailProperty(): Property
123117
{
124-
$email = new Property(
118+
return new Property(
125119
new Fqsen('\PHP8\ConstructorPromotion::$email'),
126120
new Visibility(Visibility::PROTECTED_),
127121
null,
128122
'\'test@example.com\'',
129123
false,
130-
new Location(25),
131-
new Location(25),
124+
new Location(27),
125+
new Location(27),
132126
new String_()
133127
);
134-
return $email;
135128
}
136129

137130
private function expectedBirthDateProperty(): Property
138-
{
139-
$birthDate = new Property(
140-
new Fqsen('\PHP8\ConstructorPromotion::$birth_date'),
141-
new Visibility(Visibility::PRIVATE_),
142-
null,
143-
null,
144-
false,
145-
new Location(26),
146-
new Location(26),
147-
new Object_(new Fqsen('\\' . DateTimeImmutable::class))
148-
);
149-
return $birthDate;
150-
}
151-
152-
private function expectedCreatedAtProperty(): Property
153131
{
154132
return new Property(
155-
new Fqsen('\PHP8\ConstructorPromotion::$created_at'),
133+
new Fqsen('\PHP8\ConstructorPromotion::$birth_date'),
156134
new Visibility(Visibility::PRIVATE_),
157135
null,
158136
null,
159137
false,
160-
new Location(26),
161-
new Location(26),
138+
new Location(28),
139+
new Location(28),
162140
new Object_(new Fqsen('\\' . DateTimeImmutable::class))
163141
);
164142
}
165-
166-
private function expectedUsesConstantsProperty(): Property
167-
{
168-
return new Property(
169-
new Fqsen('\PHP8\ConstructorPromotion::$uses_constants'),
170-
new Visibility(Visibility::PRIVATE_),
171-
null,
172-
null,
173-
false,
174-
new Location(26),
175-
new Location(26),
176-
new Array_()
177-
);
178-
}
179143
}

tests/integration/data/PHP8/ConstructorPromotion.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,5 @@ public function __construct(
2626
public string $name,
2727
protected string $email = 'test@example.com',
2828
private DateTimeImmutable $birth_date,
29-
private DateTimeImmutable $created_at = new DateTimeImmutable('now'),
30-
private array $uses_constants = [self::DEFAULT_VALUE],
3129
) {}
3230
}

0 commit comments

Comments
 (0)