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

Skip to content

Commit f7fd692

Browse files
committed
[PropertyInfo] Fixed promoted property type detection.
1 parent 169cfa6 commit f7fd692

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
@@ -227,22 +227,27 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra
227227
return null;
228228
}
229229

230+
// Type can be inside promoted property comment as `@var`
231+
$rawDocNode = $reflectionProperty->getDocComment();
232+
$phpDocNode = $rawDocNode ? $this->getPhpDocNode($rawDocNode) : null;
230233
$source = self::PROPERTY;
231234

232-
if ($reflectionProperty->isPromoted()) {
235+
if ($phpDocNode && [] === $phpDocNode->getTagsByName('@var')) {
236+
$phpDocNode = null;
237+
}
238+
239+
// or in the constructor comment as `@param`.
240+
if (!$phpDocNode && $reflectionProperty->isPromoted()) {
233241
$constructor = new \ReflectionMethod($class, '__construct');
234242
$rawDocNode = $constructor->getDocComment();
243+
$phpDocNode = $rawDocNode ? $this->getPhpDocNode($rawDocNode) : null;
235244
$source = self::MUTATOR;
236-
} else {
237-
$rawDocNode = $reflectionProperty->getDocComment();
238245
}
239246

240-
if (!$rawDocNode) {
247+
if (!$phpDocNode) {
241248
return null;
242249
}
243250

244-
$phpDocNode = $this->getPhpDocNode($rawDocNode);
245-
246251
return [$phpDocNode, $source, $reflectionProperty->class];
247252
}
248253

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ public function testExtractPhp80Type(string $class, $property, array $type = nul
465465
public function php80TypesProvider()
466466
{
467467
return [
468+
[Php80Dummy::class, 'promotedWithDocCommentAndType', [new Type(Type::BUILTIN_TYPE_INT)]],
469+
[Php80Dummy::class, 'promotedWithDocComment', [new Type(Type::BUILTIN_TYPE_STRING)]],
468470
[Php80Dummy::class, 'promotedAndMutated', [new Type(Type::BUILTIN_TYPE_STRING)]],
469471
[Php80Dummy::class, 'promoted', null],
470472
[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)