Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scope - universal constant booleans for type-specified methods and st…
…atic methods, related ImpossibleCheckType rules
- Loading branch information
1 parent
49aa301
commit 43ebec3
Showing
9 changed files
with
391 additions
and
17 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
80 changes: 80 additions & 0 deletions
80
src/Rules/Comparison/ImpossibleCheckTypeMethodCallRule.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,80 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
|
||
class ImpossibleCheckTypeMethodCallRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
/** @var bool */ | ||
private $checkAlwaysTrueCheckTypeFunctionCall; | ||
|
||
public function __construct( | ||
bool $checkAlwaysTrueCheckTypeFunctionCall | ||
) | ||
{ | ||
$this->checkAlwaysTrueCheckTypeFunctionCall = $checkAlwaysTrueCheckTypeFunctionCall; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return \PhpParser\Node\Expr\MethodCall::class; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Expr\MethodCall $node | ||
* @param \PHPStan\Analyser\Scope $scope | ||
* @return string[] errors | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!is_string($node->name)) { | ||
return []; | ||
} | ||
|
||
$nodeType = $scope->getType($node); | ||
if (!$nodeType instanceof ConstantBooleanType) { | ||
return []; | ||
} | ||
|
||
if (!$nodeType->getValue()) { | ||
$method = $this->getMethod($node->var, $node->name, $scope); | ||
|
||
return [sprintf( | ||
'Call to method %s::%s() will always evaluate to false.', | ||
$method->getDeclaringClass()->getDisplayName(), | ||
$method->getName() | ||
)]; | ||
} elseif ($this->checkAlwaysTrueCheckTypeFunctionCall) { | ||
$method = $this->getMethod($node->var, $node->name, $scope); | ||
|
||
return [sprintf( | ||
'Call to method %s::%s() will always evaluate to true.', | ||
$method->getDeclaringClass()->getDisplayName(), | ||
$method->getName() | ||
)]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
private function getMethod( | ||
Expr $var, | ||
string $methodName, | ||
Scope $scope | ||
): MethodReflection | ||
{ | ||
$calledOnType = $scope->getType($var); | ||
if (!$calledOnType->hasMethod($methodName)) { | ||
throw new \PHPStan\ShouldNotHappenException(); | ||
} | ||
|
||
return $calledOnType->getMethod($methodName, $scope); | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
src/Rules/Comparison/ImpossibleCheckTypeStaticMethodCallRule.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,93 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\ObjectType; | ||
|
||
class ImpossibleCheckTypeStaticMethodCallRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
/** @var bool */ | ||
private $checkAlwaysTrueCheckTypeFunctionCall; | ||
|
||
public function __construct( | ||
bool $checkAlwaysTrueCheckTypeFunctionCall | ||
) | ||
{ | ||
$this->checkAlwaysTrueCheckTypeFunctionCall = $checkAlwaysTrueCheckTypeFunctionCall; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return \PhpParser\Node\Expr\StaticCall::class; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Expr\StaticCall $node | ||
* @param \PHPStan\Analyser\Scope $scope | ||
* @return string[] errors | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!is_string($node->name)) { | ||
return []; | ||
} | ||
|
||
$nodeType = $scope->getType($node); | ||
if (!$nodeType instanceof ConstantBooleanType) { | ||
return []; | ||
} | ||
|
||
if (!$nodeType->getValue()) { | ||
$method = $this->getMethod($node->class, $node->name, $scope); | ||
|
||
return [sprintf( | ||
'Call to static method %s::%s() will always evaluate to false.', | ||
$method->getDeclaringClass()->getDisplayName(), | ||
$method->getName() | ||
)]; | ||
} elseif ($this->checkAlwaysTrueCheckTypeFunctionCall) { | ||
$method = $this->getMethod($node->class, $node->name, $scope); | ||
|
||
return [sprintf( | ||
'Call to static method %s::%s() will always evaluate to true.', | ||
$method->getDeclaringClass()->getDisplayName(), | ||
$method->getName() | ||
)]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* @param Node\Name|Expr $class | ||
* @param string $methodName | ||
* @param Scope $scope | ||
* @return MethodReflection | ||
* @throws \PHPStan\ShouldNotHappenException | ||
*/ | ||
private function getMethod( | ||
$class, | ||
string $methodName, | ||
Scope $scope | ||
): MethodReflection | ||
{ | ||
if ($class instanceof Node\Name) { | ||
$calledOnType = new ObjectType($scope->resolveName($class)); | ||
} else { | ||
$calledOnType = $scope->getType($class); | ||
} | ||
|
||
if (!$calledOnType->hasMethod($methodName)) { | ||
throw new \PHPStan\ShouldNotHappenException(); | ||
} | ||
|
||
return $calledOnType->getMethod($methodName, $scope); | ||
} | ||
|
||
} |
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
39 changes: 39 additions & 0 deletions
39
tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeMethodCallRuleTest.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,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PHPStan\Tests\AssertionClassMethodTypeSpecifyingExtension; | ||
|
||
class ImpossibleCheckTypeMethodCallRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
public function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new ImpossibleCheckTypeMethodCallRule(true); | ||
} | ||
|
||
/** | ||
* @return \PHPStan\Type\MethodTypeSpecifyingExtension[] | ||
*/ | ||
protected function getMethodTypeSpecifyingExtensions(): array | ||
{ | ||
return [ | ||
new AssertionClassMethodTypeSpecifyingExtension(null), | ||
]; | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/impossible-method-call.php'], [ | ||
[ | ||
'Call to method PHPStan\Tests\AssertionClass::assertString() will always evaluate to true.', | ||
14, | ||
], | ||
[ | ||
'Call to method PHPStan\Tests\AssertionClass::assertString() will always evaluate to false.', | ||
15, | ||
], | ||
]); | ||
} | ||
|
||
} |
Oops, something went wrong.