Skip to content

Commit

Permalink
Added missing T_EXIT to LanguageConstructWithParenthesesSniff
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Aug 14, 2017
1 parent bdecdad commit a7010a8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function register(): array
T_BREAK,
T_CONTINUE,
T_ECHO,
T_EXIT,
T_INCLUDE,
T_INCLUDE_ONCE,
T_PRINT,
Expand Down Expand Up @@ -48,11 +49,19 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $languageConstru
return;
}

$containsContentBetweenParentheses = TokenHelper::findNextEffective($phpcsFile, $openParenthesisPointer + 1, $closeParenthesisPointer) !== null;
if ($tokens[$languageConstructPointer]['code'] === T_EXIT && $containsContentBetweenParentheses) {
return;
}

$fix = $phpcsFile->addFixableError(sprintf('Usage of language construct "%s" with parentheses is disallowed.', $tokens[$languageConstructPointer]['content']), $languageConstructPointer, self::CODE_USED_WITH_PARENTHESES);
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($openParenthesisPointer, $tokens[$openParenthesisPointer - 1]['code'] === T_WHITESPACE ? '' : ' ');
$phpcsFile->fixer->replaceToken($tokens[$openParenthesisPointer]['parenthesis_closer'], '');
$phpcsFile->fixer->replaceToken($openParenthesisPointer, '');
if ($tokens[$openParenthesisPointer - 1]['code'] !== T_WHITESPACE && $containsContentBetweenParentheses) {
$phpcsFile->fixer->addContent($openParenthesisPointer, ' ');
}
$phpcsFile->fixer->replaceToken($closeParenthesisPointer, '');
$phpcsFile->fixer->endChangeset();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function testErrors()
{
$report = $this->checkFile(__DIR__ . '/data/languageConstructWithParenthesesErrors.php', [], [LanguageConstructWithParenthesesSniff::CODE_USED_WITH_PARENTHESES]);

$this->assertSame(11, $report->getErrorCount());
$this->assertSame(13, $report->getErrorCount());

$this->assertSniffError($report, 5, LanguageConstructWithParenthesesSniff::CODE_USED_WITH_PARENTHESES, 'Usage of language construct "continue" with parentheses is disallowed.');
$this->assertSniffError($report, 8, LanguageConstructWithParenthesesSniff::CODE_USED_WITH_PARENTHESES, 'Usage of language construct "break" with parentheses is disallowed.');
Expand All @@ -27,6 +27,8 @@ public function testErrors()
$this->assertSniffError($report, 22, LanguageConstructWithParenthesesSniff::CODE_USED_WITH_PARENTHESES, 'Usage of language construct "return" with parentheses is disallowed.');
$this->assertSniffError($report, 27, LanguageConstructWithParenthesesSniff::CODE_USED_WITH_PARENTHESES, 'Usage of language construct "yield" with parentheses is disallowed.');
$this->assertSniffError($report, 31, LanguageConstructWithParenthesesSniff::CODE_USED_WITH_PARENTHESES, 'Usage of language construct "throw" with parentheses is disallowed.');
$this->assertSniffError($report, 36, LanguageConstructWithParenthesesSniff::CODE_USED_WITH_PARENTHESES, 'Usage of language construct "die" with parentheses is disallowed.');
$this->assertSniffError($report, 37, LanguageConstructWithParenthesesSniff::CODE_USED_WITH_PARENTHESES, 'Usage of language construct "exit" with parentheses is disallowed.');

$this->assertAllFixedInFile($report);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ function boo()
} catch (\Throwable $e) {

}

die;
exit;
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ function boo()
} catch (\Throwable $e) {

}

die();
exit();
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ function returnConditions(): bool
{
return (null === true) || (null === false);
}

die(0);
die('Error');
exit(0);
exit('Error');

0 comments on commit a7010a8

Please sign in to comment.