Skip to content

Commit 3f8d56d

Browse files
mvrieljaapio
authored andcommitted
Update tests
1 parent 435f6a3 commit 3f8d56d

File tree

9 files changed

+81
-64
lines changed

9 files changed

+81
-64
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/ClassConstant.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ protected function doCreate(
112112
private function determineValue(ClassConstantIterator $value): null|Expression
113113
{
114114
$expression = $value->getValue() !== null ? $this->valueConverter->prettyPrintExpr($value->getValue()) : null;
115+
if ($expression === null) {
116+
return null;
117+
}
118+
115119
if ($this->valueConverter instanceof ExpressionPrinter) {
116120
$expression = new Expression($expression, $this->valueConverter->getParts());
117121
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use phpDocumentor\Reflection\Location;
1212
use phpDocumentor\Reflection\Php\Class_ as ClassElement;
1313
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
14+
use phpDocumentor\Reflection\Php\Expression;
15+
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
1416
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1517
use phpDocumentor\Reflection\Php\StrategyContainer;
1618
use PhpParser\Modifiers;
@@ -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
/** @param iterable<Reducer> $reducers */
@@ -76,7 +80,7 @@ private function promoteParameterToProperty(ContextStack $context, StrategyConta
7680
->visibility($param)
7781
->type($param->type)
7882
->docblock($param->getDocComment())
79-
->default($param->default)
83+
->default($this->determineDefault($param))
8084
->readOnly($this->readOnly($param->flags))
8185
->static(false)
8286
->startLocation(new Location($param->getLine(), $param->getStartFilePos()))
@@ -95,6 +99,24 @@ private function promoteParameterToProperty(ContextStack $context, StrategyConta
9599
$methodContainer->addProperty($property);
96100
}
97101

102+
private function determineDefault(Param $value): Expression|null
103+
{
104+
$expression = $value->default !== null ? $this->valueConverter->prettyPrintExpr($value->default) : null;
105+
if ($expression === null) {
106+
return null;
107+
}
108+
109+
if ($this->valueConverter instanceof ExpressionPrinter) {
110+
$expression = new Expression($expression, $this->valueConverter->getParts());
111+
}
112+
113+
if (is_string($expression)) {
114+
$expression = new Expression($expression, []);
115+
}
116+
117+
return $expression;
118+
}
119+
98120
private function readOnly(int $flags): bool
99121
{
100122
return (bool) ($flags & Modifiers::READONLY) === true;

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

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

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

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
use phpDocumentor\Reflection\Php\EnumCase as EnumCaseElement;
1212
use phpDocumentor\Reflection\Php\Factory\Reducer\Reducer;
1313
use phpDocumentor\Reflection\Php\Expression;
14+
use phpDocumentor\Reflection\Php\Expression as ValueExpression;
1415
use phpDocumentor\Reflection\Php\Expression\ExpressionPrinter;
1516
use phpDocumentor\Reflection\Php\StrategyContainer;
1617
use PhpParser\Node\Stmt\EnumCase as EnumCaseNode;
1718
use PhpParser\PrettyPrinter\Standard as PrettyPrinter;
1819

1920
use function assert;
21+
use function is_string;
2022

2123
final class EnumCase extends AbstractFactory
2224
{
@@ -43,21 +45,30 @@ protected function doCreate(ContextStack $context, object $object, StrategyConta
4345
$enum = $context->peek();
4446
assert($enum instanceof EnumElement);
4547

46-
$value = $object->expr !== null ? $this->prettyPrinter->prettyPrintExpr($object->expr) : null;
47-
if ($this->prettyPrinter instanceof ExpressionPrinter) {
48-
$value = new Expression($value, $this->prettyPrinter->getParts());
49-
}
50-
51-
$case = new EnumCaseElement(
48+
$enum->addCase(new EnumCaseElement(
5249
$object->getAttribute('fqsen'),
5350
$docBlock,
5451
new Location($object->getLine()),
5552
new Location($object->getEndLine()),
56-
$value,
57-
);
53+
$this->determineValue($object),
54+
));
55+
}
5856

59-
$enum->addCase($case);
57+
private function determineValue(EnumCaseNode $value): ValueExpression|null
58+
{
59+
$expression = $value->expr !== null ? $this->prettyPrinter->prettyPrintExpr($value->expr) : null;
60+
if ($expression === null) {
61+
return null;
62+
}
63+
64+
if ($this->prettyPrinter instanceof ExpressionPrinter) {
65+
$expression = new ValueExpression($expression, $this->prettyPrinter->getParts());
66+
}
67+
68+
if (is_string($expression)) {
69+
$expression = new ValueExpression($expression, []);
70+
}
6071

61-
return $case;
72+
return $expression;
6273
}
6374
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ protected function doCreate(
8787
private function determineValue(GlobalConstantIterator $value): ?Expression
8888
{
8989
$expression = $value->getValue() !== null ? $this->valueConverter->prettyPrintExpr($value->getValue()) : null;
90+
if ($expression === null) {
91+
return null;
92+
}
93+
9094
if ($this->valueConverter instanceof ExpressionPrinter) {
9195
$expression = new Expression($expression, $this->valueConverter->getParts());
9296
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ private function determineDefault(PropertyIterator $value): Expression|null
115115
? $this->valueConverter->prettyPrintExpr($value->getDefault())
116116
: null;
117117

118+
if ($expression === null) {
119+
return null;
120+
}
121+
118122
if ($this->valueConverter instanceof ExpressionPrinter) {
119123
$expression = new Expression($expression, $this->valueConverter->getParts());
120124
}

tests/integration/PHP8/ConstructorPromotionTest.php

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

36-
/** @var Project */
37-
private $project;
35+
private ProjectFactory $fixture;
36+
private Project $project;
3837

3938
protected function setUp() : void
4039
{
@@ -56,17 +55,13 @@ public function testPropertiesAreCreated() : void
5655
$constructor->addArgument(new Argument('name', new String_(), "'default name'"));
5756
$constructor->addArgument(new Argument('email', new Object_(new Fqsen('\\PHP8\\Email'))));
5857
$constructor->addArgument(new Argument('birth_date', new Object_(new Fqsen('\\' . DateTimeImmutable::class))));
59-
$constructor->addArgument(new Argument('created_at', new Object_(new Fqsen('\\' . DateTimeImmutable::class))));
60-
$constructor->addArgument(new Argument('uses_constants', new Array_()));
6158

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

10196
private function expectedNameProperty(): Property
10297
{
103-
$name = new Property(
98+
return new Property(
10499
new Fqsen('\PHP8\ConstructorPromotion::$name'),
105100
new Visibility(Visibility::PUBLIC_),
106101
new DocBlock(
@@ -113,68 +108,37 @@ private function expectedNameProperty(): Property
113108
),
114109
"'default name'",
115110
false,
116-
new Location(24, 393),
117-
new Location(24, 428),
111+
new Location(26, 393),
112+
new Location(26, 428),
118113
new String_()
119114
);
120-
return $name;
121115
}
122116

123117
private function expectedEmailProperty(): Property
124118
{
125-
$email = new Property(
119+
return new Property(
126120
new Fqsen('\PHP8\ConstructorPromotion::$email'),
127121
new Visibility(Visibility::PROTECTED_),
128122
null,
129123
null,
130124
false,
131-
new Location(25, 439),
132-
new Location(25, 460),
125+
new Location(27, 439),
126+
new Location(27, 460),
133127
new Object_(new Fqsen('\\PHP8\\Email'))
134128
);
135-
return $email;
136129
}
137130

138131
private function expectedBirthDateProperty(): Property
139-
{
140-
$birthDate = new Property(
141-
new Fqsen('\PHP8\ConstructorPromotion::$birth_date'),
142-
new Visibility(Visibility::PRIVATE_),
143-
null,
144-
null,
145-
false,
146-
new Location(26),
147-
new Location(26),
148-
new Object_(new Fqsen('\\' . DateTimeImmutable::class))
149-
);
150-
return $birthDate;
151-
}
152-
153-
private function expectedCreatedAtProperty(): Property
154132
{
155133
return new Property(
156-
new Fqsen('\PHP8\ConstructorPromotion::$created_at'),
134+
new Fqsen('\PHP8\ConstructorPromotion::$birth_date'),
157135
new Visibility(Visibility::PRIVATE_),
158136
null,
159137
null,
160138
false,
161-
new Location(26),
162-
new Location(26),
139+
new Location(28),
140+
new Location(28),
163141
new Object_(new Fqsen('\\' . DateTimeImmutable::class))
164142
);
165143
}
166-
167-
private function expectedUsesConstantsProperty(): Property
168-
{
169-
return new Property(
170-
new Fqsen('\PHP8\ConstructorPromotion::$uses_constants'),
171-
new Visibility(Visibility::PRIVATE_),
172-
null,
173-
null,
174-
false,
175-
new Location(26),
176-
new Location(26),
177-
new Array_()
178-
);
179-
}
180144
}

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 = 'default name',
2727
protected Email $email,
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)