Skip to content

Commit e70599f

Browse files
committed
Squiz/NonExecutableCode: handle FQN exit/die
This commit fixes the sniff to handle "fully qualified exit/die" the same as unqualified exit/die. Includes tests.
1 parent ddeab84 commit e70599f

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public function process(File $phpcsFile, $stackPtr)
6767

6868
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
6969

70+
// Allow for PHP 8.4+ fully qualified use of exit/die.
71+
if ($tokens[$stackPtr]['code'] === \T_EXIT && $tokens[$prev]['code'] === \T_NS_SEPARATOR) {
72+
$prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($prev - 1), null, true);
73+
}
74+
7075
// Tokens which can be used in inline expressions need special handling.
7176
if (isset($this->expressionTokens[$tokens[$stackPtr]['code']]) === true) {
7277
// If this token is preceded by a logical operator, it only relates to one line

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ function parseError2() {
311311
// All logical operators are allowed with inline expressions (but this was not correctly handled by the sniff).
312312
function exitExpressionsWithLogicalOperators() {
313313
$condition = false;
314-
$condition || exit();
314+
$condition || \exit();
315315
$condition or die();
316316

317317
$condition = true;
@@ -327,7 +327,7 @@ function exitExpressionsWithLogicalOperators() {
327327
function exitExpressionsInTernary() {
328328
$value = $myValue ? $myValue : exit();
329329
$value = $myValue ?: exit();
330-
$value = $var == 'foo' ? 'bar' : die( 'world' );
330+
$value = $var == 'foo' ? 'bar' : \die( 'world' );
331331

332332
$value = (!$myValue ) ? exit() : $myValue;
333333
$value = $var != 'foo' ? die( 'world' ) : 'bar';
@@ -418,3 +418,17 @@ $closure = function ()
418418
echo 'foo';
419419
return; // This return should be flagged as not required.
420420
};
421+
422+
function fqnExitWithCodeAfter() {
423+
do_something();
424+
exit();
425+
do_something_else();
426+
}
427+
428+
function fqnExitInControlStructureWithCodeAfter() {
429+
if(do_something()) {
430+
die();
431+
do_something_else();
432+
}
433+
do_something_else();
434+
}

src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public function getWarningList($testFile='')
9090
406 => 1,
9191
412 => 1,
9292
419 => 1,
93+
425 => 1,
94+
431 => 1,
9395
];
9496

9597
case 'NonExecutableCodeUnitTest.2.inc':

0 commit comments

Comments
 (0)