diff --git a/src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php b/src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php index 45b4f7306f..0374a8f757 100644 --- a/src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php +++ b/src/Standards/Generic/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php @@ -61,7 +61,7 @@ public function process(File $phpcsFile, $stackPtr) $token = $tokens[$stackPtr]; // Skip invalid statement. - if (isset($token['parenthesis_opener']) === false) { + if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) { return; } @@ -84,7 +84,7 @@ public function process(File $phpcsFile, $stackPtr) continue; } - // Find next non empty token, if it is a open curly brace we have a + // Find next non empty token, if it is a open parenthesis we have a // function call. $index = $phpcsFile->findNext(Tokens::$emptyTokens, ($next + 1), null, true); diff --git a/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.1.inc b/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.1.inc new file mode 100644 index 0000000000..31f1a64199 --- /dev/null +++ b/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.1.inc @@ -0,0 +1,95 @@ +rewind(); $it->valid(); $it->next()) { + echo $it->current(); +} + +for ($i = 0; MyClass::staticMethod($value); $i++) { + echo $i; +} + +for ($i = 0; $countFunction($value); $i++) { + echo $i; +} + +$a = array(1, 2, 3, 4); +for ($i = 0; $i < count($a); $i++): + $a[$i] *= $i; +endfor; + +for ($i = 0, $c = sizeof($a); $i < $c; ++$i): + $a[$i] *= $i; +endfor; + +$it = new ArrayIterator($a); +for ($it->rewind(); $it->valid(); $it->next()): + echo $it->current(); +endfor; + +for ($i = 0; MyClass::staticMethod($value); $i++) : + echo $i; +endfor; + +for ($i = 0; $countFunction($value); $i++): + echo $i; +endfor; + +for ($i = 0; (new MyClass)->method(); $i++) { +} + +for (; $i < 10; ++$i) {} + +for (; count($a); ++$i) {} + +for ($i = 0;; ++$i) {} + +for ($i = 0; $i < 10;) {} + +for ($i = 0; count($a);) {} + +for (;; $i++) {} + +for ($i = 0;;) {} + +for (;;) {} + +for ($i = 0; (new MyClass)->method(); $i++): +endfor; + +for (; $i < 10; ++$i) : +endfor; + +for (; count($a); ++$i) : +endfor; + +for ($i = 0;; ++$i) : +endfor; + +for ($i = 0; $i < 10;) : +endfor; + +for ($i = 0; count($a);) : +endfor; + +for (;; $i++) : +endfor; + +for ($i = 0;;) : +endfor; + +for (;;) : +endfor; + +for ($i = 0; $i < 10; $i = increment($i)) {} + +for ($i = initialValue(); $i < 10; $i = increment($i)) {} diff --git a/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.2.inc b/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.2.inc new file mode 100644 index 0000000000..54ef0a522b --- /dev/null +++ b/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.2.inc @@ -0,0 +1,5 @@ +rewind(); $it->valid(); $it->next()) { - echo $it->current(); -} \ No newline at end of file diff --git a/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.php b/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.php index a82dcfeb27..04490c91f9 100644 --- a/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.php +++ b/src/Standards/Generic/Tests/CodeAnalysis/ForLoopWithTestFunctionCallUnitTest.php @@ -41,14 +41,33 @@ public function getErrorList() * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * + * @param string $testFile The name of the test file being tested. + * * @return array */ - public function getWarningList() + public function getWarningList($testFile='') { - return [ - 4 => 1, - 13 => 1, - ]; + switch ($testFile) { + case 'ForLoopWithTestFunctionCallUnitTest.1.inc': + return [ + 4 => 1, + 13 => 1, + 17 => 1, + 21 => 1, + 26 => 1, + 35 => 1, + 39 => 1, + 43 => 1, + 47 => 1, + 52 => 1, + 58 => 1, + 66 => 1, + 72 => 1, + 81 => 1, + ]; + default: + return []; + }//end switch }//end getWarningList()