-
Notifications
You must be signed in to change notification settings - Fork 542
Allow process multiple UnreachableStatementNode on NodeScopeResolver::processNodes() #3745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ondrejmirtes
merged 20 commits into
phpstan:2.1.x
from
samsonasik:allow-process-multiple-unreachable
Jan 2, 2025
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e813a70
Allow process multiple UnreachableStatementNode on NodeScopeResolver:…
samsonasik 20212d6
fix phpstan notice
samsonasik c57d114
fix test on CatchWithUnthrownExceptionRuleTest
samsonasik 85068ed
add addNextStatement() and getNextStatements() method
samsonasik 1cd58c5
fix baseline
samsonasik b13a52f
fix cs
samsonasik d7a88aa
call $nodeCallback() after loop
samsonasik b3e4d26
make immutable, add namespace in test
samsonasik e4e2415
fix phpstan
samsonasik 979b614
fix unit test
samsonasik 54c68ac
fix cs
samsonasik 03bbc04
reuse duplicated process unreachable statement logic
samsonasik 4fa2d3a
add UnreachableStatementNextStatementsRuleTest
samsonasik 8ad84c0
Fix cs
samsonasik afd35aa
final touch: allow register inner function as next of after unreachab…
samsonasik 2d7efbc
final touch; fix phpstan
samsonasik 2c625cd
Adjust test
ondrejmirtes 10dfba4
Generics here no longer make sense
ondrejmirtes d553fb3
Failing test for top level scope
ondrejmirtes 2427d86
fix top level function
samsonasik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.