Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f68081a

Browse files
committed
[PropertyInfo] Add support for promoted properties in PhpStanExtractor
1 parent 2a38810 commit f68081a

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/Symfony/Component/PropertyInfo/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 11 additions & 5 deletions
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,21 @@ public function testDummyNamespaceWithProperty()
434434
$this->assertEquals('A\Property', $phpStanTypes[0]->getClassName());
435435
$this->assertEquals($phpDocTypes[0]->getClassName(), $phpStanTypes[0]->getClassName());
436436
}
437+
438+
/**
439+
* @dataProvider php80TypesProvider
440+
*/
441+
public function testExtractPhp80Type($property, array $type = null)
442+
{
443+
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Component\PropertyInfo\Tests\Fixtures\Php80Dummy', $property, []));
444+
}
445+
446+
public function php80TypesProvider()
447+
{
448+
return [
449+
['promotedAndMutated', [new Type(Type::BUILTIN_TYPE_STRING)]],
450+
];
451+
}
437452
}
438453

439454
class PhpStanOmittedParamTagTypeDocBlock

0 commit comments

Comments
 (0)