Skip to content

Commit

Permalink
[PropertyInfo] Fixed promoted property type detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
LastDragon-ru committed Nov 22, 2023
1 parent 169cfa6 commit f7fd692
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/Symfony/Component/PropertyInfo/Extractor/PhpStanExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,27 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra
return null;
}

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

if ($reflectionProperty->isPromoted()) {
if ($phpDocNode && [] === $phpDocNode->getTagsByName('@var')) {
$phpDocNode = null;
}

// or in the constructor comment as `@param`.
if (!$phpDocNode && $reflectionProperty->isPromoted()) {
$constructor = new \ReflectionMethod($class, '__construct');
$rawDocNode = $constructor->getDocComment();
$phpDocNode = $rawDocNode ? $this->getPhpDocNode($rawDocNode) : null;
$source = self::MUTATOR;
} else {
$rawDocNode = $reflectionProperty->getDocComment();
}

if (!$rawDocNode) {
if (!$phpDocNode) {
return null;
}

$phpDocNode = $this->getPhpDocNode($rawDocNode);

return [$phpDocNode, $source, $reflectionProperty->class];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ public function testExtractPhp80Type(string $class, $property, array $type = nul
public function php80TypesProvider()
{
return [
[Php80Dummy::class, 'promotedWithDocCommentAndType', [new Type(Type::BUILTIN_TYPE_INT)]],
[Php80Dummy::class, 'promotedWithDocComment', [new Type(Type::BUILTIN_TYPE_STRING)]],
[Php80Dummy::class, 'promotedAndMutated', [new Type(Type::BUILTIN_TYPE_STRING)]],
[Php80Dummy::class, 'promoted', null],
[Php80Dummy::class, 'collection', [new Type(Type::BUILTIN_TYPE_ARRAY, collection: true, collectionValueType: new Type(Type::BUILTIN_TYPE_STRING))]],
Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/PropertyInfo/Tests/Fixtures/Php80Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,23 @@ class Php80Dummy

/**
* @param string $promotedAndMutated
* @param string $promotedWithDocComment
* @param string $promotedWithDocCommentAndType
* @param array<string> $collection
*/
public function __construct(private mixed $promoted, private mixed $promotedAndMutated, private array $collection)
public function __construct(
private mixed $promoted,
private mixed $promotedAndMutated,
/**
* Comment without @var.
*/
private mixed $promotedWithDocComment,
/**
* @var int
*/
private mixed $promotedWithDocCommentAndType,
private array $collection,
)
{
}

Expand Down

0 comments on commit f7fd692

Please sign in to comment.