PHP 8.3 | Tokenizer/PHP + Generic/UnconditionalIfStatement: bug fix #3890
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.
Description
When analyzing code during live coding, a situation could occur where the open parenthesis for an
if
control structure exists, but not a closing parenthesis. In that case,$end
would be set to-1
(via--null
) and thefor
loop would never even run, but a warning would still be thrown for the code.This warning is a false positive and should be avoided.
On top of that, PHP 8.3 deprecates the use of the increment/decrement operators on non-float/non-integer values, which can cause the below warning when running the sniff on PHP 8.3.
Both the incorrect sniff warning, as well as the PHP 8.3 deprecation warning will now be avoided via the defensive coding added to the sniff.
When debugging the issue on the command line with the added test and using the following command
phpcs -ps ./generic/tests/codeanalysis/UnconditionalIfStatementUnitTest.inc --standard=Generic --sniffs=Generic.CodeAnalysis.UnconditionalIfStatement
, I was not receiving any output at all.Digging deeper, I discovered two (loosely related) bugs in the Tokenizer/PHP class, which were the cause of that and which could be seen when running the tests:
This bug is fixed by fixing the incorrect comparison on line 3073.
... which then exposed the next bug:
... which is fixed by adding some extra defensive coding in the condition.
All three fixes are covered by the one test which was added.
Suggested changelog entry
true
/false
/null
is at the end of a file during live coding.Related issues/external references
N/A
Types of changes