Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic/JumbledIncrementer: fix non-problematic bug and improve test coverage #385

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function process(File $phpcsFile, $stackPtr)
protected function findIncrementers(array $tokens, array $token)
{
// Skip invalid statement.
if (isset($token['parenthesis_opener']) === false) {
if (isset($token['parenthesis_opener'], $token['parenthesis_closer']) === false) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
jrfnl marked this conversation as resolved.
Show resolved Hide resolved

for ($same = 0; $same < 20; $same++) {
for ($j = 0; $j < 5; $same += 2) {
for ($k = 0; $k > 3; $same++) {

}
}
}

for ($i = 0; $i < 20; $i++) {
for ($j = 0; $j < 5; $j += 2) {
for ($k = 0; $k > 3; $k++) {

}
}
}

for ($i = 0; $i < 20; $i++) {
for ($same = 0; $same < 5; $same += 2) {
for ($k = 0; $k > 3; $same++) {

}
}
}

for (; $i < 10; $i++) {
for ($j = 0;; $j++) {
if ($j > 5) {
break;
}
for (;; $k++) {
if ($k > 5) {
break;
}
}
}
}

for (; $same < 10; $same++) {
for ($j = 0;; $same++) {
if ($j > 5) {
break;
}
for (;; $same++) {
if ($k > 5) {
break;
}
}
}
}

for ($i = 0; $i < 20; $i++) :
for ($j = 0; $j < 5; $j += 2) :
endfor;
endfor;

for ($same = 0; $same < 20; $same++) :
for ($j = 0; $j < 5; $same += 2) :
endfor;
endfor;

// Sniff bails early when there is no incrementor in the third expression of the outer for loop.
for ($same = 0; $same < 10;) {
++$same;
for ($j = 0; $j < 5; $same++) {}
}

for ($i = 1, $same = 0; $i <= 10; $i++, $same++) {
for ($same = 0, $k = 0; $k < 5; $same++, $k++) {}
}

for ($i = 20; $i > 0; $i--) {
for ($j = 5; $j > 0; $j -= 2) {
for ($k = 3; $k > 0; $k--) {}
}
}

for ($same = 20; $same > 0; $same--) {
for ($j = 5; $j > 0; $same -= 2) {
for ($k = 3; $k > 0; $same--) {}
}
}

for ($i = 0; $i < 20; $i++);

for ($same = 0; $same < 20; $same++) {
for ($j = 0; $j < 20; $same++);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Intentional parse error (inner for loop missing closing parenthesis).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.
for ($i = 0; $i < 20; $i++) {
for ($i = 0; $i < 20; $i++
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

// Intentional parse error (missing for conditions).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.
for
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Intentional parse error (inner for loop missing opening parenthesis).
// This should be the only test in this file.
// Testing that the sniff is *not* triggered.
for ($i = 0; $i < 20; $i++) {
for
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,30 @@ 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 file being tested.
*
* @return array<int, int>
*/
public function getWarningList()
public function getWarningList($testFile='')
{
return [
3 => 2,
4 => 1,
20 => 1,
];
switch ($testFile) {
case 'JumbledIncrementerUnitTest.1.inc':
return [
3 => 2,
4 => 1,
20 => 1,
40 => 2,
41 => 1,
58 => 1,
69 => 1,
79 => 2,
80 => 1,
87 => 1,
];

default:
return [];
}

}//end getWarningList()

Expand Down