Skip to content

Commit cdac3b1

Browse files
committed
Generic/DisallowYodaConditions: handle FQN true/false/null
This commit fixes the sniff to handle "fully qualified true/false" the same as unqualified true/false. Includes tests.
1 parent 2e70c5f commit cdac3b1

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

src/Standards/Generic/Sniffs/ControlStructures/DisallowYodaConditionsSniff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ public function isArrayStatic(File $phpcsFile, $arrayToken)
164164
T_COMMA => T_COMMA,
165165
T_TRUE => T_TRUE,
166166
T_FALSE => T_FALSE,
167+
T_NULL => T_NULL,
168+
T_NS_SEPARATOR => T_NS_SEPARATOR,
167169
];
168170

169171
for ($i = ($start + 1); $i < $end; $i++) {

src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.inc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ if ($value == true) {}
77
if (true === $value) {}
88
if (true == $value) {}
99

10-
if($value === true){}
11-
if($value == true){}
10+
if($value === false){}
11+
if($value == false){}
1212
if(false === $value){}
1313
if(!false == $value || true !== $value){}
1414

@@ -139,8 +139,8 @@ if (is_array($val)
139139
&& array('foo', 'bar') === array($foo, $bar)
140140
&& ['foo', 'bar'] === [$foo, $bar]
141141
&& array('foo' => true, 'bar' => false) === array(getContents())
142-
&& ['foo' => true, 'bar' => false] === array(getContents())
143-
&& array(getContents()) === ['foo' => true, 'bar' => false]
142+
&& ['foo' => true, 'bar' => \false, 'baz' => null] === array(getContents())
143+
&& array(getContents()) === ['foo' => \true, 'bar' => false, 'baz' => \null]
144144
) {
145145
}
146146

@@ -185,3 +185,14 @@ echo match ($text) {
185185
1 >= $value;
186186
1 <= $value;
187187
1 <=> $value;
188+
189+
// Handle FQN true/false/null the same as plain true/false/null.
190+
if ($value === \true) {}
191+
if (\true === $value) {}
192+
193+
if($value == \FALSE){}
194+
if(\FALSE === $value){}
195+
if(!\false == $value || true !== $value){}
196+
197+
if($value === \Null){}
198+
if(\Null == $value){}

src/Standards/Generic/Tests/ControlStructures/DisallowYodaConditionsUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function getErrorList()
7272
185 => 1,
7373
186 => 1,
7474
187 => 1,
75+
191 => 1,
76+
194 => 1,
77+
195 => 2,
78+
198 => 1,
7579
];
7680

7781
}//end getErrorList()

0 commit comments

Comments
 (0)