Skip to content

Commit

Permalink
Use the correct type for final constants
Browse files Browse the repository at this point in the history
  • Loading branch information
thg2k authored and ondrejmirtes committed Nov 17, 2024
1 parent 5e3a364 commit 25a06dd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Reflection/ClassConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(
private ?string $deprecatedDescription,
private bool $isDeprecated,
private bool $isInternal,
private bool $isFinal,
)
{
}
Expand Down Expand Up @@ -124,7 +125,7 @@ public function isPublic(): bool

public function isFinal(): bool
{
return $this->reflection->isFinal();
return $this->isFinal || $this->reflection->isFinal();
}

public function isDeprecated(): TrinaryLogic
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ public function getConstant(string $name): ClassConstantReflection
$deprecatedDescription = $resolvedPhpDoc->getDeprecatedTag() !== null ? $resolvedPhpDoc->getDeprecatedTag()->getMessage() : null;
$isDeprecated = $resolvedPhpDoc->isDeprecated();
$isInternal = $resolvedPhpDoc->isInternal();
$isFinal = $resolvedPhpDoc->isFinal();
$varTags = $resolvedPhpDoc->getVarTags();
if (isset($varTags[0]) && count($varTags) === 1) {
$phpDocType = $varTags[0]->getType();
Expand All @@ -1101,6 +1102,7 @@ public function getConstant(string $name): ClassConstantReflection
$deprecatedDescription,
$isDeprecated,
$isInternal,
$isFinal,
);
}
return $this->constants[$name];
Expand Down
1 change: 1 addition & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,7 @@ function (Type $type, callable $traverse): Type {
$constantReflection = $constantClassReflection->getConstant($constantName);
if (
!$constantClassReflection->isFinal()
&& !$constantReflection->isFinal()
&& !$constantReflection->hasPhpDocType()
&& !$constantReflection->hasNativeType()
) {
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/nsrt/class-constant-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class Foo
/** @var string */
private const PRIVATE_TYPE = 'foo';

/** @final */
const FINAL_TYPE = 'zoo';

public function doFoo()
{
assertType('1', self::NO_TYPE);
Expand All @@ -28,6 +31,10 @@ public function doFoo()
assertType('\'foo\'', self::PRIVATE_TYPE);
assertType('string', static::PRIVATE_TYPE);
assertType('string', $this::PRIVATE_TYPE);

assertType('\'zoo\'', self::FINAL_TYPE);
assertType('\'zoo\'', static::FINAL_TYPE);
assertType('\'zoo\'', $this::FINAL_TYPE);
}

}
Expand Down

0 comments on commit 25a06dd

Please sign in to comment.