Skip to content

Commit d1cb540

Browse files
committed
Fixed some errors in LanguageConstructWithParenthesesSniff
1 parent a904fae commit d1cb540

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/LanguageConstructWithParenthesesSniff.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,23 @@ public function register(): array
3737
public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $languageConstructPointer)
3838
{
3939
$tokens = $phpcsFile->getTokens();
40+
$openParenthesisPointer = TokenHelper::findNextEffective($phpcsFile, $languageConstructPointer + 1);
41+
if ($tokens[$openParenthesisPointer]['code'] !== T_OPEN_PARENTHESIS) {
42+
return;
43+
}
44+
45+
$closeParenthesisPointer = $tokens[$openParenthesisPointer]['parenthesis_closer'];
46+
$afterCloseParenthesisPointer = TokenHelper::findNextEffective($phpcsFile, $closeParenthesisPointer + 1);
47+
if ($tokens[$afterCloseParenthesisPointer]['code'] !== T_SEMICOLON) {
48+
return;
49+
}
4050

41-
$nextTokenPointer = TokenHelper::findNextEffective($phpcsFile, $languageConstructPointer + 1);
42-
if ($tokens[$nextTokenPointer]['code'] === T_OPEN_PARENTHESIS) {
43-
$fix = $phpcsFile->addFixableError(sprintf('Usage of language construct "%s" with parentheses is disallowed.', $tokens[$languageConstructPointer]['content']), $languageConstructPointer, self::CODE_USED_WITH_PARENTHESES);
44-
if ($fix) {
45-
$phpcsFile->fixer->beginChangeset();
46-
$phpcsFile->fixer->replaceToken($nextTokenPointer, $tokens[$nextTokenPointer - 1]['code'] === T_WHITESPACE ? '' : ' ');
47-
$phpcsFile->fixer->replaceToken($tokens[$nextTokenPointer]['parenthesis_closer'], '');
48-
$phpcsFile->fixer->endChangeset();
49-
}
51+
$fix = $phpcsFile->addFixableError(sprintf('Usage of language construct "%s" with parentheses is disallowed.', $tokens[$languageConstructPointer]['content']), $languageConstructPointer, self::CODE_USED_WITH_PARENTHESES);
52+
if ($fix) {
53+
$phpcsFile->fixer->beginChangeset();
54+
$phpcsFile->fixer->replaceToken($openParenthesisPointer, $tokens[$openParenthesisPointer - 1]['code'] === T_WHITESPACE ? '' : ' ');
55+
$phpcsFile->fixer->replaceToken($tokens[$openParenthesisPointer]['parenthesis_closer'], '');
56+
$phpcsFile->fixer->endChangeset();
5057
}
5158
}
5259

tests/Sniffs/ControlStructures/data/languageConstructWithParenthesesNoErrors.php

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

3434
}
35+
36+
function returnCalculationOnStart(): bool
37+
{
38+
return (10 + 5) === 15;
39+
}
40+
41+
function returnCalculationOnEnd(): bool
42+
{
43+
return 15 === (10 + 5);
44+
}
45+
46+
function returnConditions(): bool
47+
{
48+
return (null === true) || (null === false);
49+
}

0 commit comments

Comments
 (0)