-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect unused constructor method calls
- Loading branch information
1 parent
25d5715
commit 21c50e3
Showing
4 changed files
with
107 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
64 changes: 64 additions & 0 deletions
64
src/Rules/Methods/CallToConstructorStatementWithoutSideEffectsRule.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,64 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Expression> | ||
*/ | ||
class CallToConstructorStatementWithoutSideEffectsRule implements Rule | ||
{ | ||
|
||
private ReflectionProvider $reflectionProvider; | ||
|
||
public function __construct(ReflectionProvider $reflectionProvider) | ||
{ | ||
$this->reflectionProvider = $reflectionProvider; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Expression::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->expr instanceof Node\Expr\New_) { | ||
return []; | ||
} | ||
|
||
$instantiation = $node->expr; | ||
if (!$instantiation->class instanceof Node\Name) { | ||
return []; | ||
} | ||
|
||
$className = $scope->resolveName($instantiation->class); | ||
if (!$this->reflectionProvider->hasClass($className)) { | ||
return []; | ||
} | ||
|
||
$classReflection = $this->reflectionProvider->getClass($className); | ||
if (!$classReflection->hasConstructor()) { | ||
return []; | ||
} | ||
|
||
$constructor = $classReflection->getConstructor(); | ||
if ($constructor->hasSideEffects()->no()) { | ||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Call to %s::%s() on a separate line has no effect.', | ||
$classReflection->getDisplayName(), | ||
$constructor->getName() | ||
))->build(), | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
tests/PHPStan/Rules/Methods/CallToConstructorStatementWithoutSideEffectsRuleTest.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,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<CallToConstructorStatementWithoutSideEffectsRule> | ||
*/ | ||
class CallToConstructorStatementWithoutSideEffectsRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new CallToConstructorStatementWithoutSideEffectsRule($this->createReflectionProvider()); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/constructor-statement-no-side-effects.php'], [ | ||
[ | ||
'Call to Exception::__construct() on a separate line has no effect.', | ||
6, | ||
], | ||
]); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
tests/PHPStan/Rules/Methods/data/constructor-statement-no-side-effects.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,13 @@ | ||
<?php | ||
|
||
namespace ConstructorStatementNoSideEffects; | ||
|
||
function () { | ||
new \Exception(); | ||
throw new \Exception(); | ||
}; | ||
|
||
function () { | ||
new \PDOStatement(); | ||
new \stdClass(); | ||
}; |