diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 4391b8097b..cfbe59613e 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -1079,6 +1079,10 @@ public function hasConsistentConstructor(): bool public function isFinalByKeyword(): bool { + if ($this->isAnonymous()) { + return true; + } + return $this->reflection->isFinal(); } diff --git a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php index 0aea96e4c3..114bf4f4c6 100644 --- a/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php +++ b/tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php @@ -6,6 +6,7 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; use PHPStan\Testing\RuleTestCase; +use const PHP_VERSION_ID; /** * @extends RuleTestCase @@ -735,4 +736,14 @@ public function testTaggedUnions(): void ]); } + public function testBug7904(): void + { + if (PHP_VERSION_ID < 80000) { + $this->markTestSkipped('Test requires PHP 8.0.'); + } + + $this->checkExplicitMixed = true; + $this->analyse([__DIR__ . '/data/bug-7904.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Methods/data/bug-7904.php b/tests/PHPStan/Rules/Methods/data/bug-7904.php new file mode 100644 index 0000000000..25eaa3e6b8 --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-7904.php @@ -0,0 +1,13 @@ += 8.0 + +namespace Bug7904; + +interface Test { + public static function create(): static; +} + +$impl = new class implements Test { + public static function create(): static { + return new self(); + } +};