Skip to content

Commit ea5fe67

Browse files
committed
EarlyExitSniff: fixed fixing of negative logical conditions
1 parent 4b52495 commit ea5fe67

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

SlevomatCodingStandard/Sniffs/ControlStructures/EarlyExitSniff.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ private function getNegativeCondition(\PHP_CodeSniffer\Files\File $phpcsFile, in
230230

231231
$condition = TokenHelper::getContent($phpcsFile, $startPointer, $endPointer);
232232

233+
$booleanNotPointer = TokenHelper::findNextEffective($phpcsFile, $startPointer);
234+
if ($tokens[$booleanNotPointer]['code'] === T_BOOLEAN_NOT) {
235+
$negativeCondition = preg_replace('~^!\\s*~', '', $condition);
236+
return preg_replace('~^\(\\s*(.+?)\\s*\)\\s*$~', '\\1', $negativeCondition);
237+
}
238+
233239
$booleanPointers = TokenHelper::findNextAll($phpcsFile, \PHP_CodeSniffer\Util\Tokens::$booleanOperators, $startPointer, $endPointer + 1);
234240
if (count($booleanPointers) > 0) {
235241
$uniqueBooleanOperators = array_unique(array_map(function (int $pointer) use ($tokens) {
@@ -274,12 +280,6 @@ private function getNegativeCondition(\PHP_CodeSniffer\Files\File $phpcsFile, in
274280
return $negativeCondition;
275281
}
276282

277-
$booleanNotPointer = TokenHelper::findNextEffective($phpcsFile, $startPointer);
278-
if ($tokens[$booleanNotPointer]['code'] === T_BOOLEAN_NOT) {
279-
$negativeCondition = preg_replace('~^!\\s*~', '', $condition);
280-
return preg_replace('~^\(\\s*(.+?)\\s*\)\\s*~', '\\1', $negativeCondition);
281-
}
282-
283283
if (TokenHelper::findNext($phpcsFile, [T_INSTANCEOF, T_BITWISE_AND], $startPointer, $endPointer + 1) !== null) {
284284
return sprintf('!(%s)', $condition);
285285
}

tests/Sniffs/ControlStructures/EarlyExitSniffTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public function testErrors(): void
1515
{
1616
$report = self::checkFile(__DIR__ . '/data/earlyExitErrors.php');
1717

18-
self::assertSame(29, $report->getErrorCount());
18+
self::assertSame(31, $report->getErrorCount());
1919

2020
foreach ([6, 15, 24, 33, 42, 50, 58, 66, 74, 82, 90, 98, 108, 149, 157, 165, 191, 199, 207] as $line) {
2121
self::assertSniffError($report, $line, EarlyExitSniff::CODE_EARLY_EXIT_NOT_USED, 'Use early exit instead of else.');
2222
}
2323

24-
foreach ([115, 122, 129, 135, 141, 213, 222, 229] as $line) {
24+
foreach ([115, 122, 129, 135, 141, 213, 222, 229, 235, 241] as $line) {
2525
self::assertSniffError($report, $line, EarlyExitSniff::CODE_EARLY_EXIT_NOT_USED, 'Use early exit to reduce code nesting.');
2626
}
2727

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,19 @@ function binaryAndCondition() {
248248

249249
$this->eventManager->dispatchEvent($eventName, $event);
250250
}
251+
252+
function negativeLogicalAndCondition() {
253+
if (isset($parameterMappings[$parameterName]) && array_key_exists($parameterName, $parameterMappings)) {
254+
return;
255+
}
256+
257+
unset($parameters[$key]);
258+
}
259+
260+
function negativeLogicalOrCondition() {
261+
if (isset($parameterMappings[$parameterName]) || array_key_exists($parameterName, $parameterMappings)) {
262+
return;
263+
}
264+
265+
unset($parameters[$key]);
266+
}

tests/Sniffs/ControlStructures/data/earlyExitErrors.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,15 @@ function binaryAndCondition() {
230230
$this->eventManager->dispatchEvent($eventName, $event);
231231
}
232232
}
233+
234+
function negativeLogicalAndCondition() {
235+
if (! (isset($parameterMappings[$parameterName]) && array_key_exists($parameterName, $parameterMappings))) {
236+
unset($parameters[$key]);
237+
}
238+
}
239+
240+
function negativeLogicalOrCondition() {
241+
if (! (isset($parameterMappings[$parameterName]) || array_key_exists($parameterName, $parameterMappings))) {
242+
unset($parameters[$key]);
243+
}
244+
}

0 commit comments

Comments
 (0)