8000 [PropertyInfo] Fixed promoted property type detection. · symfony/symfony@433ba0c · GitHub
[go: up one dir, main page]

Skip to content

Commit 433ba0c

Browse files
committed
[PropertyInfo] Fixed promoted property type detection.
1 parent 96030d0 commit 433ba0c

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,22 +237,27 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra
237237
return null;
238238
}
239239

240+
// Type can be inside promoted property comment as `@var`
241+
$rawDocNode = $reflectionProperty->getDocComment();
242+
$phpDocNode = $rawDocNode ? $this->getPhpDocNode($rawDocNode) : null;
240243
$source = self::PROPERTY;
241244

242-
if ($reflectionProperty->isPromoted()) {
245+
if ($phpDocNode && [] === $phpDocNode->getTagsByName('@var')) {
246+
$phpDocNode = null;
247+
}
248+
249+
// or in the constructor comment as `@param`.
250+
if (!$phpDocNode && $reflectionProperty->isPromoted()) {
243251
$constructor = new \ReflectionMethod($class, '__construct');
244252
$rawDocNode = $constructor->getDocComment();
253+
$phpDocNode = $rawDocNode ? $this->getPhpDocNode($rawDocNode) : null;
245254
$source = self::MUTATOR;
246-
} else {
247-
$rawDocNode = $reflectionProperty->getDocComment();
248255
}
249256

250-
if (!$rawDocNode) {
257+
if (!$phpDocNode) {
251258
return null;
252259
}
253260

254-
$phpDocNode = $this->getPhpDocNode($rawDocNode);
255-
256261
return [$phpDocNode, $source, $reflectionProperty->class];
257262
}
258263

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,8 @@ public function testExtractPhp80Type(string $class, $property, array $type = nul
474474
public static function php80TypesProvider()
475475
{
476476
return [
477+
[Php80Dummy::class, 'promotedWithDocCommentAndType', [new Type(Type::BUILTIN_TYPE_INT)]],
478+
[Php80Dummy::class, 'promotedWithDocComment', [new Type(Type::BUILTIN_TYPE_STRING)]],
477479
[Php80Dummy::class, 'promotedAndMutated', [new Type(Type::BUILTIN_TYPE_STRING)]],
478480
[Php80Dummy::class, 'promoted', null],
479481
[Php80Dummy::class, 'collection', [new Type(Type::BUILTIN_TYPE_ARRAY, collection: true, collectionValueType: new Type(Type::BUILTIN_TYPE_STRING))]],

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,23 @@ class Php80Dummy
1717

1818
/**
1919
* @param string $promotedAndMutated
20+
* @param string $promotedWithDocComment
21+
* @param string $promotedWithDocCommentAndType
2022
* @param array<string> $collection
2123
*/
22-
public function __construct(private mixed $promoted, private mixed $promotedAndMutated, private array $collection)
24+
public function __construct(
25+
private mixed $promoted,
26+
private mixed $promotedAndMutated,
27+
/**
28+
* Comment without @var.
29+
*/
30+
private mixed $promotedWithDocComment,
31+
/**
32+
* @var int
33+
*/
34+
private mixed $promotedWithDocCommentAndType,
35+
private array $collection,
36+
)
2337
{
2438
}
2539

0 commit comments

Comments
 (0)
0