Skip to content

Commit

Permalink
Merge pull request #175 from micaherne/174-boilerplate-with-firstline…
Browse files Browse the repository at this point in the history
…-comment

Fixes #174. Insert boilerplate correctly where comment on first line.
  • Loading branch information
stronk7 authored Jul 4, 2024
2 parents a0bfb12 + 8965b35 commit f7668d9
Show file tree
Hide file tree
Showing 6 changed files with 77 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);
}
}

private function moveBoilerplate(File $file, int $start, int $target): void
Expand Down
28 changes: 28 additions & 0 deletions moodle/Tests/FilesBoilerPlateCommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,32 @@ 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();
}

public function testMoodleFilesBoilerplateCommentWithPhpcsTag() {
$this->setStandard('moodle');
$this->setSniff('moodle.Files.BoilerplateComment');
$this->setFixture(__DIR__ . '/fixtures/files/boilerplatecomment/with_phpcs_tag.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 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php // phpcs:enable
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php // phpcs:enable
// 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/>.

0 comments on commit f7668d9

Please sign in to comment.