Skip to content

Commit

Permalink
Avoid PHP error with BoilerplateComment and empty files
Browse files Browse the repository at this point in the history
Covered with a specific new test.

Fixes moodlehq#97
  • Loading branch information
stronk7 committed Feb 14, 2024
1 parent 42ab278 commit d2dc105
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion moodle/Sniffs/Files/BoilerplateCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function process(File $file, $stackptr) {
// Note that the opening PHP tag includes one newline.
$numnewlines = 0;
for ($i = $stackptr + 1; $i <= $stackptr + 5; ++$i) {
if ($tokens[$i]['code'] == T_WHITESPACE && $tokens[$i]['content'] == "\n") {
if (isset($tokens[$i]) && $tokens[$i]['code'] == T_WHITESPACE && $tokens[$i]['content'] == "\n") {
$numnewlines++;
} else {
break;
Expand Down
13 changes: 13 additions & 0 deletions moodle/Tests/FilesBoilerPlateCommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ public function test_moodle_files_boilerplatecomment_short() {
$this->verify_cs_results();
}

public function test_moodle_files_boilerplatecomment_short_empty() {
$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
$this->set_fixture(__DIR__ . '/fixtures/files/boilerplatecomment/short_empty.php');

$this->set_errors([
1 => 'FileTooShort',
]);
$this->set_warnings([]);

$this->verify_cs_results();
}

public function test_moodle_files_boilerplatecomment_wrongline() {
$this->set_standard('moodle');
$this->set_sniff('moodle.Files.BoilerplateComment');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php

0 comments on commit d2dc105

Please sign in to comment.