diff --git a/src/Rules/Methods/OverridingMethodRule.php b/src/Rules/Methods/OverridingMethodRule.php index 3112bb3c3c..3a568a95a8 100644 --- a/src/Rules/Methods/OverridingMethodRule.php +++ b/src/Rules/Methods/OverridingMethodRule.php @@ -87,7 +87,7 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker $scope): array } } - if ($this->phpVersion->supportsOverrideAttribute() && $this->hasOverrideAttribute($node->getOriginalNode())) { + if ($this->hasOverrideAttribute($node->getOriginalNode())) { return [ RuleErrorBuilder::message(sprintf( 'Method %s::%s() has #[\Override] attribute but does not override any method.', @@ -111,8 +111,7 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker $scope): array $messages = []; if ( - $this->phpVersion->supportsOverrideAttribute() - && $this->checkMissingOverrideMethodAttribute + $this->checkMissingOverrideMethodAttribute && !$scope->isInTrait() && !$this->hasOverrideAttribute($node->getOriginalNode()) ) { diff --git a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php index 31793b5a77..a309c79faa 100644 --- a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php +++ b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php @@ -687,15 +687,12 @@ public function testBug9615(): void public function testBug10149(): void { $this->phpVersionId = PHP_VERSION_ID; - $errors = []; - if (PHP_VERSION_ID >= 80300) { - $errors = [ - [ - 'Method Bug10149\StdSat::__get() has #[\Override] attribute but does not override any method.', - 10, - ], - ]; - } + $errors = [ + [ + 'Method Bug10149\StdSat::__get() has #[\Override] attribute but does not override any method.', + 10, + ], + ]; $this->analyse([__DIR__ . '/data/bug-10149.php'], $errors); } @@ -726,7 +723,6 @@ public function testTraits(): void $this->analyse([__DIR__ . '/data/overriding-trait-methods.php'], $errors); } - #[RequiresPhp('>= 8.3')] public function testOverrideAttribute(): void { $this->phpVersionId = PHP_VERSION_ID; @@ -745,7 +741,16 @@ public function testOverrideAttribute(): void public static function dataCheckMissingOverrideAttribute(): iterable { yield [false, 80000, []]; - yield [true, 80000, []]; + yield [true, 80000, [ + [ + 'Method CheckMissingOverrideAttr\Bar::doFoo() overrides method CheckMissingOverrideAttr\Foo::doFoo() but is missing the #[\Override] attribute.', + 18, + ], + [ + 'Method CheckMissingOverrideAttr\ChildOfParentWithAbstractConstructor::__construct() overrides method CheckMissingOverrideAttr\ParentWithAbstractConstructor::__construct() but is missing the #[\Override] attribute.', + 49, + ], + ]]; yield [false, 80300, []]; yield [true, 80300, [ [ @@ -785,7 +790,6 @@ public function testBug10153(): void $this->analyse([__DIR__ . '/data/bug-10153.php'], $errors); } - #[RequiresPhp('>= 8.3')] public function testBug12471(): void { $this->checkMissingOverrideMethodAttribute = true; @@ -812,7 +816,6 @@ public function testSimpleXmlElementChildClass(): void $this->analyse([__DIR__ . '/data/simple-xml-element-child.php'], []); } - #[RequiresPhp('>= 8.3')] public function testFixOverride(): void { $this->phpVersionId = PHP_VERSION_ID; @@ -820,7 +823,6 @@ public function testFixOverride(): void $this->fix(__DIR__ . '/data/fix-override-attribute.php', __DIR__ . '/data/fix-override-attribute.php.fixed'); } - #[RequiresPhp('>= 8.3')] public function testFixWithTabs(): void { $this->phpVersionId = PHP_VERSION_ID;