From 3f4dc46a50581b3434891d4471e08cfa6c9ddd67 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 17 Jun 2024 12:13:35 +0200 Subject: [PATCH] Allow extra consecutive header lines after the official boilerplate Once the official boilerplate checks have been performed, allow the "block" to be expanded with any extra information. As far as the block is a number of consecutive comment lines, exclusively separated by \n, that's allowed. Note that any spacing different from \n or any non consecutive comment line will stop the sequence. --- .../Sniffs/Files/BoilerplateCommentSniff.php | 19 ++++++++++++++++++- .../fixtures/files/boilerplatecomment/ok2.php | 4 ++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/moodle/Sniffs/Files/BoilerplateCommentSniff.php b/moodle/Sniffs/Files/BoilerplateCommentSniff.php index eb5f987..329ffde 100644 --- a/moodle/Sniffs/Files/BoilerplateCommentSniff.php +++ b/moodle/Sniffs/Files/BoilerplateCommentSniff.php @@ -166,7 +166,24 @@ public function process(File $phpcsFile, $stackPtr): void return; } - $tokenptr++; + // Let's jump over all the (allowed) consecutive comments to find the first non-comment token. + $lastComment = $tokenptr; + $nextComment = $tokenptr; + while (($nextComment = $phpcsFile->findNext(T_COMMENT, ($nextComment + 1), null, false)) !== false) { + // Only \n is allowed as spacing since the previous comment line. + if (strpos($tokens[$nextComment - 1]['content'], "\n") === false) { + // Stop looking for consecutive comments, some spacing broke the sequence. + $nextComment = $lastComment; // Previous was the last one. + break; + } + if ($tokens[$nextComment]['line'] !== ($tokens[$lastComment]['line'] + 1)) { + // Stop looking for consecutive comments, the lines are not. + break; + } + error_log("Found comment on line " . $tokens[$nextComment]['line']); + $lastComment = $nextComment; + } + $tokenptr = $lastComment + 1; // Move to the last found comment + 1. $nextnonwhitespace = $phpcsFile->findNext(T_WHITESPACE, $tokenptr, null, true); diff --git a/moodle/Tests/fixtures/files/boilerplatecomment/ok2.php b/moodle/Tests/fixtures/files/boilerplatecomment/ok2.php index 61f4f1b..49f799c 100644 --- a/moodle/Tests/fixtures/files/boilerplatecomment/ok2.php +++ b/moodle/Tests/fixtures/files/boilerplatecomment/ok2.php @@ -13,5 +13,9 @@ // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +// +// Note that extra lines after the official boilerplate are allowed +// to put any other information. We only will require the blank line +// after the whole boilerplate ends. class someclass { }