diff --git a/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php b/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php index 85fc1c2b55..d95c27dee6 100644 --- a/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php +++ b/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php @@ -46,9 +46,10 @@ public function register() public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $prevIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); - if (in_array( - $tokens[$prevIndex]['code'], + $nextIndex = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true); + + if ($nextIndex === false || in_array( + $tokens[$nextIndex]['code'], [ T_OPEN_SHORT_ARRAY, T_ARRAY, @@ -56,6 +57,7 @@ public function process(File $phpcsFile, $stackPtr) T_FALSE, T_NULL, T_LNUMBER, + T_DNUMBER, T_CONSTANT_ENCAPSED_STRING, ], true @@ -64,29 +66,35 @@ public function process(File $phpcsFile, $stackPtr) return; } - if ($tokens[$prevIndex]['code'] === T_CLOSE_SHORT_ARRAY) { - $prevIndex = $tokens[$prevIndex]['bracket_opener']; - } - - $prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prevIndex - 1), null, true); - if ($prevIndex === false) { - return; - } + $skipTokens = array_merge( + [ + T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS, + T_OPEN_PARENTHESIS => T_OPEN_PARENTHESIS, + ], + Tokens::$emptyTokens + ); - if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true) === true) { + $previousIndex = $phpcsFile->findPrevious($skipTokens, ($stackPtr - 1), null, true); + if ($previousIndex === false) { return; } - if ($tokens[$prevIndex]['code'] === T_STRING_CONCAT) { - return; + if (in_array( + $tokens[$previousIndex]['code'], + [ + T_VARIABLE, + T_STRING, + ], + true + ) === false + ) { + $phpcsFile->addError( + 'Usage of Yoda conditions is not allowed. Switch the expression order.', + $stackPtr, + 'DisallowYodaCondition' + ); } - $phpcsFile->addError( - 'Usage of Yoda conditions is not allowed. Switch the expression order.', - $stackPtr, - 'DisallowYodaCondition' - ); - }//end process() diff --git a/src/Standards/Generic/Sniffs/ControlStructures/EnforceYodaConditionsSniff.php b/src/Standards/Generic/Sniffs/ControlStructures/EnforceYodaConditionsSniff.php index b88ef7c854..aea6dfeb3b 100644 --- a/src/Standards/Generic/Sniffs/ControlStructures/EnforceYodaConditionsSniff.php +++ b/src/Standards/Generic/Sniffs/ControlStructures/EnforceYodaConditionsSniff.php @@ -51,6 +51,7 @@ public function process(File $phpcsFile, $stackPtr) T_FALSE, T_NULL, T_LNUMBER, + T_DNUMBER, T_CONSTANT_ENCAPSED_STRING, ], true diff --git a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc index d1f8dab0d4..cb7705f706 100644 --- a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc +++ b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc @@ -5,7 +5,7 @@ $value = ''; if ($value === true) {} if ($value == true) {} if (true === $value) {} -if(true == $value){} +if (true == $value) {} if($value === true){} if($value == true){} @@ -61,7 +61,57 @@ if(array() == $value){} $assigned = $value === 'string'; $assigned = 'string' == $value; -if(($otherValue) === $value){} -if($otherValue === ($value)){} +if(($value) === $otherValue){} +if($value === ($otherValue)){} + if(($value) === true){} -if((true) === $value){} \ No newline at end of file +if((true) === $value){} + +if(($value + 1 + 1) === $value){} +if(($value + $value) === $value){} + +if($value == self::CONSTANT_1){} + +const CONSTANT1 = 1; +if($value === CONSTANT1){} +if(CONSTANT1 === $value){} + +if($value === ($value1 | $value2)){} +if(($value1 | $value2) === $value){} + +// Check with objects +if($object->myVar === $value){} +if($value === $object->myVar){} + +if($object->function() === $value){} +if($value === $object->function()){} + +// Check with functions +if(myFunction() === $value){} +if($value === myFunction()){} + +// check with multiple operations +if($value === true && $value === 1 && $value === null){} +if(($value === true && $value === 1) == ($value === null && $value === new stdClass())){} + +if(true === $value && 1 === $value && null === $value){} +if((true === $value && 1 === $value) == (null === $value && new stdClass() === $value)){} + +// Add comments in the middle +if( + //comment + true + // comment + === + // comment + $value +){} + +if( + //comment + $value + // comment + === + // comment + true +){} \ No newline at end of file diff --git a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php index 605cab3d2e..9146a02a25 100644 --- a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php +++ b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php @@ -44,6 +44,7 @@ public function getErrorList() 57 => 1, 58 => 1, 62 => 1, + 68 => 1, ]; }//end getErrorList()