Skip to content

Commit

Permalink
Overriding method rule - static/non-static
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 4, 2020
1 parent 093ae3f commit 437e99c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
24 changes: 21 additions & 3 deletions src/Rules/Methods/OverridingMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,34 @@ public function processNode(Node $node, Scope $scope): array
$prototype->getName()
))->nonIgnorable()->build();
}
} else {
if ($method->isPrivate()) {
} elseif ($method->isPrivate()) {
$messages[] = RuleErrorBuilder::message(sprintf(
'Private method %s::%s() overriding protected method %s::%s() should be protected or public.',
$method->getDeclaringClass()->getName(),
$method->getName(),
$prototype->getDeclaringClass()->getName(),
$prototype->getName()
))->nonIgnorable()->build();
}

if ($prototype->isStatic()) {
if (!$method->isStatic()) {
$messages[] = RuleErrorBuilder::message(sprintf(
'Private method %s::%s() overriding protected method %s::%s() should be protected or public.',
'Non-static method %s::%s() overrides static method %s::%s().',
$method->getDeclaringClass()->getName(),
$method->getName(),
$prototype->getDeclaringClass()->getName(),
$prototype->getName()
))->nonIgnorable()->build();
}
} elseif ($method->isStatic()) {
$messages[] = RuleErrorBuilder::message(sprintf(
'Static method %s::%s() overrides non-static method %s::%s().',
$method->getDeclaringClass()->getName(),
$method->getName(),
$prototype->getDeclaringClass()->getName(),
$prototype->getName()
))->nonIgnorable()->build();
}

return $messages;
Expand Down
16 changes: 12 additions & 4 deletions tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@ public function testOverridingFinalMethod(): void
$this->analyse([__DIR__ . '/data/overriding-final-method.php'], [
[
'Method OverridingFinalMethod\Bar::doFoo() overrides final method OverridingFinalMethod\Foo::doFoo().',
33,
43,
],
[
'Private method OverridingFinalMethod\Bar::doBar() overriding public method OverridingFinalMethod\Foo::doBar() should also be public.',
38,
48,
],
[
'Protected method OverridingFinalMethod\Bar::doBaz() overriding public method OverridingFinalMethod\Foo::doBaz() should also be public.',
43,
53,
],
[
'Private method OverridingFinalMethod\Bar::doLorem() overriding protected method OverridingFinalMethod\Foo::doLorem() should be protected or public.',
48,
58,
],
[
'Non-static method OverridingFinalMethod\Bar::doIpsum() overrides static method OverridingFinalMethod\Foo::doIpsum().',
63,
],
[
'Static method OverridingFinalMethod\Bar::doDolor() overrides non-static method OverridingFinalMethod\Foo::doDolor().',
68,
],
]);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/PHPStan/Rules/Methods/data/overriding-final-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ protected function doLorem()

}

public static function doIpsum()
{

}

public function doDolor()
{

}

}

class Bar extends Foo
Expand All @@ -50,4 +60,14 @@ private function doLorem()

}

public function doIpsum()
{

}

public static function doDolor()
{

}

}

0 comments on commit 437e99c

Please sign in to comment.