Skip to content

Commit

Permalink
Fixes #174. Insert boilerplate correctly if there is a comment on fir…
Browse files Browse the repository at this point in the history
…st line.
  • Loading branch information
micaherne committed Jul 2, 2024
1 parent a0bfb12 commit 904ac82
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
12 changes: 10 additions & 2 deletions moodle/Sniffs/Files/BoilerplateCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,16 @@ private function fullComment(): array

private function insertBoilerplate(File $file, int $stackptr): void
{
$prefix = substr($file->getTokens()[$stackptr]['content'], -1) === "\n" ? '' : "\n";
$file->fixer->addContent($stackptr, $prefix . implode("\n", $this->fullComment()) . "\n");
$token = $file->getTokens()[$stackptr];
$paddedComment = implode("\n", $this->fullComment()) . "\n";

if ($token['code'] === T_OPEN_TAG) {
$replacement = trim($token['content']) . "\n" . $paddedComment;
$file->fixer->replaceToken($stackptr, $replacement);
} else {
$prefix = substr($token['content'], -1) === "\n" ? '' : "\n";
$file->fixer->addContent($stackptr, $prefix . $paddedComment);

Check warning on line 244 in moodle/Sniffs/Files/BoilerplateCommentSniff.php

View check run for this annotation

Codecov / codecov/patch

moodle/Sniffs/Files/BoilerplateCommentSniff.php#L243-L244

Added lines #L243 - L244 were not covered by tests
}
}

private function moveBoilerplate(File $file, int $start, int $target): void
Expand Down
14 changes: 14 additions & 0 deletions moodle/Tests/FilesBoilerPlateCommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,18 @@ public function testMoodleFilesBoilerplateCommentTrailingWhitespaceMissing() {

$this->verifyCsResults();
}

public function testMoodleFilesBoilerplateCommentFirstlineComment() {
$this->setStandard('moodle');
$this->setSniff('moodle.Files.BoilerplateComment');
$this->setFixture(__DIR__ . '/fixtures/files/boilerplatecomment/firstline_comment.php');

$this->setErrors([
1 => 'NoBoilerplateComment',
]);

$this->setWarnings([]);

$this->verifyCsResults();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php // Some comment on first line.

class test {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
// Some comment on first line.

class test {
}

0 comments on commit 904ac82

Please sign in to comment.