Skip to content

Commit 4c094ed

Browse files
committed
Squiz/NonExecutableCode: bug fix - allow for all logic operators
The sniff, as it was, only allowed for `or` and `||` logical operators before a termination expression, not for `and`, `&&` or `xor`, while PHP allows these too. See: https://3v4l.org/OWS4n#veol Fixed now. Includes unit tests.
1 parent 7fc2e87 commit 4c094ed

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function process(File $phpcsFile, $stackPtr)
6363

6464
// Tokens which can be used in inline expressions need special handling.
6565
if (isset($this->expressionTokens[$tokens[$stackPtr]['code']]) === true) {
66-
// If this token is preceded with an "or", it only relates to one line
66+
// If this token is preceded by a logical operator, it only relates to one line
6767
// and should be ignored. For example: fopen() or die().
68-
if ($tokens[$prev]['code'] === T_LOGICAL_OR || $tokens[$prev]['code'] === T_BOOLEAN_OR) {
68+
if (isset(Tokens::$booleanOperators[$tokens[$prev]['code']]) === true) {
6969
return;
7070
}
7171
}

src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.1.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,20 @@ function parseError2() {
308308
echo 'unreachable';
309309
}
310310

311+
// All logical operators are allowed with inline expressions (but this was not correctly handled by the sniff).
312+
function exitExpressionsWithLogicalOperators() {
313+
$condition = false;
314+
$condition || exit();
315+
$condition or die();
316+
317+
$condition = true;
318+
$condition && die();
319+
$condition and exit;
320+
321+
$condition xor die();
322+
323+
echo 'still executable as exit, in all of the above cases, is used as part of an expression';
324+
}
325+
311326
// Intentional syntax error.
312327
return array_map(

0 commit comments

Comments
 (0)