Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check - unreachable conditions (useful for !checkAlwaysTrue* projects)
- Loading branch information
1 parent
2a96f14
commit 186e4e0
Showing
4 changed files
with
135 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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
|
||
class UnreachableIfBranchesRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\If_::class; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Stmt\If_ $node | ||
* @param Scope $scope | ||
* @return RuleError[] | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$errors = []; | ||
$conditionType = $scope->getType($node->cond)->toBoolean(); | ||
$nextBranchIsDead = $conditionType instanceof ConstantBooleanType && $conditionType->getValue(); | ||
|
||
foreach ($node->elseifs as $elseif) { | ||
if ($nextBranchIsDead) { | ||
$errors[] = RuleErrorBuilder::message('Elseif branch is unreachable because previous condition is always true.')->line($elseif->getLine())->build(); | ||
continue; | ||
} | ||
|
||
$elseIfConditionType = $scope->getType($elseif->cond)->toBoolean(); | ||
$nextBranchIsDead = $elseIfConditionType instanceof ConstantBooleanType && $elseIfConditionType->getValue(); | ||
} | ||
|
||
if ($node->else !== null && $nextBranchIsDead) { | ||
$errors[] = RuleErrorBuilder::message('Else branch is unreachable because previous condition is always true.')->line($node->else->getLine())->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
tests/PHPStan/Rules/Comparison/UnreachableIfBranchesRuleTest.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,42 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Comparison; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
class UnreachableIfBranchesRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new UnreachableIfBranchesRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/unreachable-if-branches.php'], [ | ||
[ | ||
'Else branch is unreachable because previous condition is always true.', | ||
15, | ||
], | ||
[ | ||
'Elseif branch is unreachable because previous condition is always true.', | ||
25, | ||
], | ||
[ | ||
'Else branch is unreachable because previous condition is always true.', | ||
27, | ||
], | ||
[ | ||
'Elseif branch is unreachable because previous condition is always true.', | ||
39, | ||
], | ||
[ | ||
'Else branch is unreachable because previous condition is always true.', | ||
41, | ||
], | ||
]); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
tests/PHPStan/Rules/Comparison/data/unreachable-if-branches.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,45 @@ | ||
<?php | ||
|
||
function () { | ||
|
||
if (true) { | ||
|
||
} | ||
|
||
}; | ||
|
||
function () { | ||
|
||
if (true) { | ||
|
||
} else { // unreachable | ||
|
||
} | ||
|
||
}; | ||
|
||
function (string $str) { | ||
|
||
if (true) { | ||
|
||
} elseif ($str) { // unreachable | ||
|
||
} else { // unreachable | ||
|
||
} | ||
|
||
}; | ||
|
||
function (string $foo, string $bar) { | ||
|
||
if ($str) { | ||
|
||
} elseif (true) { | ||
|
||
} elseif ($bar) { // unreachable | ||
|
||
} else { // unreachable | ||
|
||
} | ||
|
||
}; |