Skip to content

Commit a7010a8

Browse files
committed
Added missing T_EXIT to LanguageConstructWithParenthesesSniff
1 parent bdecdad commit a7010a8

File tree

5 files changed

+25
-3
lines changed

5 files changed

+25
-3
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/LanguageConstructWithParenthesesSniff.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function register(): array
1818
T_BREAK,
1919
T_CONTINUE,
2020
T_ECHO,
21+
T_EXIT,
2122
T_INCLUDE,
2223
T_INCLUDE_ONCE,
2324
T_PRINT,
@@ -48,11 +49,19 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $languageConstru
4849
return;
4950
}
5051

52+
$containsContentBetweenParentheses = TokenHelper::findNextEffective($phpcsFile, $openParenthesisPointer + 1, $closeParenthesisPointer) !== null;
53+
if ($tokens[$languageConstructPointer]['code'] === T_EXIT && $containsContentBetweenParentheses) {
54+
return;
55+
}
56+
5157
$fix = $phpcsFile->addFixableError(sprintf('Usage of language construct "%s" with parentheses is disallowed.', $tokens[$languageConstructPointer]['content']), $languageConstructPointer, self::CODE_USED_WITH_PARENTHESES);
5258
if ($fix) {
5359
$phpcsFile->fixer->beginChangeset();
54-
$phpcsFile->fixer->replaceToken($openParenthesisPointer, $tokens[$openParenthesisPointer - 1]['code'] === T_WHITESPACE ? '' : ' ');
55-
$phpcsFile->fixer->replaceToken($tokens[$openParenthesisPointer]['parenthesis_closer'], '');
60+
$phpcsFile->fixer->replaceToken($openParenthesisPointer, '');
61+
if ($tokens[$openParenthesisPointer - 1]['code'] !== T_WHITESPACE && $containsContentBetweenParentheses) {
62+
$phpcsFile->fixer->addContent($openParenthesisPointer, ' ');
63+
}
64+
$phpcsFile->fixer->replaceToken($closeParenthesisPointer, '');
5665
$phpcsFile->fixer->endChangeset();
5766
}
5867
}

tests/Sniffs/ControlStructures/LanguageConstructWithParenthesesSniffTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testErrors()
1414
{
1515
$report = $this->checkFile(__DIR__ . '/data/languageConstructWithParenthesesErrors.php', [], [LanguageConstructWithParenthesesSniff::CODE_USED_WITH_PARENTHESES]);
1616

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

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

3133
$this->assertAllFixedInFile($report);
3234
}

tests/Sniffs/ControlStructures/data/languageConstructWithParenthesesErrors.fixed.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ function boo()
3232
} catch (\Throwable $e) {
3333

3434
}
35+
36+
die;
37+
exit;

tests/Sniffs/ControlStructures/data/languageConstructWithParenthesesErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@ function boo()
3232
} catch (\Throwable $e) {
3333

3434
}
35+
36+
die();
37+
exit();

tests/Sniffs/ControlStructures/data/languageConstructWithParenthesesNoErrors.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ function returnConditions(): bool
4747
{
4848
return (null === true) || (null === false);
4949
}
50+
51+
die(0);
52+
die('Error');
53+
exit(0);
54+
exit('Error');

0 commit comments

Comments
 (0)