From 33f38f1d41e898249f55dc7a971e660854cf7c40 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 27 Feb 2023 14:07:26 +0000 Subject: [PATCH 1/6] Ignore whitespace between tags in unreachable test --- package.xml | 1 + .../Sniffs/PHP/NonExecutableCodeSniff.php | 10 ++++++ .../Tests/PHP/NonExecutableCodeUnitTest.3.inc | 31 +++++++++++++++++++ .../Tests/PHP/NonExecutableCodeUnitTest.php | 5 +++ 4 files changed, 47 insertions(+) create mode 100644 src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.3.inc diff --git a/package.xml b/package.xml index 9338b46e79..eff49f7791 100644 --- a/package.xml +++ b/package.xml @@ -1857,6 +1857,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + diff --git a/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php b/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php index 343d9b29e3..dd5df2a858 100644 --- a/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php @@ -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); diff --git a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.3.inc b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.3.inc new file mode 100644 index 0000000000..9433aed0bb --- /dev/null +++ b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.3.inc @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + +
non-executable
+ + + + + + + + +
non-executable
+ + + diff --git a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php index f5f90c0b92..70d8244895 100644 --- a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php +++ b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php @@ -86,6 +86,11 @@ public function getWarningList($testFile='') 54 => 2, ]; break; + case 'NonExecutableCodeUnitTest.3.inc': + return [ + 19 => 1, + 28 => 1, + ]; default: return []; break; From 01410b4a0a1897955022eac38ef421e423883b12 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 8 Jun 2023 17:37:30 +0100 Subject: [PATCH 2/6] Fix false-negative when missing semicolon --- src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php b/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php index dd5df2a858..445b683f7c 100644 --- a/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php @@ -228,6 +228,10 @@ public function process(File $phpcsFile, $stackPtr) if ($tokens[$start]['code'] === T_SEMICOLON) { break; } + + if ($tokens[$start]['code'] === T_CLOSE_TAG) { + break; + } }//end for if (isset($tokens[$start]) === false) { From b0558986e98daba58dede9ce480cd5d9bafe9ee3 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Thu, 8 Jun 2023 17:38:24 +0100 Subject: [PATCH 3/6] Add more tests --- .../Tests/PHP/NonExecutableCodeUnitTest.3.inc | 33 +++++++++++++++++++ .../Tests/PHP/NonExecutableCodeUnitTest.php | 7 ++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.3.inc b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.3.inc index 9433aed0bb..6fe5c16c66 100644 --- a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.3.inc +++ b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.3.inc @@ -12,6 +12,14 @@ + + + + + + + + @@ -20,6 +28,15 @@ + + + + + +
non-executable
+ + + @@ -29,3 +46,19 @@ + + + + + + + + + + + + + + + + diff --git a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php index 70d8244895..06c0d475b1 100644 --- a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php +++ b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php @@ -88,8 +88,11 @@ public function getWarningList($testFile='') break; case 'NonExecutableCodeUnitTest.3.inc': return [ - 19 => 1, - 28 => 1, + 27 => 1, + 36 => 1, + 45 => 1, + 54 => 1, + 62 => 1, ]; default: return []; From 0924523cdbae256cf07eb9dedb90b735c7a706f8 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 12 Jun 2023 17:51:10 +0100 Subject: [PATCH 4/6] Combine 'if' statements, update comment --- .../Squiz/Sniffs/PHP/NonExecutableCodeSniff.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php b/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php index 445b683f7c..1a943b6a79 100644 --- a/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php +++ b/src/Standards/Squiz/Sniffs/PHP/NonExecutableCodeSniff.php @@ -204,8 +204,8 @@ public function process(File $phpcsFile, $stackPtr) $end = ($phpcsFile->numTokens - 1); }//end if - // Find the semicolon that ends this statement, skipping - // nested statements like FOR loops and closures. + // Find the semicolon or closing PHP tag that ends this statement, + // skipping nested statements like FOR loops and closures. for ($start = ($stackPtr + 1); $start < $phpcsFile->numTokens; $start++) { if ($start === $end) { break; @@ -225,11 +225,7 @@ public function process(File $phpcsFile, $stackPtr) continue; } - if ($tokens[$start]['code'] === T_SEMICOLON) { - break; - } - - if ($tokens[$start]['code'] === T_CLOSE_TAG) { + if ($tokens[$start]['code'] === T_SEMICOLON || $tokens[$start]['code'] === T_CLOSE_TAG) { break; } }//end for From 34ba861a8f4e68cbd7995133817d39c29dc955f2 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 12 Jun 2023 18:36:56 +0100 Subject: [PATCH 5/6] Add test case with multiple statements on one line --- .../Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc | 6 ++++++ src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php | 1 + 2 files changed, 7 insertions(+) diff --git a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc index c9bf052f34..2a759ec483 100644 --- a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc +++ b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc @@ -58,4 +58,10 @@ $a = new class { } }; +// Multiple statements are still one line of unreachable code, so should get +// only one complaint from this sniff. (Well, technically two here since there +// are two 'exit()' statements above, so one complaint from each of those. So, +// two here, but not six.) +echo 'one'; echo 'two'; echo 'three'; + interface MyInterface { diff --git a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php index 06c0d475b1..659706ad8f 100644 --- a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php +++ b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php @@ -84,6 +84,7 @@ public function getWarningList($testFile='') 10 => 2, 14 => 1, 54 => 2, + 65 => 2, ]; break; case 'NonExecutableCodeUnitTest.3.inc': From 2067e7c6e7fd6369350000678531ba2107ed0ad2 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Mon, 12 Jun 2023 18:49:16 +0100 Subject: [PATCH 6/6] Add test case with one statement on several lines --- .../Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc | 6 ++++++ src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc index 2a759ec483..9b7a22bcc9 100644 --- a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc +++ b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.2.inc @@ -64,4 +64,10 @@ $a = new class { // two here, but not six.) echo 'one'; echo 'two'; echo 'three'; +// A single statement split across multiple lines. Here we get complaints for +// each line, even though they're all part of one statement. +echo 'one' . 'two' + . 'three' . 'four' + . 'five' . 'six'; + interface MyInterface { diff --git a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php index 659706ad8f..19f0083ed4 100644 --- a/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php +++ b/src/Standards/Squiz/Tests/PHP/NonExecutableCodeUnitTest.php @@ -85,6 +85,9 @@ public function getWarningList($testFile='') 14 => 1, 54 => 2, 65 => 2, + 69 => 2, + 70 => 2, + 71 => 2, ]; break; case 'NonExecutableCodeUnitTest.3.inc':