Skip to content

Commit

Permalink
Generic/ForLoopShouldBeWhileLoop: fix E_DEPRECATED error
Browse files Browse the repository at this point in the history
This commit fixes an issue in the sniff that could result in the following E_DEPRECATED
error when running PHP 8.3:

```
Decrement on type null has no effect, this will change in the next major version of PHP

src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php:65
```

This sniff relies on finding the position of the open and closing
parentheses for a given `for` loop. However, the problem was that there was
no defensive code for cases when the closing parenthesis is missing. The
sniff would still work when running PHP >= 8.2 but on PHP 8.3 it would
throw the deprecated message above.

The issue was fixed by bailing early if the closing parenthesis is
missing. A test with a `for` loop without the closing parenthesis was added.
  • Loading branch information
rodrigoprimo committed Jan 4, 2024
1 parent 9c4b38c commit 8ad4ea5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function process(File $phpcsFile, $stackPtr)
$token = $tokens[$stackPtr];

// Skip invalid statement.
if (isset($token['parenthesis_opener']) === false) {
if (isset($token['parenthesis_opener']) === false || isset($token['parenthesis_closer']) === false) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// Intentional parse error. Testing that the sniff is *not* triggered in this case.
for ($i = 0; $i < 10; $i++

0 comments on commit 8ad4ea5

Please sign in to comment.