diff --git a/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php b/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php index 4a98566587..3dc7795b37 100644 --- a/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php +++ b/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php @@ -57,9 +57,7 @@ public function process(File $phpcsFile, $stackPtr) T_CONSTANT_ENCAPSED_STRING, ]; - if ($previousIndex === false - || in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false - ) { + if (in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false) { return; } @@ -71,9 +69,6 @@ public function process(File $phpcsFile, $stackPtr) } $prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($previousIndex - 1), null, true); - if ($prevIndex === false) { - return; - } if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true) === true) { return; @@ -85,16 +80,15 @@ public function process(File $phpcsFile, $stackPtr) // Is it a parenthesis. if ($tokens[$previousIndex]['code'] === T_CLOSE_PARENTHESIS) { - // Check what exists inside the parenthesis. - $closeParenthesisIndex = $phpcsFile->findPrevious( + $beforeOpeningParenthesisIndex = $phpcsFile->findPrevious( Tokens::$emptyTokens, ($tokens[$previousIndex]['parenthesis_opener'] - 1), null, true ); - if ($closeParenthesisIndex === false || $tokens[$closeParenthesisIndex]['code'] !== T_ARRAY) { - if ($tokens[$closeParenthesisIndex]['code'] === T_STRING) { + if ($beforeOpeningParenthesisIndex === false || $tokens[$beforeOpeningParenthesisIndex]['code'] !== T_ARRAY) { + if ($tokens[$beforeOpeningParenthesisIndex]['code'] === T_STRING) { return; } @@ -110,14 +104,14 @@ public function process(File $phpcsFile, $stackPtr) return; } - // If there is nothing inside the parenthesis, it it not a Yoda. + // If there is nothing inside the parenthesis, it is not a Yoda condition. $opener = $tokens[$previousIndex]['parenthesis_opener']; $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($previousIndex - 1), ($opener + 1), true); if ($prev === false) { return; } - } else if ($tokens[$closeParenthesisIndex]['code'] === T_ARRAY - && $this->isArrayStatic($phpcsFile, $closeParenthesisIndex) === false + } else if ($tokens[$beforeOpeningParenthesisIndex]['code'] === T_ARRAY + && $this->isArrayStatic($phpcsFile, $beforeOpeningParenthesisIndex) === false ) { return; }//end if @@ -144,7 +138,6 @@ public function isArrayStatic(File $phpcsFile, $arrayToken) { $tokens = $phpcsFile->getTokens(); - $arrayEnd = null; if ($tokens[$arrayToken]['code'] === T_OPEN_SHORT_ARRAY) { $start = $arrayToken; $end = $tokens[$arrayToken]['bracket_closer']; @@ -152,6 +145,7 @@ public function isArrayStatic(File $phpcsFile, $arrayToken) $start = $tokens[$arrayToken]['parenthesis_opener']; $end = $tokens[$arrayToken]['parenthesis_closer']; } else { + // Shouldn't be possible but may happen if external sniffs are using this method. return true; } diff --git a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc index 9f3c20b970..27053c4d65 100644 --- a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc +++ b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc @@ -175,3 +175,13 @@ echo match ($text) { }; 1 ?? $nullCoalescingShouldNotTriggerSniff; + +1 + 2 === $sniffBailsArithmeticToken; + +'string' . 'concat' === $sniffBailsStringConcatToken; + +1 != $value; +1 <> $value; +1 >= $value; +1 <= $value; +1 <=> $value; diff --git a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php index 52fc370a68..64a487d512 100644 --- a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php +++ b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php @@ -67,6 +67,11 @@ public function getErrorList() 167 => 1, 173 => 1, 174 => 1, + 183 => 1, + 184 => 1, + 185 => 1, + 186 => 1, + 187 => 1, ]; }//end getErrorList()