Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e560258

Browse files
committedMar 20, 2024··
Ignore line length limit in docblocks
Note: This does not apply to inline docs. We should _perhaps_ limit this to things like Behat @Given/When/Then but there are other times when this is not helpful too.
1 parent e6a6fdb commit e560258

File tree

4 files changed

+106
-1
lines changed

4 files changed

+106
-1
lines changed
 

‎moodle/Sniffs/Files/LineLengthSniff.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
namespace MoodleHQ\MoodleCS\moodle\Sniffs\Files;
2626

27+
use MoodleHQ\MoodleCS\moodle\Util\Docblocks;
2728
use PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff as GenericLineLengthSniff;
28-
use PHP_CodeSniffer\Sniffs\Sniff;
2929
use PHP_CodeSniffer\Files\File;
3030

3131
class LineLengthSniff extends GenericLineLengthSniff
@@ -40,6 +40,17 @@ public function process(File $file, $stackptr) {
4040
if (strpos($file->getFilename(), DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR) !== false) {
4141
return;
4242
}
43+
4344
parent::process($file, $stackptr);
4445
}
46+
47+
protected function checkLineLength($phpcsFile, $tokens, $stackPtr) {
48+
// Ignore lines that are part of a docblock.
49+
// We may extend this to only ignore certain tags in the future.
50+
if (Docblocks::getStartOfCurrentDocblock($phpcsFile, $stackPtr) !== null) {
51+
return;
52+
}
53+
54+
parent::checkLineLength($phpcsFile, $tokens, $stackPtr);
55+
}
4556
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
// This file is part of Moodle - https://moodle.org/
4+
//
5+
// Moodle is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Moodle is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
17+
18+
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Commenting;
19+
20+
use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase;
21+
22+
/**
23+
* Test the MissingDocblockSniff sniff.
24+
*
25+
* @copyright 2024 onwards Andrew Lyons <andrew@nicols.co.uk>
26+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
*
28+
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\Commenting\MissingDocblockSniff
29+
*/
30+
class LineLengthSniffTest extends MoodleCSBaseTestCase
31+
{
32+
/**
33+
* @dataProvider fixtureProvider
34+
*/
35+
public function testSniffWithFixtures(
36+
string $fixture,
37+
?string $fixturePath,
38+
array $errors,
39+
array $warnings
40+
): void {
41+
// xdebug_break();
42+
$this->setStandard('moodle');
43+
$this->setSniff('moodle.Files.LineLength');
44+
$this->setFixture(
45+
sprintf("%s/fixtures/LineLength/%s.php", __DIR__, $fixture),
46+
$fixturePath,
47+
);
48+
$this->setErrors($errors);
49+
$this->setWarnings($warnings);
50+
51+
$this->verifyCsResults();
52+
}
53+
54+
public static function fixtureProvider(): array {
55+
$cases = [
56+
[
57+
'fixture' => 'langfile',
58+
'fixturePath' => '/lang/en/assignfeedback_editpdf.php',
59+
'errors' => [],
60+
'warnings' => [],
61+
],
62+
[
63+
'fixture' => 'standard',
64+
'fixturePath' => null,
65+
'errors' => [
66+
13 => 'Line exceeds maximum limit of 180 characters; contains 182 characters',
67+
],
68+
'warnings' => [
69+
70+
],
71+
],
72+
];
73+
return $cases;
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$string['example_of_a_very_long_string_name'] = 'This is an example of a very long string with a vey long name which combined will exceed the maximum lenth of your typical Moodle coding style';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
class Example
4+
{
5+
/**
6+
* This is an example of a very long string within the docblock of a class.
7+
*
8+
* Checks that all actiities in the specified section are hidden. You need to be in the course page. It can be used being logged as a student and as a teacher on editing mode.
9+
*
10+
* @Given /^I change the name of the "(?P<activity_name_string>(?:[^"]|\\")*)" activity name to "(?P<new_name_string>(?:[^"]|\\")*)"$/
11+
*/
12+
public function i_change_names(): void {
13+
// This is also a really stupidly long comment but this one is not allowed to be over long. The reason we accept long docblock strings but not long comments string is because
14+
// docblocks are used as code.
15+
}
16+
}

0 commit comments

Comments
 (0)
Please sign in to comment.