Skip to content

Commit 837e602

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/ForLoopWithTestFunctionCall: fix E_DEPRECATED error
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/ForLoopWithTestFunctionCall.php:69 ``` 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. This would happen because since there is no closing parenthesis `$end` is set to null, and $next <= $end always evaluates to false (https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/84acf4e56f110db8e75cb9a575c5727df637643c/src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php#L73). 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.
1 parent 6e61c85 commit 837e602

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function process(File $phpcsFile, $stackPtr)
6161
$token = $tokens[$stackPtr];
6262

6363
// Skip invalid statement.
64-
if (isset($token['parenthesis_opener']) === false) {
64+
if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
6565
return;
6666
}
6767

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
// Similar to issue PHPCSStandards/PHP_CodeSniffer#226
4+
// Intentional parse error (missing close parenthesis). Testing that the sniff is *not* triggered
5+
// in this case and that no PHP 8.3+ deprecation notice is thrown.
6+
for ($i = 0; $i < count($a); $i++

0 commit comments

Comments
 (0)