Skip to content

Commit 2fb2d7d

Browse files
rodrigoprimojrfnl
authored andcommitted
Generic/DisallowYodaConditions: improve code coverage
Includes removing two unreachable conditions The first condition is unreachable because PHPCS will never call DisallowYodaConditionsSniff::process() if there is no non-empty token before a comparison token. Even if the file contains a syntax error, there must be at least a PHP opening tag before the comparison token for the method to be called. The second condition is unreachable because at this point in the code there will always be at least two non-empty tokens before the comparison token. If there is only one, it must be a PHP opening tag and, in this case, the method will bail before reaching the code that sets the `$prevIndex` variable. This commit also documents why a certain line is uncovered by tests - and cannot be covered, nor can it be removed as external sniffs may call `DisallowYodaConditionsSniff::isArrayStatic()` directly.
1 parent 6a75209 commit 2fb2d7d

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ public function process(File $phpcsFile, $stackPtr)
5757
T_CONSTANT_ENCAPSED_STRING,
5858
];
5959

60-
if ($previousIndex === false
61-
|| in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false
62-
) {
60+
if (in_array($tokens[$previousIndex]['code'], $relevantTokens, true) === false) {
6361
return;
6462
}
6563

@@ -71,9 +69,6 @@ public function process(File $phpcsFile, $stackPtr)
7169
}
7270

7371
$prevIndex = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($previousIndex - 1), null, true);
74-
if ($prevIndex === false) {
75-
return;
76-
}
7772

7873
if (in_array($tokens[$prevIndex]['code'], Tokens::$arithmeticTokens, true) === true) {
7974
return;
@@ -150,6 +145,7 @@ public function isArrayStatic(File $phpcsFile, $arrayToken)
150145
$start = $tokens[$arrayToken]['parenthesis_opener'];
151146
$end = $tokens[$arrayToken]['parenthesis_closer'];
152147
} else {
148+
// Shouldn't be possible but may happen if external sniffs are using this method.
153149
return true;
154150
}
155151

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,13 @@ echo match ($text) {
175175
};
176176

177177
1 ?? $nullCoalescingShouldNotTriggerSniff;
178+
179+
1 + 2 === $sniffBailsArithmeticToken;
180+
181+
'string' . 'concat' === $sniffBailsStringConcatToken;
182+
183+
1 != $value;
184+
1 <> $value;
185+
1 >= $value;
186+
1 <= $value;
187+
1 <=> $value;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public function getErrorList()
6767
167 => 1,
6868
173 => 1,
6969
174 => 1,
70+
183 => 1,
71+
184 => 1,
72+
185 => 1,
73+
186 => 1,
74+
187 => 1,
7075
];
7176

7277
}//end getErrorList()

0 commit comments

Comments
 (0)