From 99e2ffad86abb8e4b164b934a1bd538d46399123 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Mon, 13 Oct 2025 18:58:33 +0100 Subject: [PATCH 1/2] Remove the PHP version constraint from the `#[\Override]` attribute rules. --- src/Rules/Methods/OverridingMethodRule.php | 5 ++--- .../Rules/Methods/OverridingMethodRuleTest.php | 15 ++++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) 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..21004b3966 100644 --- a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php +++ b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php @@ -726,7 +726,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 +744,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 +793,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 +819,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 +826,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; From 52713b7fbe5c6d3951d6f4311d1d92b4419d07d4 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Mon, 13 Oct 2025 19:27:02 +0100 Subject: [PATCH 2/2] Test updates. --- .../Rules/Methods/OverridingMethodRuleTest.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php b/tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php index 21004b3966..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); }