-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
getNextStatements
in UnreachableStatementNode
Co-authored-by: Ondrej Mirtes <[email protected]>
- Loading branch information
1 parent
068be33
commit e36bb83
Showing
6 changed files
with
203 additions
and
16 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
94 changes: 94 additions & 0 deletions
94
tests/PHPStan/Rules/DeadCode/UnreachableStatementNextStatementsRuleTest.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,94 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\DeadCode; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\UnreachableStatementNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<Rule> | ||
*/ | ||
class UnreachableStatementNextStatementsRuleTest extends RuleTestCase | ||
{ | ||
|
||
/** | ||
* @return Rule<Node> | ||
*/ | ||
protected function getRule(): Rule | ||
{ | ||
return new class implements Rule { | ||
|
||
public function getNodeType(): string | ||
{ | ||
return UnreachableStatementNode::class; | ||
} | ||
|
||
/** | ||
* @param UnreachableStatementNode $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$errors = [ | ||
RuleErrorBuilder::message('First unreachable') | ||
->identifier('tests.nextUnreachableStatements') | ||
->build(), | ||
]; | ||
|
||
foreach ($node->getNextStatements() as $nextStatement) { | ||
$errors[] = RuleErrorBuilder::message('Another unreachable') | ||
->line($nextStatement->getStartLine()) | ||
->identifier('tests.nextUnreachableStatements') | ||
->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
}; | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/multiple_unreachable.php'], [ | ||
[ | ||
'First unreachable', | ||
14, | ||
], | ||
[ | ||
'Another unreachable', | ||
15, | ||
], | ||
[ | ||
'Another unreachable', | ||
17, | ||
], | ||
[ | ||
'Another unreachable', | ||
22, | ||
], | ||
]); | ||
} | ||
|
||
public function testRuleTopLevel(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/multiple_unreachable_top_level.php'], [ | ||
[ | ||
'First unreachable', | ||
9, | ||
], | ||
[ | ||
'Another unreachable', | ||
10, | ||
], | ||
[ | ||
'Another unreachable', | ||
17, | ||
], | ||
]); | ||
} | ||
|
||
} |
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
23 changes: 23 additions & 0 deletions
23
tests/PHPStan/Rules/DeadCode/data/multiple_unreachable.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,23 @@ | ||
<?php | ||
|
||
namespace MultipleUnreachable; | ||
|
||
/** | ||
* @param 'foo' $foo | ||
*/ | ||
function foo($foo) | ||
{ | ||
if ($foo === 'foo') { | ||
return 1; | ||
} | ||
|
||
echo 'statement 1'; | ||
echo 'statement 2'; | ||
|
||
function innerFunction() | ||
{ | ||
echo 'statement 3'; | ||
} | ||
|
||
echo innerFunction(); | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/PHPStan/Rules/DeadCode/data/multiple_unreachable_top_level.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,17 @@ | ||
<?php | ||
|
||
namespace MultipleUnreachableTopLevel; | ||
|
||
if (true) { | ||
return 1; | ||
} | ||
|
||
echo 'statement 1'; | ||
echo 'statement 2'; | ||
|
||
function func() | ||
{ | ||
echo 'statement 3'; | ||
} | ||
|
||
echo func(); |