Skip to content

Commit

Permalink
ControlStructureSpacingSniff: Fixed false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Mar 22, 2019
1 parent e7875e3 commit 06a7e06
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use const T_GOTO;
use const T_IF;
use const T_OPEN_CURLY_BRACKET;
use const T_OPEN_SHORT_ARRAY;
use const T_OPEN_TAG;
use const T_RETURN;
use const T_SEMICOLON;
Expand Down Expand Up @@ -363,11 +364,15 @@ private function findControlStructureEnd(File $phpcsFile, int $controlStructureP
return TokenHelper::findPreviousExcluding($phpcsFile, T_WHITESPACE, $pointerAfterControlStructureEnd - 1);
}

$nextPointer = TokenHelper::findNext($phpcsFile, [T_SEMICOLON, T_ANON_CLASS, T_CLOSURE], $controlStructurePointer + 1);
$nextPointer = TokenHelper::findNext($phpcsFile, [T_SEMICOLON, T_ANON_CLASS, T_CLOSURE, T_OPEN_SHORT_ARRAY], $controlStructurePointer + 1);
if ($tokens[$nextPointer]['code'] === T_SEMICOLON) {
return $nextPointer;
}

if ($tokens[$nextPointer]['code'] === T_OPEN_SHORT_ARRAY) {
return (int) TokenHelper::findNext($phpcsFile, T_SEMICOLON, $tokens[$nextPointer]['bracket_closer'] + 1);
}

return (int) TokenHelper::findNext($phpcsFile, T_SEMICOLON, $tokens[$nextPointer]['scope_closer'] + 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testDefaultSettingsErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/controlStructureSpacingWithDefaultSettingsErrors.php');

self::assertSame(49, $report->getErrorCount());
self::assertSame(51, $report->getErrorCount());

self::assertSniffError($report, 4, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_CONTROL_STRUCTURE, 'Expected 1 lines before "if", found 2.');
self::assertSniffError($report, 4, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_CONTROL_STRUCTURE, 'Expected 1 lines after "if", found 0.');
Expand Down Expand Up @@ -69,6 +69,8 @@ public function testDefaultSettingsErrors(): void
self::assertSniffError($report, 168, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE, 'Expected 0 lines before "return", found 1.');
self::assertSniffError($report, 173, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE, 'Expected 0 lines before "return", found 1.');
self::assertSniffError($report, 182, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_CONTROL_STRUCTURE, 'Expected 0 lines after "return", found 1.');
self::assertSniffError($report, 199, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_BEFORE_FIRST_CONTROL_STRUCTURE, 'Expected 0 lines before "return", found 1.');
self::assertSniffError($report, 199, ControlStructureSpacingSniff::CODE_INCORRECT_LINES_COUNT_AFTER_LAST_CONTROL_STRUCTURE, 'Expected 0 lines after "return", found 1.');

self::assertAllFixedInFile($report);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,20 @@ public function count()

};
};

function () {
return [
function () {
$a = 1;
$b = 2;

return $a * $b;
},
function () {
$a = 1;
$b = 2;

return $a * $b;
},
];
};
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,22 @@ public function count()
};

};

function () {

return [
function () {
$a = 1;
$b = 2;

return $a * $b;
},
function () {
$a = 1;
$b = 2;

return $a * $b;
},
];

};
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ public function count()
};
};

function () {
return [
function () {
$a = 1;
$b = 2;

return $a * $b;
},
function () {
$a = 1;
$b = 2;

return $a * $b;
},
];
};

if (true) {

}

0 comments on commit 06a7e06

Please sign in to comment.