8000 [PropertyInfo] Add PHP 8.0 promoted properties `@param` mutation supp… · symfony/symfony@92b0da1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 92b0da1

Browse files
raphaelvoisinnicolas-grekas
authored andcommitted
[PropertyInfo] Add PHP 8.0 promoted properties @param mutation support to PhpDocExtractor
1 parent 77ec2ad commit 92b0da1

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

src/Symfony/Component/PropertyInfo/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support for phpDocumentor and PHPStan pseudo-types
8+
* Add PHP 8.0 promoted properties `@param` mutation support to `PhpDocExtractor`
89

910
6.0
1011
---

src/Symfony/Component/PropertyInfo/Extractor/PhpDocExtractor.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,11 @@ public function getTypes(string $class, string $property, array $context = []):
128128
return null;
129129
}
130130

131-
switch ($source) {
132-
case self::PROPERTY:
133-
$tag = 'var';
134-
break;
135-
136-
case self::ACCESSOR:
137-
$tag = 'return';
138-
break;
139-
140-
case self::MUTATOR:
141-
$tag = 'param';
142-
break;
143-
}
131+
$tag = match ($source) {
132+
self::PROPERTY => 'var',
133+
self::ACCESSOR => 'return',
134+
self::MUTATOR => 'param',
135+
};
144136

145137
$parentClass = null;
146138
$types = [];
@@ -249,9 +241,19 @@ private function getDocBlock(string $class, string $property): array
249241
return $this->docBlocks[$propertyHash];
250242
}
251243

244+
try {
245+
$reflectionProperty = new \ReflectionProperty($class, $property);
246+
} catch (\ReflectionException $e) {
247+
$reflectionProperty = null;
248+
}
249+
252250
$ucFirstProperty = ucfirst($property);
253251

254252
switch (true) {
253+
case $reflectionProperty?->isPromoted() && $docBlock = $this->getDocBlockFromConstructor($class, $property):
254+
$data = [$docBlock, self::MUTATOR, null];
255+
break;
256+
255257
case $docBlock = $this->getDocBlockFromProperty($class, $property):
256258
$data = [$docBlock, self::PROPERTY, null];
257259
break;

src/Symfony/Component/PropertyInfo/Tests/Extractor/PhpDocExtractorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1717
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
1818
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
19+
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy;
1920
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait;
2021
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsingTrait;
2122
use Symfony\Component\PropertyInfo\Type;
@@ -429,6 +430,22 @@ public function pseudoTypesProvider(): array
429430
['positiveInt', [new Type(Type::BUILTIN_TYPE_INT, false, null)]],
430431
];
431432
}
433+
434+
/**
435+
* @dataProvider promotedPropertyProvider
436+
*/
437+
public function testExtractPromotedProperty(string $property, ?array $types)
438+
{
439+
$this->assertEquals($types, $this->extractor->getTypes(Php80Dummy::class, $property));
440+
}
441+
442+
public function promotedPropertyProvider(): array
443+
{
444+
return [
445+
['promoted', null],
446+
['promotedAndMutated', [new Type(Type::BUILTIN_TYPE_STRING)]],
447+
];
448+
}
432449
}
433450

434451
class EmptyDocBlock

src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php80Dummy.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ class Php80Dummy
66
{
77
public mixed $mixedProperty;
88

9+
/**
10+
* @param string $promotedAndMutated
11+
*/
12+
public function __construct(private mixed $promoted, private mixed $promotedAndMutated)
13+
{
14+
}
15+
916
public function getFoo(): array|null
1017
{
1118
}

0 commit comments

Comments
 (0)
0