Skip to content

[TypeInfo] Fix @var tag reading for promoted properties #59963

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

Merged
merged 1 commit into from
Mar 13, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ final class DummyWithPhpDoc

/**
* @param bool $promoted
* @param bool $promotedVarAndParam
*/
public function __construct(
public mixed $promoted,
/**
* @var string
*/
public mixed $promotedVar,
/**
* @var string
*/
public mixed $promotedVarAndParam,
) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function testReadPhpDoc()

$this->assertEquals(Type::array(Type::object(Dummy::class)), $resolver->resolve($reflection->getProperty('arrayOfDummies')));
$this->assertEquals(Type::bool(), $resolver->resolve($reflection->getProperty('promoted')));
$this->assertEquals(Type::string(), $resolver->resolve($reflection->getProperty('promotedVar')));
$this->assertEquals(Type::string(), $resolver->resolve($reflection->getProperty('promotedVarAndParam')));
$this->assertEquals(Type::object(Dummy::class), $resolver->resolve($reflection->getMethod('getNextDummy')));
$this->assertEquals(Type::object(Dummy::class), $resolver->resolve($reflection->getMethod('getNextDummy')->getParameters()[0]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,38 @@ public function resolve(mixed $subject, ?TypeContext $typeContext = null): Type
throw new UnsupportedException(\sprintf('Expected subject to be a "ReflectionProperty", a "ReflectionParameter" or a "ReflectionFunctionAbstract", "%s" given.', get_debug_type($subject)), $subject);
}

$docComment = match (true) {
$subject instanceof \ReflectionProperty => $subject->isPromoted() ? $subject->getDeclaringClass()?->getConstructor()?->getDocComment() : $subject->getDocComment(),
$subject instanceof \ReflectionParameter => $subject->getDeclaringFunction()->getDocComment(),
$subject instanceof \ReflectionFunctionAbstract => $subject->getDocComment(),
$typeContext ??= $this->typeContextFactory->createFromReflection($subject);

$docComments = match (true) {
$subject instanceof \ReflectionProperty => $subject->isPromoted()
? ['@var' => $subject->getDocComment(), '@param' => $subject->getDeclaringClass()?->getConstructor()?->getDocComment()]
: ['@var' => $subject->getDocComment()],
$subject instanceof \ReflectionParameter => ['@param' => $subject->getDeclaringFunction()->getDocComment()],
$subject instanceof \ReflectionFunctionAbstract => ['@return' => $subject->getDocComment()],
};

if (!$docComment) {
return $this->reflectionTypeResolver->resolve($subject);
}
foreach ($docComments as $tagName => $docComment) {
if (!$docComment) {
continue;
}

$typeContext ??= $this->typeContextFactory->createFromReflection($subject);
$tokens = new TokenIterator($this->lexer->tokenize($docComment));
$docNode = $this->phpDocParser->parse($tokens);

$tagName = match (true) {
$subject instanceof \ReflectionProperty => $subject->isPromoted() ? '@param' : '@var',
$subject instanceof \ReflectionParameter => '@param',
$subject instanceof \ReflectionFunctionAbstract => '@return',
};
foreach ($docNode->getTagsByName($tagName) as $tag) {
$tagValue = $tag->value;

$tokens = new TokenIterator($this->lexer->tokenize($docComment));
$docNode = $this->phpDocParser->parse($tokens);
if ('@var' === $tagName && $tagValue instanceof VarTagValueNode) {
return $this->stringTypeResolver->resolve((string) $tagValue, $typeContext);
}

foreach ($docNode->getTagsByName($tagName) as $tag) {
$tagValue = $tag->value;
if ('@param' === $tagName && $tagValue instanceof ParamTagValueNode && '$'.$subject->getName() === $tagValue->parameterName) {
return $this->stringTypeResolver->resolve((string) $tagValue, $typeContext);
}

if (
$tagValue instanceof VarTagValueNode
|| $tagValue instanceof ParamTagValueNode && $tagName && '$'.$subject->getName() === $tagValue->parameterName
|| $tagValue instanceof ReturnTagValueNode
) {
return $this->stringTypeResolver->resolve((string) $tagValue, $typeContext);
if ('@return' === $tagName && $tagValue instanceof ReturnTagValueNode) {
return $this->stringTypeResolver->resolve((string) $tagValue, $typeContext);
}
}
}

Expand Down
Loading