diff --git a/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php b/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php index 08877d347..56165449e 100644 --- a/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php +++ b/SlevomatCodingStandard/Sniffs/ControlStructures/AssignmentInConditionSniff.php @@ -42,6 +42,7 @@ public function process(File $phpcsFile, $conditionStartPointer): void { $tokens = $phpcsFile->getTokens(); $token = $tokens[$conditionStartPointer]; + if ($token['code'] === T_DO) { $whilePointer = TokenHelper::findNext($phpcsFile, T_WHILE, $token['scope_closer'] + 1); $whileToken = $tokens[$whilePointer]; @@ -53,6 +54,14 @@ public function process(File $phpcsFile, $conditionStartPointer): void $parenthesisCloser = $token['parenthesis_closer']; $type = $token['code'] === T_IF ? 'if' : 'elseif'; } + + if ( + $parenthesisOpener === null + || $parenthesisCloser === null + ) { + return; + } + $this->processCondition($phpcsFile, $parenthesisOpener, $parenthesisCloser, $type); } diff --git a/tests/Sniffs/ControlStructures/AssignmentInConditionSniffTest.php b/tests/Sniffs/ControlStructures/AssignmentInConditionSniffTest.php index 1a37cb4a2..5aea3b7d6 100644 --- a/tests/Sniffs/ControlStructures/AssignmentInConditionSniffTest.php +++ b/tests/Sniffs/ControlStructures/AssignmentInConditionSniffTest.php @@ -49,4 +49,10 @@ public function testErrorsWithIgnoreAssignmentsInsideFunctionCalls(): void } } + public function testLiveCoding(): void + { + $resultFile = self::checkFile(__DIR__ . '/data/assignmentsInConditionsLiveCoding.php'); + self::assertNoSniffErrorInFile($resultFile); + } + } diff --git a/tests/Sniffs/ControlStructures/data/assignmentsInConditionsLiveCoding.php b/tests/Sniffs/ControlStructures/data/assignmentsInConditionsLiveCoding.php new file mode 100644 index 000000000..003dc8e5b --- /dev/null +++ b/tests/Sniffs/ControlStructures/data/assignmentsInConditionsLiveCoding.php @@ -0,0 +1,5 @@ += 99.00 + +// Live coding/parse error. +// This must be the last test in the file. +if ($a = 1