Skip to content

Commit b384372

Browse files
committed
Squiz/NonExecutableCode: bug fix - expressions after PHP 7.0/7.4 null coalesce (equals)
PHP 7.0 introduced the null coalesce operator. PHP 7.4 introduced the null coalesce equals operator. The `T_EXIT` token can be used in combination with those without affecting the code directly following it. See: https://3v4l.org/C218d#veol Fixed now. Includes unit test. Related to 2857
1 parent 2048a01 commit b384372

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function process(File $phpcsFile, $stackPtr)
7373
if ($tokens[$prev]['code'] === T_INLINE_THEN || $tokens[$prev]['code'] === T_INLINE_ELSE) {
7474
return;
7575
}
76+
77+
// Expressions are allowed with PHP 7.0+ null coalesce and PHP 7.4+ null coalesce equals.
78+
if ($tokens[$prev]['code'] === T_COALESCE || $tokens[$prev]['code'] === T_COALESCE_EQUAL) {
79+
return;
80+
}
7681
}
7782

7883
// Check if this token is actually part of a one-line IF or ELSE statement.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,5 +335,12 @@ function exitExpressionsInTernary() {
335335
echo 'still executable';
336336
}
337337

338+
// Inline expressions are allowed with null coalesce and null coalesce equals.
339+
function exitExpressionsWithNullCoalesce() {
340+
$value = $nullableValue ?? exit();
341+
$value ??= die();
342+
echo 'still executable';
343+
}
344+
338345
// Intentional syntax error.
339346
return array_map(

0 commit comments

Comments
 (0)