-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect unused method call on a separate line with possibly pure method
- Loading branch information
Showing
7 changed files
with
395 additions
and
2 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
68 changes: 68 additions & 0 deletions
68
src/Rules/DeadCode/CallToMethodStatementWithoutImpurePointsRule.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,68 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\DeadCode; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\CollectedDataNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function array_key_exists; | ||
use function sprintf; | ||
use function strtolower; | ||
|
||
/** | ||
* @implements Rule<CollectedDataNode> | ||
*/ | ||
class CallToMethodStatementWithoutImpurePointsRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return CollectedDataNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$methods = []; | ||
foreach ($node->get(MethodWithoutImpurePointsCollector::class) as $collected) { | ||
foreach ($collected as [$className, $methodName, $classDisplayName]) { | ||
$className = strtolower($className); | ||
|
||
if (!array_key_exists($className, $methods)) { | ||
$methods[$className] = []; | ||
} | ||
$methods[$className][strtolower($methodName)] = $classDisplayName . '::' . $methodName; | ||
} | ||
} | ||
|
||
$errors = []; | ||
foreach ($node->get(PossiblyPureMethodCallCollector::class) as $filePath => $data) { | ||
foreach ($data as [$className, $method, $line]) { | ||
$className = strtolower($className); | ||
|
||
if (!array_key_exists($className, $methods)) { | ||
continue; | ||
} | ||
|
||
$lowerMethod = strtolower($method); | ||
if (!array_key_exists($lowerMethod, $methods[$className])) { | ||
continue; | ||
} | ||
|
||
$originalMethodName = $methods[$className][$lowerMethod]; | ||
|
||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Call to method %s() on a separate line has no effect.', | ||
$originalMethodName, | ||
))->file($filePath) | ||
->line($line) | ||
->identifier('method.resultUnused') | ||
->build(); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\DeadCode; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
use PHPStan\Node\MethodReturnStatementsNode; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use function count; | ||
|
||
/** | ||
* @implements Collector<MethodReturnStatementsNode, array{class-string, string, string}> | ||
*/ | ||
class MethodWithoutImpurePointsCollector implements Collector | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return MethodReturnStatementsNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope) | ||
{ | ||
$method = $node->getMethodReflection(); | ||
if (!$method->isPure()->maybe()) { | ||
return null; | ||
} | ||
if (!$method->hasSideEffects()->maybe()) { | ||
return null; | ||
} | ||
|
||
if (count($node->getImpurePoints()) !== 0) { | ||
return null; | ||
} | ||
|
||
if (count($node->getStatementResult()->getThrowPoints()) !== 0) { | ||
return null; | ||
} | ||
|
||
$variant = ParametersAcceptorSelector::selectSingle($method->getVariants()); | ||
foreach ($variant->getParameters() as $parameter) { | ||
if (!$parameter->passedByReference()->createsNewVariable()) { | ||
continue; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
if (count($method->getAsserts()->getAll()) !== 0) { | ||
return null; | ||
} | ||
|
||
return [$method->getDeclaringClass()->getName(), $method->getName(), $method->getDeclaringClass()->getDisplayName()]; | ||
} | ||
|
||
} |
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,68 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\DeadCode; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
use function count; | ||
use function strtolower; | ||
|
||
/** | ||
* @implements Collector<Node\Stmt\Expression, array{class-string, string, int}> | ||
*/ | ||
class PossiblyPureMethodCallCollector implements Collector | ||
{ | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Expression::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope) | ||
{ | ||
if (!$node->expr instanceof Node\Expr\MethodCall) { | ||
return null; | ||
} | ||
if (!$node->expr->name instanceof Node\Identifier) { | ||
return null; | ||
} | ||
|
||
$methodName = $node->expr->name->toString(); | ||
if (strtolower($methodName) === '__construct') { | ||
return null; | ||
} | ||
|
||
$calledOnType = $scope->getType($node->expr->var); | ||
$methodReflection = $scope->getMethodReflection($calledOnType, $methodName); | ||
if ($methodReflection === null) { | ||
return null; | ||
} | ||
if (!$methodReflection->isFinal()->yes()) { | ||
if (!$methodReflection->getDeclaringClass()->isFinal()) { | ||
$typeClassReflections = $calledOnType->getObjectClassReflections(); | ||
if (count($typeClassReflections) !== 1) { | ||
return null; | ||
} | ||
|
||
if (!$typeClassReflections[0]->isFinal()) { | ||
return null; | ||
} | ||
} | ||
} | ||
if (!$methodReflection->isPure()->maybe()) { | ||
return null; | ||
} | ||
if (!$methodReflection->hasSideEffects()->maybe()) { | ||
return null; | ||
} | ||
|
||
return [$methodReflection->getDeclaringClass()->getName(), $methodReflection->getName(), $node->getStartLine()]; | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
tests/PHPStan/Rules/DeadCode/CallToMethodStatementWithoutImpurePointsRuleTest.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,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\DeadCode; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<CallToMethodStatementWithoutImpurePointsRule> | ||
*/ | ||
class CallToMethodStatementWithoutImpurePointsRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new CallToMethodStatementWithoutImpurePointsRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/call-to-method-without-impure-points.php'], [ | ||
[ | ||
'Call to method CallToMethodWithoutImpurePoints\finalX::myFunc() on a separate line has no effect.', | ||
7, | ||
], | ||
[ | ||
'Call to method CallToMethodWithoutImpurePoints\finalX::myFunc() on a separate line has no effect.', | ||
8, | ||
], | ||
[ | ||
'Call to method CallToMethodWithoutImpurePoints\foo::finalFunc() on a separate line has no effect.', | ||
30, | ||
], | ||
[ | ||
'Call to method CallToMethodWithoutImpurePoints\y::myFinalBaseFunc() on a separate line has no effect.', | ||
36, | ||
], | ||
[ | ||
'Call to method CallToMethodWithoutImpurePoints\y::myFunc() on a separate line has no effect.', | ||
39, | ||
], | ||
[ | ||
'Call to method CallToMethodWithoutImpurePoints\finalSubSubY::mySubSubFunc() on a separate line has no effect.', | ||
40, | ||
], | ||
[ | ||
'Call to method CallToMethodWithoutImpurePoints\y::myFinalBaseFunc() on a separate line has no effect.', | ||
41, | ||
], | ||
[ | ||
'Call to method CallToMethodWithoutImpurePoints\AbstractFoo::myFunc() on a separate line has no effect.', | ||
119, | ||
], | ||
]); | ||
} | ||
|
||
protected function getCollectors(): array | ||
{ | ||
return [ | ||
new PossiblyPureMethodCallCollector(), | ||
new MethodWithoutImpurePointsCollector(), | ||
]; | ||
} | ||
|
||
} |
Oops, something went wrong.