Do not report do-while-false pseudo-loops that are interrupted by a break statement#4551
Merged
ondrejmirtes merged 2 commits intophpstan:2.1.xfrom Feb 10, 2026
Merged
Conversation
543d8ad to
fca23a9
Compare
Contributor
Author
|
(The second commit did not change semantics, only simplified redundant conditions.) |
97e48d3 to
4ad243c
Compare
Member
|
I rebased the PR. It needs some fixes: |
4ad243c to
8e745e7
Compare
Contributor
Author
|
@ondrejmirtes fixed (and I wonder why it didn’t make the test fail, but anyway...) |
Member
|
Thank you! |
Contributor
|
@claudepache how did you come up with this construction in PHP? I am aware of this pattern in languages which have block-scoped variables, but I can't find a use-case in PHP (because variables will be alive even after the block they are assigned/declared in). no shaming.. I am just curious |
Member
|
@staabm I'd guess it's another way of basically doing |
Contributor
Author
|
@staabm Nothing to do with variable scope, only with code flow. Here is an example of use: // $date_in might be: '2.2025', '02 2025', '02-2025', '2025-02', '2/2025', '022025', ...
do {
$date_tmp = explode('.', $date_in);
if (ctype_digit($date_tmp[1] ?? ''))
break;
$date_tmp = explode(' ', $date_in);
if (ctype_digit($date_tmp[1] ?? ''))
break;
$date_tmp = explode('-', $date_in);
if (ctype_digit($date_tmp[1] ?? ''))
break;
$date_tmp = explode('/', $date_in);
if (ctype_digit($date_tmp[1] ?? ''))
break;
if ((strlen($date_in) == 4) || (strlen($date_in) == 6)) {
$date_tmp[0] = substr($date_in, 0, 2);
$date_tmp[1] = substr($date_in, 2 );
}
} while (false);
// $date_tmp is assumed to hold date components ($month and $year in either order)
// continue processing... |
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.
This enables support of the
do-break-while-falsepattern:The error is still reported when the pseudo-loop is not broken with
break.