Skip to content

Commit

Permalink
Ignore whitespace between tags in unreachable test
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Mar 3, 2023
1 parent ed8e00d commit ab62641
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
<file baseinstalldir="PHP/CodeSniffer" name="LowercasePHPFunctionsUnitTest.php" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="NonExecutableCodeUnitTest.1.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="NonExecutableCodeUnitTest.2.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="NonExecutableCodeUnitTest.3.inc" role="test" />
<file baseinstalldir="PHP/CodeSniffer" name="NonExecutableCodeUnitTest.php" role="test" />
</dir>
<dir name="Scope">
Expand Down
10 changes: 10 additions & 0 deletions src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ public function process(File $phpcsFile, $stackPtr)
continue;
}

// Skip HTML whitespace.
if ($tokens[$i]['code'] === T_INLINE_HTML && \trim($tokens[$i]['content']) === '') {
continue;
}

// Skip PHP re-open tag (eg, after inline HTML).
if ($tokens[$i]['code'] === T_OPEN_TAG) {
continue;
}

$line = $tokens[$i]['line'];
if ($line > $lastLine) {
$type = substr($tokens[$stackPtr]['type'], 2);
Expand Down
15 changes: 15 additions & 0 deletions src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,21 @@ class TestAlternativeControlStructures {
}
}

// There should be no problem with the following code.
?>

<?php if (true): ?>
<?php foreach ([] as $item): ?>
<?php continue; ?>
<?php endforeach; ?>
<?php endif; ?>

<?php if (true) { ?>
<?php foreach ([] as $item) { ?>
<?php continue; ?>
<?php } ?>
<?php }

$var_after_class_in_global_space = 1;
do_something_else();

Expand Down
31 changes: 31 additions & 0 deletions src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.3.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!-- no problem here -->
<?php if (true): ?>
<?php foreach ([] as $item): ?>
<?php continue; ?>
<?php endforeach; ?>
<?php endif; ?>

<!-- no problem here -->
<?php if (true) { ?>
<?php foreach ([] as $item) { ?>
<?php continue; ?>
<?php } ?>
<?php } ?>

<!-- should detect an error here -->
<?php if (true): ?>
<?php foreach ([] as $item): ?>
<?php continue; ?>
<div>non-executable</div>
<?php endforeach; ?>
<?php endif; ?>

<!-- should detect an error here -->
<?php if (true): ?>
<?php foreach ([] as $item): ?>
<?php continue; ?>

<div>non-executable</div>

<?php endforeach; ?>
<?php endif; ?>
5 changes: 5 additions & 0 deletions src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public function getWarningList($testFile='')
54 => 2,
];
break;
case 'NonExecutableCodeUnitTest.3.inc':
return [
19 => 1,
28 => 1,
];
default:
return [];
break;
Expand Down

0 comments on commit ab62641

Please sign in to comment.