Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Rules/Methods/OverridingMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand All @@ -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())
) {
Expand Down
30 changes: 16 additions & 14 deletions tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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, [
[
Expand Down Expand Up @@ -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;
Expand All @@ -812,15 +816,13 @@ 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;
$this->checkMissingOverrideMethodAttribute = true;
$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;
Expand Down
Loading