Skip to content

Commit c17d7c7

Browse files
committed
Squiz/NonExecutableCode: bug fix - expressions in PHP 7.4 arrow functions
PHP 7.4 introduced arrow functions. The `T_EXIT` token can be used in those without affecting the code following it. See: https://3v4l.org/gSrt4#veol Fixed now. Includes unit test.
1 parent b384372 commit c17d7c7

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ public function process(File $phpcsFile, $stackPtr)
7878
if ($tokens[$prev]['code'] === T_COALESCE || $tokens[$prev]['code'] === T_COALESCE_EQUAL) {
7979
return;
8080
}
81-
}
81+
82+
// Expressions are allowed in arrow functions.
83+
if ($tokens[$prev]['code'] === T_FN_ARROW) {
84+
return;
85+
}
86+
}//end if
8287

8388
// Check if this token is actually part of a one-line IF or ELSE statement.
8489
for ($i = ($stackPtr - 1); $i > 0; $i--) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,5 +342,11 @@ function exitExpressionsWithNullCoalesce() {
342342
echo 'still executable';
343343
}
344344

345+
// Inline expressions are allowed in arrow functions.
346+
function exitExpressionsInArrowFunction() {
347+
$callable = fn() => die();
348+
echo 'still executable';
349+
}
350+
345351
// Intentional syntax error.
346352
return array_map(

0 commit comments

Comments
 (0)