Skip to content

Commit 2e70c5f

Browse files
committed
Generic/UnconditionalIfStatement: handle FQN true/false/null
This sniff checks for `if`/`elseif` which only contain "true" or "false", i.e. don't actually compare to anything. This commit fixes the sniff to still function correctly when it encounters "fully qualified true/false". Includes tests.
1 parent 1cd9224 commit 2e70c5f

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
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/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:

0 commit comments

Comments
 (0)