Fix #5865: Do-while loop broken by exception not supported (reported as always-true)#5078
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix #5865: Do-while loop broken by exception not supported (reported as always-true)#5078phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…oints - DoWhileLoopConstantConditionRule now considers explicit throw points as valid loop exit mechanisms, suppressing the "always true" warning - Added throw points to DoWhileLoopConditionNode alongside exit points - Updated NodeScopeResolver to pass throw points when creating the node - New regression test in tests/PHPStan/Rules/Comparison/data/bug-5865.php Closes phpstan/phpstan#5865
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
Dunno what you think about this fix @staabm
To me, since lot of method has throws phpdoc, I feel like it will not report always true while loop anymore.
public function sayHello(): void
{
do {
$this->foo();
} while (true);
}
should be still reported for instance.
Contributor
|
@VincentLanglet you could add test coverage for your mentioned example, close this PR and let the bot try a 2nd time after your case is covered in tests |
Contributor
|
Closing for now because of phpstan/phpstan#5865 (comment) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
A
do { ... } while (true);loop whose body contains a function call with an explicit@throwsannotation was incorrectly reported as "Do-while loop condition is always true." The loop is intentionally infinite and exits only via an exception, which is a valid pattern.Changes
src/Node/DoWhileLoopConditionNode.php— Added$throwPointsparameter andgetThrowPoints()method so the rule can inspect throw points from the loop bodysrc/Analyser/NodeScopeResolver.php— Pass throw points alongside exit points when constructingDoWhileLoopConditionNodesrc/Rules/Comparison/DoWhileLoopConstantConditionRule.php— When the condition is alwaystrue, also check for explicit throw points; if any exist, the loop can exit via exception and the warning is suppressedtests/PHPStan/Rules/Comparison/DoWhileLoopConstantConditionRuleTest.php— AddedtestBug5865tests/PHPStan/Rules/Comparison/data/bug-5865.php— Regression test dataRoot cause
The rule already recognized direct
throwstatements andbreak/returnas valid exit mechanisms fordo-while(true)loops (via exit points). However, function calls annotated with@throwsgenerate "throw points" rather than "exit points." The rule did not check throw points, so it could not see that the loop body had an explicit way to exit via exception. The fix adds explicit throw point checking to the rule.Test
The regression test reproduces the exact scenario from the issue: a
do-while(true)loop inside a try-catch where the loop body calls a method annotated with@throws RuntimeException. The test expects no errors.Fixes phpstan/phpstan#5865