Skip to content

Commit 8acf157

Browse files
simPodnicolas-grekas
authored andcommitted
[PropertyInfo] Add support for promoted properties in PhpStanExtractor
1 parent 2a38810 commit 8acf157

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/Symfony/Component/PropertyInfo/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add support for phpDocumentor and PHPStan pseudo-types
88
* Add PHP 8.0 promoted properties `@param` mutation support to `PhpDocExtractor`
9+
* Add PHP 8.0 promoted properties `@param` mutation support to `PhpStanExtractor`
910

1011
6.0
1112
---

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ private function getDocBlock(string $class, string $property): array
196196

197197
$ucFirstProperty = ucfirst($property);
198198

199-
if ([$docBlock, $declaringClass] = $this->getDocBlockFromProperty($class, $property)) {
200-
$data = [$docBlock, self::PROPERTY, null, $declaringClass];
199+
if ([$docBlock, $source, $declaringClass] = $this->getDocBlockFromProperty($class, $property)) {
200+
$data = [$docBlock, $source, null, $declaringClass];
201201
} elseif ([$docBlock, $_, $declaringClass] = $this->getDocBlockFromMethod($class, $ucFirstProperty, self::ACCESSOR)) {
202202
$data = [$docBlock, self::ACCESSOR, null, $declaringClass];
203203
} elseif ([$docBlock, $prefix, $declaringClass] = $this->getDocBlockFromMethod($class, $ucFirstProperty, self::MUTATOR)) {
@@ -210,7 +210,7 @@ private function getDocBlock(string $class, string $property): array
210210
}
211211

212212
/**
213-
* @return array{PhpDocNode, string}|null
213+
* @return array{PhpDocNode, int, string}|null
214214
*/
215215
private function getDocBlockFromProperty(string $class, string $property): ?array
216216
{
@@ -221,15 +221,21 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra
221221
return null;
222222
}
223223

224-
if (null === $rawDocNode = $reflectionProperty->getDocComment() ?: null) {
224+
$source = self::PROPERTY;
225+
226+
if ($reflectionProperty->isPromoted()) {
227+
$constructor = new \ReflectionMethod($class, '__construct');
228+
$rawDocNode = $constructor->getDocComment();
229+
$source = self::MUTATOR;
230+
} elseif (null === $rawDocNode = $reflectionProperty->getDocComment() ?: null) {
225231
return null;
226232
}
227233

228234
$tokens = new TokenIterator($this->lexer->tokenize($rawDocNode));
229235
$phpDocNode = $this->phpDocParser->parse($tokens);
230236
$tokens->consumeTokenType(Lexer::TOKEN_END);
231237

232-
return [$phpDocNode, $reflectionProperty->class];
238+
return [$phpDocNode, $source, $reflectionProperty->class];
233239
}
234240

235241
/**

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

+16
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\PropertyInfo\Tests\Fixtures\DefaultValue;
1818
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
1919
use Symfony\Component\PropertyInfo\Tests\Fixtures\ParentDummy;
20+
use Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy;
2021
use Symfony\Component\PropertyInfo\Tests\Fixtures\RootDummy\RootDummyItem;
2122
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsedInTrait;
2223
use Symfony\Component\PropertyInfo\Tests\Fixtures\TraitUsage\DummyUsingTrait;
@@ -434,6 +435,21 @@ public function testDummyNamespaceWithProperty()
434435
$this->assertEquals('A\Property', $phpStanTypes[0]->getClassName());
435436
$this->assertEquals($phpDocTypes[0]->getClassName(), $phpStanTypes[0]->getClassName());
436437
}
438+
439+
/**
440+
* @dataProvider php80TypesProvider
441+
*/
442+
public function testExtractPhp80Type($property, array $type = null)
443+
{
444+
$this->assertEquals($type, $this->extractor->getTypes(Php80Dummy::class, $property, []));
445+
}
446+
447+
public function php80TypesProvider()
448+
{
449+
return [
450+
['promotedAndMutated', [new Type(Type::BUILTIN_TYPE_STRING)]],
451+
];
452+
}
437453
}
438454

439455
class PhpStanOmittedParamTagTypeDocBlock

0 commit comments

Comments
 (0)