Skip to content

Commit ac2ed16

Browse files
committed
ConditionHelper: Fixed getNegativeCondition()
1 parent 2387855 commit ac2ed16

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

SlevomatCodingStandard/Helpers/ConditionHelper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ private static function getNegativeConditionPart(File $phpcsFile, int $condition
113113
$booleanPointers = TokenHelper::findNextAll($phpcsFile, Tokens::$booleanOperators, $conditionBoundaryStartPointer, $conditionBoundaryEndPointer + 1);
114114

115115
if ($tokens[$pointerAfterConditionStart]['code'] === T_BOOLEAN_NOT) {
116-
if ($nested && count($booleanPointers) > 0) {
117-
return self::removeBooleanNot($condition);
118-
}
119-
120116
$pointerAfterBooleanNot = TokenHelper::findNextEffective($phpcsFile, $pointerAfterConditionStart + 1);
121117
if ($tokens[$pointerAfterBooleanNot]['code'] === T_OPEN_PARENTHESIS) {
118+
if ($nested && count($booleanPointers) > 0) {
119+
return self::removeBooleanNot($condition);
120+
}
121+
122122
$pointerAfterParenthesisCloser = TokenHelper::findNextEffective($phpcsFile, $tokens[$pointerAfterBooleanNot]['parenthesis_closer'] + 1, $conditionBoundaryEndPointer + 1);
123123
if ($pointerAfterParenthesisCloser === null || $pointerAfterParenthesisCloser === $conditionBoundaryEndPointer) {
124124
return TokenHelper::getContent($phpcsFile, $pointerAfterBooleanNot + 1, $tokens[$pointerAfterBooleanNot]['parenthesis_closer'] - 1);

tests/Sniffs/ControlStructures/UselessIfConditionWithReturnSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testErrors(): void
1818
{
1919
$report = self::checkFile(__DIR__ . '/data/uselessIfConditionWithReturnErrors.php');
2020

21-
self::assertSame(8, $report->getErrorCount());
21+
self::assertSame(9, $report->getErrorCount());
2222

2323
self::assertSniffError($report, 4, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
2424
self::assertSniffError($report, 12, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
@@ -28,6 +28,7 @@ public function testErrors(): void
2828
self::assertSniffError($report, 44, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
2929
self::assertSniffError($report, 52, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
3030
self::assertSniffError($report, 61, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
31+
self::assertSniffError($report, 70, UselessIfConditionWithReturnSniff::CODE_USELESS_IF_CONDITION);
3132

3233
self::assertAllFixedInFile($report);
3334
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,11 @@ function () {
4141
return false;
4242
}
4343
};
44+
45+
function () {
46+
return (
47+
!empty($day['from']) || empty($day['to'])
48+
) && (
49+
empty($day['from']) || !empty($day['to'])
50+
);
51+
};

tests/Sniffs/ControlStructures/data/uselessIfConditionWithReturnErrors.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ function () {
6565
return false;
6666
}
6767
};
68+
69+
function () {
70+
if ((
71+
empty($day['from']) && !empty($day['to'])
72+
) || (
73+
!empty($day['from']) && empty($day['to'])
74+
)) {
75+
return false;
76+
}
77+
78+
return true;
79+
};

0 commit comments

Comments
 (0)