Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PropertyInfo] Fixed promoted property type detection for PhpStanExtractor #52684

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/Symfony/Component/PropertyInfo/Extractor/PhpStanExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,7 @@ private function getDocBlockFromConstructor(string $class, string $property): ?P
return null;
}

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

return $this->filterDocBlockParams($phpDocNode, $property);
}
Expand Down Expand Up @@ -239,24 +237,27 @@ private function getDocBlockFromProperty(string $class, string $property): ?arra
return null;
}

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

if ($reflectionProperty->isPromoted()) {
if (!$phpDocNode?->getTagsByName('@var')) {
$phpDocNode = null;
}

// or in the constructor as `@param` for promoted properties
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;
}

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

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

Expand Down Expand Up @@ -296,10 +297,17 @@ private function getDocBlockFromMethod(string $class, string $ucFirstProperty, i
return null;
}

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

return [$phpDocNode, $prefix, $reflectionMethod->class];
}

private function getPhpDocNode(string $rawDocNode): PhpDocNode
{
$tokens = new TokenIterator($this->lexer->tokenize($rawDocNode));
$phpDocNode = $this->phpDocParser->parse($tokens);
$tokens->consumeTokenType(Lexer::TOKEN_END);

return [$phpDocNode, $prefix, $reflectionMethod->class];
return $phpDocNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ public function testExtractPhp80Type(string $class, $property, array $type = nul
public static 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
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
Loading