Skip to content
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
118 changes: 90 additions & 28 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\OutOfClassScope;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClassConstant;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnum;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionEnumBackedCase;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
Expand Down Expand Up @@ -1272,22 +1273,7 @@ public function getConstant(string $name): ClassConstantReflection
}
$fileName = $declaringClass->getFileName();
$phpDocType = null;
$currentResolvedPhpDoc = $this->stubPhpDocProvider->findClassConstantPhpDoc(
$declaringClass->getName(),
$name,
);
if ($currentResolvedPhpDoc === null) {
if ($reflectionConstant->getDocComment() !== false) {
$currentResolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$fileName,
$declaringClass->getName(),
null,
null,
$reflectionConstant->getDocComment(),
);
}

}
$currentResolvedPhpDoc = $this->findConstantResolvedPhpDoc($reflectionConstant);

$nativeType = null;
if ($reflectionConstant->getType() !== null) {
Expand All @@ -1311,18 +1297,7 @@ public function getConstant(string $name): ClassConstantReflection
}
$isInternal = $resolvedPhpDoc->isInternal();
$isFinal = $resolvedPhpDoc->isFinal();
$varTags = $resolvedPhpDoc->getVarTags();
if (isset($varTags[0]) && count($varTags) === 1) {
$varTag = $varTags[0];
if ($varTag->isExplicit() || $nativeType === null || $nativeType->isSuperTypeOf($varTag->getType())->yes()) {
$phpDocType = TemplateTypeHelper::resolveTemplateTypes(
$varTag->getType(),
$declaringClass->getActiveTemplateTypeMap(),
$declaringClass->getCallSiteVarianceMap(),
TemplateTypeVariance::createInvariant(),
);
}
}
$phpDocType = self::resolveConstantVarPhpDocType($resolvedPhpDoc, $nativeType, $declaringClass);
}

$this->constants[$name] = new RealClassClassConstantReflection(
Expand All @@ -1342,6 +1317,93 @@ public function getConstant(string $name): ClassConstantReflection
return $this->constants[$name];
}

/**
* Returns the @var PHPDoc type of a class constant, if any.
* Does not walk ancestors, so it's safe to call from class-constant
* type resolution (which re-enters via @extends<value-of<...>>).
*
* @internal
*/
public function getConstantPhpDocType(string $name): ?Type
{
$reflectionConstant = $this->getNativeReflection()->getReflectionConstant($name);
if ($reflectionConstant === false) {
return null;
}

$resolvedPhpDoc = $this->findConstantResolvedPhpDoc($reflectionConstant);
if ($resolvedPhpDoc === null) {
return null;
}

$nativeType = null;
if ($reflectionConstant->getType() !== null) {
$nativeType = TypehintHelper::decideTypeFromReflection($reflectionConstant->getType());
}

$declaringClassName = $reflectionConstant->getDeclaringClass()->getName();
if ($declaringClassName === $this->getName()) {
$declaringClass = $this;
} else {
$declaringClass = $this->getAncestorWithClassName($declaringClassName);
if ($declaringClass === null) {
return null;
}
}

return self::resolveConstantVarPhpDocType($resolvedPhpDoc, $nativeType, $declaringClass);
}

private function findConstantResolvedPhpDoc(ReflectionClassConstant $reflectionConstant): ?ResolvedPhpDocBlock
{
$declaringClassName = $reflectionConstant->getDeclaringClass()->getName();

$resolvedPhpDoc = $this->stubPhpDocProvider->findClassConstantPhpDoc(
$declaringClassName,
$reflectionConstant->getName(),
);
if ($resolvedPhpDoc !== null) {
return $resolvedPhpDoc;
}

$docComment = $reflectionConstant->getDocComment();
if ($docComment === false) {
return null;
}

return $this->fileTypeMapper->getResolvedPhpDoc(
$reflectionConstant->getDeclaringClass()->getFileName() ?: null,
$declaringClassName,
null,
null,
$docComment,
);
}

private static function resolveConstantVarPhpDocType(
ResolvedPhpDocBlock $resolvedPhpDoc,
?Type $nativeType,
self $declaringClass,
): ?Type
{
$varTags = $resolvedPhpDoc->getVarTags();
if (!isset($varTags[0]) || count($varTags) !== 1) {
return null;
}

$varTag = $varTags[0];
if (!$varTag->isExplicit() && $nativeType !== null && !$nativeType->isSuperTypeOf($varTag->getType())->yes()) {
return null;
}

return TemplateTypeHelper::resolveTemplateTypes(
$varTag->getType(),
$declaringClass->getActiveTemplateTypeMap(),
$declaringClass->getCallSiteVarianceMap(),
TemplateTypeVariance::createInvariant(),
);
}

public function hasTraitUse(string $traitName): bool
{
return in_array($traitName, $this->getTraitNames(), true);
Expand Down
5 changes: 2 additions & 3 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2544,13 +2544,12 @@ function (Type $type, callable $traverse): Type {
if ($reflectionConstant->getType() !== null) {
$nativeType = TypehintHelper::decideTypeFromReflection($reflectionConstant->getType(), selfClass: $constantClassReflection);
}
$phpDocType = $constantClassReflection->getConstant($constantName)->getPhpDocType();
$types[] = $this->constantResolver->resolveClassConstantType(
$constantClassReflection->getName(),
$constantName,
$constantType,
$nativeType,
$phpDocType,
$constantClassReflection->getConstantPhpDocType($constantName),
);
unset($this->currentlyResolvingClassConstant[$resolvingName]);
continue;
Expand Down Expand Up @@ -2579,7 +2578,7 @@ function (Type $type, callable $traverse): Type {
$constantName,
$constantType,
$nativeType,
$constantReflection->getPhpDocType(),
$constantClassReflection->getConstantPhpDocType($constantName),
);
unset($this->currentlyResolvingClassConstant[$resolvingName]);
$types[] = $constantType;
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,13 @@ public function testBugInfiniteLoopOnFileTypeMapper(): void
$this->assertCount(0, $errors);
}

#[RequiresPhp('>= 8.3.0')]
public function testBug14501(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-14501.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return list<Error>
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/data/bug-14501.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace Bug14501;

function bug14501Trigger(): string
{
return Bug14501Sub::ATTR_A;
}

/**
* @template TAttribute of string
*/
abstract class Bug14501Base
{
/**
* @param TAttribute $attribute
*/
abstract public function check(string $attribute): bool;
}

/** @extends Bug14501Base<value-of<self::ATTRIBUTES>> */
final class Bug14501Sub extends Bug14501Base
{
public const string ATTR_A = 'A';
public const string ATTR_B = 'B';

public const array ATTRIBUTES = [
self::ATTR_A,
self::ATTR_B,
];

public function check(string $attribute): bool
{
return $attribute === self::ATTR_A;
}
}
Loading