Skip to content

Commit 6bd0af6

Browse files
committed
Squiz/NonExecutableCode: bug fix - false positive with PHP close tag after case/default
The `Squiz.PHP.NonExecutableCode` sniff would flag PHP open/close tags and indentation whitespace in inline HTML as (non-)executable code, while these tokens should be disregarded for the purposes of this sniff. Fixed now. Includes tests.
1 parent 6d61496 commit 6bd0af6

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ public function process(File $phpcsFile, int $stackPtr)
145145
if ($next !== false) {
146146
$lastLine = $tokens[$end]['line'];
147147
for ($i = ($stackPtr + 1); $i < $next; $i++) {
148-
if (isset(Tokens::EMPTY_TOKENS[$tokens[$i]['code']]) === true) {
148+
if (isset(Tokens::EMPTY_TOKENS[$tokens[$i]['code']]) === true
149+
|| $tokens[$i]['code'] === T_OPEN_TAG
150+
|| $tokens[$i]['code'] === T_CLOSE_TAG
151+
|| ($tokens[$i]['code'] === T_INLINE_HTML && trim($tokens[$i]['content']) === '')
152+
) {
149153
continue;
150154
}
151155

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,29 @@
6262
<?= 'unreachable - with semicolon'; ?>
6363
<?php endforeach; ?>
6464
<?php endif; ?>
65+
66+
<!-- No problem here. -->
67+
<!-- Mind: the case/default statements all miss an explicit semicolon. This is perfectly valid PHP though. -->
68+
<?php switch(true): ?>
69+
<?php case 'foo' ?>
70+
<div>executable</div>
71+
<?php break; ?>
72+
<?php case 'bar' ?>
73+
<?php return ?>
74+
<?php default ?>
75+
<div>executable</div>
76+
<?php goto LABEL; ?>
77+
<?php endswitch ?>
78+
79+
<!-- Should detect errors here. -->
80+
<?php switch(true): ?>
81+
<?php case 'foo' ?>
82+
<?php break; ?>
83+
<div>non-executable</div>
84+
<?php case 'bar' ?>
85+
<?php return; ?>
86+
<div>non-executable</div>
87+
<?php default ?>
88+
<?php goto LABEL ?>
89+
<div>non-executable</div>
90+
<?php endswitch ?>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public function getWarningList($testFile = '')
115115
45 => 1,
116116
54 => 1,
117117
62 => 1,
118+
83 => 1,
119+
86 => 1,
120+
89 => 1,
118121
];
119122
default:
120123
return [];

0 commit comments

Comments
 (0)