Skip to content

Commit df4784c

Browse files
authored
Merge pull request #1206 from PHPCSStandards/feature/1201-tokenizer-php-fqn-exit-die-true-false-null
Tokenizer/PHP: improved tokenization of fully qualified exit/die/true/false/null
2 parents 3756edd + e70599f commit df4784c

36 files changed

+1197
-35
lines changed

src/Standards/Generic/Sniffs/CodeAnalysis/UnconditionalIfStatementSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function process(File $phpcsFile, $stackPtr)
7575
for (; $next <= $end; ++$next) {
7676
$code = $tokens[$next]['code'];
7777

78-
if (isset(Tokens::$emptyTokens[$code]) === true) {
78+
if (isset(Tokens::$emptyTokens[$code]) === true || $code === T_NS_SEPARATOR) {
7979
continue;
8080
} else if ($code !== T_TRUE && $code !== T_FALSE) {
8181
$goodCondition = true;

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/CodeAnalysis/UnconditionalIfStatementUnitTest.1.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ if (true) {
1111
if (file_exists(__FILE__) === true) {
1212

1313
}
14+
15+
// Check handling of case and FQN state.
16+
if (\true) {
17+
} else if (\FALSE) {
18+
}

src/Standards/Generic/Tests/CodeAnalysis/UnconditionalIfStatementUnitTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ public function getWarningList($testFile='')
5050
switch ($testFile) {
5151
case 'UnconditionalIfStatementUnitTest.1.inc':
5252
return [
53-
3 => 1,
54-
5 => 1,
55-
7 => 1,
53+
3 => 1,
54+
5 => 1,
55+
7 => 1,
56+
16 => 1,
57+
17 => 1,
5658
];
5759

5860
default:

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()

src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.1.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,7 @@ abstract class SkipOverPHP84AbstractProperties {
177177
abstract MyType|TRUE $propA {get;}
178178
protected abstract NULL|MyClass $propB {set;}
179179
}
180+
181+
$a = \NULL;
182+
$a = \falSe;
183+
$a = \True;

src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.1.inc.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,7 @@ abstract class SkipOverPHP84AbstractProperties {
177177
abstract MyType|TRUE $propA {get;}
178178
protected abstract NULL|MyClass $propB {set;}
179179
}
180+
181+
$a = \null;
182+
$a = \false;
183+
$a = \true;

src/Standards/Generic/Tests/PHP/LowerCaseConstantUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public function getErrorList($testFile='')
7070
169 => 1,
7171
171 => 1,
7272
173 => 1,
73+
181 => 1,
74+
182 => 1,
75+
183 => 1,
7376
];
7477

7578
case 'LowerCaseConstantUnitTest.js':

src/Standards/Generic/Tests/PHP/UpperCaseConstantUnitTest.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,7 @@ abstract class SkipOverPHP84AbstractProperties {
118118
abstract MyType|true $propA {get;}
119119
protected abstract null|MyClass $propB {set;}
120120
}
121+
122+
$a = \null;
123+
$a = \falSe;
124+
$a = \True;

0 commit comments

Comments
 (0)