Skip to content

Commit f7668d9

Browse files
authored
Merge pull request #175 from micaherne/174-boilerplate-with-firstline-comment
Fixes #174. Insert boilerplate correctly where comment on first line.
2 parents a0bfb12 + 8965b35 commit f7668d9

File tree

6 files changed

+77
-2
lines changed

6 files changed

+77
-2
lines changed

moodle/Sniffs/Files/BoilerplateCommentSniff.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,16 @@ private function fullComment(): array
233233

234234
private function insertBoilerplate(File $file, int $stackptr): void
235235
{
236-
$prefix = substr($file->getTokens()[$stackptr]['content'], -1) === "\n" ? '' : "\n";
237-
$file->fixer->addContent($stackptr, $prefix . implode("\n", $this->fullComment()) . "\n");
236+
$token = $file->getTokens()[$stackptr];
237+
$paddedComment = implode("\n", $this->fullComment()) . "\n";
238+
239+
if ($token['code'] === T_OPEN_TAG) {
240+
$replacement = trim($token['content']) . "\n" . $paddedComment;
241+
$file->fixer->replaceToken($stackptr, $replacement);
242+
} else {
243+
$prefix = substr($token['content'], -1) === "\n" ? '' : "\n";
244+
$file->fixer->addContent($stackptr, $prefix . $paddedComment);
245+
}
238246
}
239247

240248
private function moveBoilerplate(File $file, int $start, int $target): void

moodle/Tests/FilesBoilerPlateCommentTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,32 @@ public function testMoodleFilesBoilerplateCommentTrailingWhitespaceMissing() {
212212

213213
$this->verifyCsResults();
214214
}
215+
216+
public function testMoodleFilesBoilerplateCommentFirstlineComment() {
217+
$this->setStandard('moodle');
218+
$this->setSniff('moodle.Files.BoilerplateComment');
219+
$this->setFixture(__DIR__ . '/fixtures/files/boilerplatecomment/firstline_comment.php');
220+
221+
$this->setErrors([
222+
1 => 'NoBoilerplateComment',
223+
]);
224+
225+
$this->setWarnings([]);
226+
227+
$this->verifyCsResults();
228+
}
229+
230+
public function testMoodleFilesBoilerplateCommentWithPhpcsTag() {
231+
$this->setStandard('moodle');
232+
$this->setSniff('moodle.Files.BoilerplateComment');
233+
$this->setFixture(__DIR__ . '/fixtures/files/boilerplatecomment/with_phpcs_tag.php');
234+
235+
$this->setErrors([
236+
1 => 'NoBoilerplateComment',
237+
]);
238+
239+
$this->setWarnings([]);
240+
241+
$this->verifyCsResults();
242+
}
215243
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php // Some comment on first line.
2+
3+
class test {
4+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
16+
// Some comment on first line.
17+
18+
class test {
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php // phpcs:enable
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // phpcs:enable
2+
// This file is part of Moodle - https://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

0 commit comments

Comments
 (0)