File tree Expand file tree Collapse file tree 6 files changed +77
-2
lines changed
Expand file tree Collapse file tree 6 files changed +77
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 1+ <?php // Some comment on first line.
2+
3+ class test {
4+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ <?php // phpcs:enable
Original file line number Diff line number Diff line change 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/>.
You can’t perform that action at this time.
0 commit comments