-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect wrong case in name of an inherited method
- Loading branch information
1 parent
b80ae7a
commit 5c524c0
Showing
5 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,24 @@ rules: | |
- PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule | ||
- PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule | ||
- PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule | ||
- PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule | ||
- PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule | ||
- PHPStan\Rules\StrictCalls\StrictFunctionCallsRule | ||
- PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule | ||
|
||
services: | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lcobucci
|
||
scopeIsInClass: | ||
class: PHPStan\Build\ScopeIsInClassTypeSpecifyingExtension | ||
arguments: | ||
isInMethodName: isInClass | ||
removeNullMethodName: getClassReflection | ||
tags: | ||
- phpstan.typeSpecifier.methodTypeSpecifyingExtension | ||
|
||
scopeIsInTrait: | ||
class: PHPStan\Build\ScopeIsInClassTypeSpecifyingExtension | ||
arguments: | ||
isInMethodName: isInTrait | ||
removeNullMethodName: getTraitReflection | ||
tags: | ||
- phpstan.typeSpecifier.methodTypeSpecifyingExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ClassReflection; | ||
|
||
class WrongCaseOfInheritedMethodRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return \PhpParser\Node\Stmt\ClassMethod::class; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Stmt\ClassMethod $node | ||
* @param \PHPStan\Analyser\Scope $scope | ||
* @return string[] | ||
*/ | ||
public function processNode( | ||
\PhpParser\Node $node, | ||
Scope $scope | ||
): array | ||
{ | ||
if (!$scope->isInClass()) { | ||
throw new \PHPStan\ShouldNotHappenException(); | ||
} | ||
|
||
$methodReflection = $scope->getClassReflection()->getNativeMethod($node->name); | ||
$declaringClass = $methodReflection->getDeclaringClass(); | ||
|
||
$messages = []; | ||
if ($declaringClass->getParentClass() !== false) { | ||
$parentMessage = $this->findMethod( | ||
$declaringClass, | ||
$declaringClass->getParentClass(), | ||
$node->name | ||
); | ||
if ($parentMessage !== null) { | ||
$messages[] = $parentMessage; | ||
} | ||
} | ||
|
||
foreach ($declaringClass->getInterfaces() as $interface) { | ||
$interfaceMessage = $this->findMethod( | ||
$declaringClass, | ||
$interface, | ||
$node->name | ||
); | ||
if ($interfaceMessage === null) { | ||
continue; | ||
} | ||
$messages[] = $interfaceMessage; | ||
} | ||
|
||
return $messages; | ||
} | ||
|
||
private function findMethod( | ||
ClassReflection $declaringClass, | ||
ClassReflection $classReflection, | ||
string $methodName | ||
): ?string | ||
{ | ||
if (!$classReflection->hasNativeMethod($methodName)) { | ||
return null; | ||
} | ||
|
||
$parentMethod = $classReflection->getNativeMethod($methodName); | ||
if ($parentMethod->getName() === $methodName) { | ||
return null; | ||
} | ||
|
||
return sprintf( | ||
'Method %s::%s() does not match %s method name: %s::%s()', | ||
$declaringClass->getName(), | ||
$methodName, | ||
$classReflection->isInterface() ? 'interface' : 'parent', | ||
$classReflection->getName(), | ||
$parentMethod->getName() | ||
); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
tests/Rules/Methods/WrongCaseOfInheritedMethodRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
class WrongCaseOfInheritedMethodRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new WrongCaseOfInheritedMethodRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/wrong-case.php'], [ | ||
[ | ||
'Method WrongCase\Foo::GETfoo() does not match interface method name: WrongCase\FooInterface::getFoo()', | ||
25, | ||
], | ||
[ | ||
'Method WrongCase\Foo::GETbar() does not match parent method name: WrongCase\FooParent::getBar()', | ||
30, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace WrongCase; | ||
|
||
interface FooInterface | ||
{ | ||
|
||
public function getFoo(); | ||
|
||
} | ||
|
||
class FooParent | ||
{ | ||
|
||
public function getBar() | ||
{ | ||
|
||
} | ||
|
||
} | ||
|
||
class Foo extends FooParent implements FooInterface | ||
{ | ||
|
||
public function GETfoo() | ||
{ | ||
|
||
} | ||
|
||
public function GETbar() | ||
{ | ||
|
||
} | ||
|
||
public function getBaz() | ||
{ | ||
|
||
} | ||
|
||
} |
@ondrejmirtes is this definition correct? I know I'm living on the edge but I'm testing things with phpstan
v0.9
andv0.10@dev
and after the addition of these services the dev version is crashing becausePHPStan\Build\ScopeIsInClassTypeSpecifyingExtension
can't be found (this is not really an issue for me, it's just to make you aware of it 😄)