Skip to content

Commit e0e03f0

Browse files
authored
Merge pull request #235 from rodrigoprimo/fix-deprecated-error-for-loop-with-test-function-call
Generic/ForLoopWithTestFunctionCall: fix potential PHP 8.3+ deprecation notice
2 parents 7a7cd6b + 817a1ef commit e0e03f0

6 files changed

+132
-22
lines changed

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

+2-2
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

@@ -84,7 +84,7 @@ public function process(File $phpcsFile, $stackPtr)
8484
continue;
8585
}
8686

87-
// Find next non empty token, if it is a open curly brace we have a
87+
// Find next non empty token, if it is a open parenthesis we have a
8888
// function call.
8989
$index = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true);
9090

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
$a = array(1, 2, 3, 4);
4+
for ($i = 0; $i < count($a); $i++) {
5+
$a[$i] *= $i;
6+
}
7+
8+
for ($i = 0, $c = sizeof($a); $i < $c; ++$i) {
9+
$a[$i] *= $i;
10+
}
11+
12+
$it = new ArrayIterator($a);
13+
for ($it->rewind(); $it->valid(); $it->next()) {
14+
echo $it->current();
15+
}
16+
17+
for ($i = 0; MyClass::staticMethod($value); $i++) {
18+
echo $i;
19+
}
20+
21+
for ($i = 0; $countFunction($value); $i++) {
22+
echo $i;
23+
}
24+
25+
$a = array(1, 2, 3, 4);
26+
for ($i = 0; $i < count($a); $i++):
27+
$a[$i] *= $i;
28+
endfor;
29+
30+
for ($i = 0, $c = sizeof($a); $i < $c; ++$i):
31+
$a[$i] *= $i;
32+
endfor;
33+
34+
$it = new ArrayIterator($a);
35+
for ($it->rewind(); $it->valid(); $it->next()):
36+
echo $it->current();
37+
endfor;
38+
39+
for ($i = 0; MyClass::staticMethod($value); $i++) :
40+
echo $i;
41+
endfor;
42+
43+
for ($i = 0; $countFunction($value); $i++):
44+
echo $i;
45+
endfor;
46+
47+
for ($i = 0; (new MyClass)->method(); $i++) {
48+
}
49+
50+
for (; $i < 10; ++$i) {}
51+
52+
for (; count($a); ++$i) {}
53+
54+
for ($i = 0;; ++$i) {}
55+
56+
for ($i = 0; $i < 10;) {}
57+
58+
for ($i = 0; count($a);) {}
59+
60+
for (;; $i++) {}
61+
62+
for ($i = 0;;) {}
63+
64+
for (;;) {}
65+
66+
for ($i = 0; (new MyClass)->method(); $i++):
67+
endfor;
68+
69+
for (; $i < 10; ++$i) :
70+
endfor;
71+
72+
for (; count($a); ++$i) :
73+
endfor;
74+
75+
for ($i = 0;; ++$i) :
76+
endfor;
77+
78+
for ($i = 0; $i < 10;) :
79+
endfor;
80+
81+
for ($i = 0; count($a);) :
82+
endfor;
83+
84+
for (;; $i++) :
85+
endfor;
86+
87+
for ($i = 0;;) :
88+
endfor;
89+
90+
for (;;) :
91+
endfor;
92+
93+
for ($i = 0; $i < 10; $i = increment($i)) {}
94+
95+
for ($i = initialValue(); $i < 10; $i = increment($i)) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
// Intentional parse error (missing open parenthesis). Testing that the sniff is *not* triggered
4+
// in this case.
5+
for
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++

src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.inc

-15
This file was deleted.

src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.php

+24-5
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,33 @@ public function getErrorList()
4141
* The key of the array should represent the line number and the value
4242
* should represent the number of warnings that should occur on that line.
4343
*
44+
* @param string $testFile The name of the test file being tested.
45+
*
4446
* @return array<int, int>
4547
*/
46-
public function getWarningList()
48+
public function getWarningList($testFile='')
4749
{
48-
return [
49-
4 => 1,
50-
13 => 1,
51-
];
50+
switch ($testFile) {
51+
case 'ForLoopWithTestFunctionCallUnitTest.1.inc':
52+
return [
53+
4 => 1,
54+
13 => 1,
55+
17 => 1,
56+
21 => 1,
57+
26 => 1,
58+
35 => 1,
59+
39 => 1,
60+
43 => 1,
61+
47 => 1,
62+
52 => 1,
63+
58 => 1,
64+
66 => 1,
65+
72 => 1,
66+
81 => 1,
67+
];
68+
default:
69+
return [];
70+
}//end switch
5271

5372
}//end getWarningList()
5473

0 commit comments

Comments
 (0)