Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow extra consecutive header lines after the official boilerplate #169

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
The format of this change log follows the advice given at [Keep a CHANGELOG](https://keepachangelog.com).

## [Unreleased]
### Fixed
- Fixed a recent regression by allowing to the `moodle.Files.BoilerplateComment` sniff to contain "extra" consecutive comment lines immediately after the official boilerplate ends.

## [v3.4.8] - 2024-06-14
### Added
Expand Down
17 changes: 16 additions & 1 deletion moodle/Sniffs/Files/BoilerplateCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,22 @@ public function process(File $phpcsFile, $stackPtr): void
return;
}

$tokenptr++;
// Let's jump over all the extra (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.
break;
}
if ($tokens[$nextComment]['line'] !== ($tokens[$lastComment]['line'] + 1)) {
// Stop looking for comments, the lines are not consecutive.
break;
}
$lastComment = $nextComment;
}
$tokenptr = $lastComment + 1; // Move to the last found comment + 1.

$nextnonwhitespace = $phpcsFile->findNext(T_WHITESPACE, $tokenptr, null, true);

Expand Down
8 changes: 8 additions & 0 deletions moodle/Tests/FilesBoilerPlateCommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ public function testMoodleFilesBoilerplateCommentOk() {
$this->setWarnings([]);

$this->verifyCsResults();

// Finally, try with another comments block after the boilerplate.
$this->setFixture(__DIR__ . '/fixtures/files/boilerplatecomment/ok3.php');

$this->setErrors([]);
$this->setWarnings([]);

$this->verifyCsResults();
}

public function testMoodleFilesBoilerplateCommentNoPHP() {
Expand Down
4 changes: 4 additions & 0 deletions moodle/Tests/fixtures/files/boilerplatecomment/ok2.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
//
// 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 { }
23 changes: 23 additions & 0 deletions moodle/Tests/fixtures/files/boilerplatecomment/ok3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php // phpcs:disable moodle.PHPUnit.TestCaseNames.Missing
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
//
// 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.

// But this is already considered another comments block, valid but another,
// so we don't require the blank line here (we have required it above).
class someclass { }