From e4d374359c9fc857106661b10c5e5107f94849ed Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Mon, 22 Apr 2024 15:31:41 -0300 Subject: [PATCH] Generic/DisallowYodaConditions: `??` should not trigger the sniff This commit changes the DisallowYodaConditions sniff so that the null coalescing operator (`??`) does not trigger it. --- .../Sniffs/ControlStructures/DisallowYodaConditionsSniff.php | 5 ++++- .../ControlStructures/DisallowYodaConditionsUnitTest.inc | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php b/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php index d82931cb6c..4a98566587 100644 --- a/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php +++ b/src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php @@ -25,7 +25,10 @@ class DisallowYodaConditionsSniff implements Sniff */ public function register() { - return Tokens::$comparisonTokens; + $tokens = Tokens::$comparisonTokens; + unset($tokens[T_COALESCE]); + + return $tokens; }//end register() diff --git a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc index ce458d8429..9f3c20b970 100644 --- a/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc +++ b/src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc @@ -173,3 +173,5 @@ echo match ($text) { 'foo' => 10 === $y, 10 === $y => 'bar', }; + +1 ?? $nullCoalescingShouldNotTriggerSniff;