diff --git a/Makefile b/Makefile index d50e4608075..776b1082bb3 100644 --- a/Makefile +++ b/Makefile @@ -118,6 +118,7 @@ lint: --exclude tests/PHPStan/Rules/Properties/data/property-hook-attributes-nodiscard.php \ --exclude tests/PHPStan/Rules/Functions/data/arrow-function-typehints-nodiscard.php \ --exclude tests/PHPStan/Rules/Functions/data/closure-typehints-nodiscard.php \ + --exclude tests/PHPStan/Reflection/data/ClassWithConstants.php \ --exclude tests/PHPStan/Rules/Functions/data/typehints-nodiscard.php \ --exclude tests/PHPStan/Rules/Methods/data/typehints-nodiscard.php \ --exclude tests/PHPStan/Rules/Cast/data/deprecated-cast.php \ diff --git a/src/Reflection/ClassConstantReflection.php b/src/Reflection/ClassConstantReflection.php index 15d9038c1cc..097082b7618 100644 --- a/src/Reflection/ClassConstantReflection.php +++ b/src/Reflection/ClassConstantReflection.php @@ -28,6 +28,8 @@ public function getValueExpr(): Expr; public function isFinal(): bool; + public function isFinalByKeyword(): bool; + public function hasPhpDocType(): bool; public function getPhpDocType(): ?Type; diff --git a/src/Reflection/Dummy/DummyClassConstantReflection.php b/src/Reflection/Dummy/DummyClassConstantReflection.php index 7cee1c0fe3d..768c5bdf275 100644 --- a/src/Reflection/Dummy/DummyClassConstantReflection.php +++ b/src/Reflection/Dummy/DummyClassConstantReflection.php @@ -32,6 +32,11 @@ public function isFinal(): bool return false; } + public function isFinalByKeyword(): bool + { + return false; + } + public function getFileName(): ?string { return null; diff --git a/src/Reflection/RealClassClassConstantReflection.php b/src/Reflection/RealClassClassConstantReflection.php index eaf0971a47d..d0b69f5eedc 100644 --- a/src/Reflection/RealClassClassConstantReflection.php +++ b/src/Reflection/RealClassClassConstantReflection.php @@ -121,6 +121,11 @@ public function isFinal(): bool return $this->isFinal || $this->reflection->isFinal(); } + public function isFinalByKeyword(): bool + { + return $this->reflection->isFinal(); + } + public function isDeprecated(): TrinaryLogic { return TrinaryLogic::createFromBoolean($this->isDeprecated || $this->reflection->isDeprecated()); diff --git a/src/Rules/RestrictedUsage/RewrittenDeclaringClassClassConstantReflection.php b/src/Rules/RestrictedUsage/RewrittenDeclaringClassClassConstantReflection.php index 9feae078a10..a92a6172737 100644 --- a/src/Rules/RestrictedUsage/RewrittenDeclaringClassClassConstantReflection.php +++ b/src/Rules/RestrictedUsage/RewrittenDeclaringClassClassConstantReflection.php @@ -29,6 +29,11 @@ public function isFinal(): bool return $this->constantReflection->isFinal(); } + public function isFinalByKeyword(): bool + { + return $this->constantReflection->isFinalByKeyword(); + } + public function hasPhpDocType(): bool { return $this->constantReflection->hasPhpDocType(); diff --git a/tests/PHPStan/Reflection/ClassReflectionTest.php b/tests/PHPStan/Reflection/ClassReflectionTest.php index 78c0295b7b8..5f036eb949f 100644 --- a/tests/PHPStan/Reflection/ClassReflectionTest.php +++ b/tests/PHPStan/Reflection/ClassReflectionTest.php @@ -7,6 +7,7 @@ use Attributes\IsAttribute2; use Attributes\IsAttribute3; use Attributes\IsNotAttribute; +use ClassConstantReflectionTest\ClassWithConstants; use GenericInheritance\C; use HasTraitUse\Bar; use HasTraitUse\Baz; @@ -191,6 +192,20 @@ public function testDeprecatedConstantFromAnotherFile(): void $this->assertTrue($constant->isDeprecated()->yes()); } + #[RequiresPhp('>= 8.1')] + public function testFinalConstant(): void + { + $reflectionProvider = self::createReflectionProvider(); + $reflection = $reflectionProvider->getClass(ClassWithConstants::class); + $constant = $reflection->getConstant('FINAL_FROM_DOCBLOCK'); + $this->assertTrue($constant->isFinal()); + $this->assertFalse($constant->isFinalByKeyword()); + + $constant = $reflection->getConstant('NATIVE_FINAL'); + $this->assertTrue($constant->isFinal()); + $this->assertTrue($constant->isFinalByKeyword()); + } + /** * @param class-string $className * @param array $expected diff --git a/tests/PHPStan/Reflection/data/ClassWithConstants.php b/tests/PHPStan/Reflection/data/ClassWithConstants.php new file mode 100644 index 00000000000..b24e65e6ed6 --- /dev/null +++ b/tests/PHPStan/Reflection/data/ClassWithConstants.php @@ -0,0 +1,13 @@ +