Skip to content

Commit 25a06dd

Browse files
thg2kondrejmirtes
authored andcommitted
Use the correct type for final constants
1 parent 5e3a364 commit 25a06dd

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

src/Reflection/ClassConstantReflection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function __construct(
2828
private ?string $deprecatedDescription,
2929
private bool $isDeprecated,
3030
private bool $isInternal,
31+
private bool $isFinal,
3132
)
3233
{
3334
}
@@ -124,7 +125,7 @@ public function isPublic(): bool
124125

125126
public function isFinal(): bool
126127
{
127-
return $this->reflection->isFinal();
128+
return $this->isFinal || $this->reflection->isFinal();
128129
}
129130

130131
public function isDeprecated(): TrinaryLogic

src/Reflection/ClassReflection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ public function getConstant(string $name): ClassConstantReflection
10801080
$deprecatedDescription = $resolvedPhpDoc->getDeprecatedTag() !== null ? $resolvedPhpDoc->getDeprecatedTag()->getMessage() : null;
10811081
$isDeprecated = $resolvedPhpDoc->isDeprecated();
10821082
$isInternal = $resolvedPhpDoc->isInternal();
1083+
$isFinal = $resolvedPhpDoc->isFinal();
10831084
$varTags = $resolvedPhpDoc->getVarTags();
10841085
if (isset($varTags[0]) && count($varTags) === 1) {
10851086
$phpDocType = $varTags[0]->getType();
@@ -1101,6 +1102,7 @@ public function getConstant(string $name): ClassConstantReflection
11011102
$deprecatedDescription,
11021103
$isDeprecated,
11031104
$isInternal,
1105+
$isFinal,
11041106
);
11051107
}
11061108
return $this->constants[$name];

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,7 @@ function (Type $type, callable $traverse): Type {
19871987
$constantReflection = $constantClassReflection->getConstant($constantName);
19881988
if (
19891989
!$constantClassReflection->isFinal()
1990+
&& !$constantReflection->isFinal()
19901991
&& !$constantReflection->hasPhpDocType()
19911992
&& !$constantReflection->hasNativeType()
19921993
) {

tests/PHPStan/Analyser/nsrt/class-constant-types.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class Foo
1515
/** @var string */
1616
private const PRIVATE_TYPE = 'foo';
1717

18+
/** @final */
19+
const FINAL_TYPE = 'zoo';
20+
1821
public function doFoo()
1922
{
2023
assertType('1', self::NO_TYPE);
@@ -28,6 +31,10 @@ public function doFoo()
2831
assertType('\'foo\'', self::PRIVATE_TYPE);
2932
assertType('string', static::PRIVATE_TYPE);
3033
assertType('string', $this::PRIVATE_TYPE);
34+
35+
assertType('\'zoo\'', self::FINAL_TYPE);
36+
assertType('\'zoo\'', static::FINAL_TYPE);
37+
assertType('\'zoo\'', $this::FINAL_TYPE);
3138
}
3239

3340
}

0 commit comments

Comments
 (0)