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

Fixes #174. Insert boilerplate correctly where comment on first line. #175

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
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 {
micaherne marked this conversation as resolved.
Show resolved Hide resolved
$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/>.