Skip to content

Commit 26d584c

Browse files
committed
Prevent plugins to check FileExpectedTags.LicenseTagInvalid
A new `license-regex` option has been added to the phpcs command. It allows to specify the regex that will be used when inspecting @license tags. By default, an empty string is used for the option and that makes the Sniff to stop checking. Whoever wants to check for anything can use the new option to configure it.
1 parent 144700a commit 26d584c

File tree

4 files changed

+123
-20
lines changed

4 files changed

+123
-20
lines changed

docs/CLI.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ Run Moodle CodeSniffer standard on a plugin
15921592

15931593
### Usage
15941594

1595-
* `phpcs [-s|--standard STANDARD] [-x|--exclude EXCLUDE] [--max-warnings MAX-WARNINGS] [--test-version TEST-VERSION] [--todo-comment-regex TODO-COMMENT-REGEX] [--] <plugin>`
1595+
* `phpcs [-s|--standard STANDARD] [-x|--exclude EXCLUDE] [--max-warnings MAX-WARNINGS] [--test-version TEST-VERSION] [--todo-comment-regex TODO-COMMENT-REGEX] [--license-regex LICENSE-REGEX] [--] <plugin>`
15961596
* `codechecker`
15971597

15981598
Run Moodle CodeSniffer standard on a plugin
@@ -1659,6 +1659,16 @@ Regex to use to match TODO/@todo comments
16591659
* Is negatable: no
16601660
* Default: `''`
16611661

1662+
#### `--license-regex`
1663+
1664+
Regex to use to match @license tags
1665+
1666+
* Accept value: yes
1667+
* Is value required: yes
1668+
* Is multiple: no
1669+
* Is negatable: no
1670+
* Default: `''`
1671+
16621672
#### `--help|-h`
16631673

16641674
Display help for the given command. When no command is given display help for the list command

src/Command/CodeCheckerCommand.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,43 @@ protected function configure(): void
3838
$this->setName('phpcs')
3939
->setAliases(['codechecker'])
4040
->setDescription('Run Moodle CodeSniffer standard on a plugin')
41-
->addOption('standard', 's', InputOption::VALUE_REQUIRED, 'The name or path of the coding standard to use', 'moodle')
42-
->addOption('exclude', 'x', InputOption::VALUE_REQUIRED, 'Comma separated list of sniff codes to exclude from checking', '')
43-
->addOption('max-warnings', null, InputOption::VALUE_REQUIRED,
44-
'Number of warnings to trigger nonzero exit code - default: -1', -1)
45-
->addOption('test-version', null, InputOption::VALUE_REQUIRED,
46-
'Version or range of version to test with PHPCompatibility', 0)
47-
->addOption('todo-comment-regex', null, InputOption::VALUE_REQUIRED,
48-
'Regex to use to match TODO/@todo comments', '');
41+
->addOption(
42+
'standard',
43+
's',
44+
InputOption::VALUE_REQUIRED,
45+
'The name or path of the coding standard to use',
46+
'moodle'
47+
)->addOption(
48+
'exclude',
49+
'x',
50+
InputOption::VALUE_REQUIRED,
51+
'Comma separated list of sniff codes to exclude from checking',
52+
''
53+
)->addOption(
54+
'max-warnings',
55+
null,
56+
InputOption::VALUE_REQUIRED,
57+
'Number of warnings to trigger nonzero exit code - default: -1',
58+
-1
59+
)->addOption(
60+
'test-version',
61+
null,
62+
InputOption::VALUE_REQUIRED,
63+
'Version or range of version to test with PHPCompatibility',
64+
0
65+
)->addOption(
66+
'todo-comment-regex',
67+
null,
68+
InputOption::VALUE_REQUIRED,
69+
'Regex to use to match TODO/@todo comments',
70+
''
71+
)->addOption(
72+
'license-regex',
73+
null,
74+
InputOption::VALUE_REQUIRED,
75+
'Regex to use to match @license tags',
76+
''
77+
);
4978
}
5079

5180
protected function initialize(InputInterface $input, OutputInterface $output): void
@@ -119,6 +148,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
119148
$todoCommentRegex = $input->getOption('todo-comment-regex');
120149
array_push($cmd, '--runtime-set', 'moodleTodoCommentRegex', $todoCommentRegex);
121150

151+
// Set the regex to use to match @license tags.
152+
// Note that the option defaults to an empty string,
153+
// meaning that no checks will be performed. Configure it
154+
// to a valid regex ('GPL-3.0', 'https:', ...) to enable the checks.
155+
$licenseRegex = $input->getOption('license-regex');
156+
array_push($cmd, '--runtime-set', 'moodleLicenseRegex', $licenseRegex);
157+
122158
// Add the files to process.
123159
foreach ($files as $file) {
124160
$cmd[] = $file;

tests/Command/CodeCheckerCommandTest.php

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,19 @@ abstract private function somefunc() { // To verify PHPCompatibility sniff.
9696
$output = $commandTester->getDisplay();
9797
$this->assertMatchesRegularExpression('/E\.* 10\.* \/ 10 \(100%\)/', $output); // Progress.
9898
$this->assertMatchesRegularExpression('/\/fixable.php/', $output); // File.
99-
$this->assertMatchesRegularExpression('/ 8 ERRORS AND 1 WARNING AFFECTING 8 /', $output); // Summary.
99+
$this->assertMatchesRegularExpression('/ 11 ERRORS AND 1 WARNING AFFECTING 8 /', $output); // Summary.
100100
$this->assertMatchesRegularExpression('/moodle\.Files\.BoilerplateComment\.Wrong/', $output); // Moodle sniff.
101+
$this->assertMatchesRegularExpression('/Expected MOODLE_INTERNAL check/', $output); // Moodle sniff.
101102
$this->assertMatchesRegularExpression('/print_error\(\) has been deprecated/', $output); // Moodle sniff.
103+
$this->assertMatchesRegularExpression('/Usage of ELSEIF not allowed; use ELSE IF/', $output); // Squiz sniff.
102104
$this->assertMatchesRegularExpression('/print_object\(\) is forbidden/', $output); // Moodle sniff.
103-
$this->assertMatchesRegularExpression('/Missing doc comment for class test/', $output); // Moodle sniff.
105+
$this->assertMatchesRegularExpression('/Missing docblock for class test/', $output); // Moodle sniff.
106+
$this->assertMatchesRegularExpression('/Missing @copyright tag/', $output); // Moodle sniff.
107+
$this->assertMatchesRegularExpression('/Missing @license tag/', $output); // Moodle sniff.
108+
$this->assertMatchesRegularExpression('/Missing docblock for function somefunc/', $output); // Moodle sniff.
104109
$this->assertMatchesRegularExpression('/AbstractPrivateMethods\.Found/', $output); // PHPCompatibility sniff.
105-
$this->assertMatchesRegularExpression('/Files\.EndFileNewline\.NotFound/', $output); // End of file.
110+
$this->assertMatchesRegularExpression('/Opening brace must be the last content/', $output); // Generic sniff.
111+
$this->assertMatchesRegularExpression('/Files\.EndFileNewline\.NotFound/', $output); // Generic of file.
106112
$this->assertMatchesRegularExpression('/PHPCBF CAN FIX THE 3 MARKED SNIFF/', $output); // PHPCBF note.
107113
$this->assertMatchesRegularExpression('/Time:.*Memory:/', $output); // Time.
108114

@@ -214,27 +220,38 @@ public function testExecuteWithExclusions()
214220
$commandTester = $this->executeCommand($this->pluginDir);
215221
$output = $commandTester->getDisplay();
216222
$this->assertSame(1, $commandTester->getStatusCode());
217-
$this->assertMatchesRegularExpression('/FOUND 9 ERRORS AND 1 WARNING AFFECTING 1 LINE/', $output);
223+
$this->assertMatchesRegularExpression('/FOUND \d+ ERRORS? AND \d+ WARNINGS? AFFECTING 1 LINE/', $output);
218224

219225
// With exclusions.
220-
$commandTester = $this->executeCommand($this->pluginDir, ['--exclude' => 'moodle.Files.RequireLogin,moodle.Files.BoilerplateComment']);
226+
$commandTester = $this->executeCommand(
227+
$this->pluginDir,
228+
['--exclude' => 'moodle.Files.RequireLogin,' .
229+
'moodle.Files.BoilerplateComment,' .
230+
'moodle.Commenting.MissingDocblock,' .
231+
'moodle.Commenting.FileExpectedTags']
232+
);
221233
$this->assertSame(0, $commandTester->getStatusCode());
222234
}
223235

224236
public function testExecuteWithTodoCommentRegex()
225237
{
226-
// Let's add a file with some comments having links and some without
238+
// Let's add a file with some comments having links and some without.
227239
$content = <<<'EOT'
228240
<?php
229241
// phpcs:disable moodle.Files.BoilerplateComment
230-
// Without any CUSTOM-[0-9]+ reference.
242+
243+
/**
244+
* This is a nice fixture file with some todo tags.
245+
*
246+
* @copyright 2024 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
247+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
248+
* @todo This is also the simplest, but within a phpdoc block
249+
* @todo This is also the simplest, but within a phpdoc block. CUSTOM-123
250+
*/
231251
232252
// TODO: This is the simplest TODO comment.
233-
/** @todo This is also the simplest, but within a phpdoc block */
234-
// With a CUSTOM-[0-9]+ reference.
235253
236254
// TODO: This is the simplest TODO comment. CUSTOM-123.
237-
/** @todo This is also the simplest, but within a phpdoc block. CUSTOM-123 */
238255

239256
EOT;
240257

@@ -247,13 +264,53 @@ public function testExecuteWithTodoCommentRegex()
247264
$this->assertSame(0, $commandTester->getStatusCode());
248265

249266
// With a "CUSTOM-[0-9]+" regex configured.
267+
// TODO: This will start requiring complete regexp soon, see https://github.com/moodlehq/moodle-cs/issues/141
268+
// (with "complete regex" meaning, with delimiters, basically).
250269
$commandTester = $this->executeCommand($this->pluginDir, ['--todo-comment-regex' => 'CUSTOM-[0-9]+']);
251270
$output = $commandTester->getDisplay();
252271
$this->assertSame(0, $commandTester->getStatusCode());
253272
$this->assertMatchesRegularExpression('/FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES/', $output);
254273
$this->assertMatchesRegularExpression('/Missing required "CUSTOM-\[0-9\]\+"/', $output);
255274
}
256275

276+
public function testExecuteWithLicenseRegex()
277+
{
278+
// Let's add a file with some comments having various licenses.
279+
$content = <<<'EOT'
280+
<?php
281+
// phpcs:disable moodle.Files.BoilerplateComment
282+
283+
/**
284+
* This is a nice fixture file with some licenses.
285+
*
286+
* @copyright 2024 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
287+
* @license https://example.org/invented-license IL v3 or later
288+
*/
289+
290+
EOT;
291+
292+
$this->fs->dumpFile($this->pluginDir . '/test_comment_licenses.php', $content);
293+
294+
// Without any regex configured.
295+
$commandTester = $this->executeCommand($this->pluginDir);
296+
$output = $commandTester->getDisplay();
297+
$this->assertSame(0, $commandTester->getStatusCode());
298+
$this->assertMatchesRegularExpression('/\.{10} 10 \/ 10 \(100%\)/', $output);
299+
300+
// With a "~v[345]] or later~" regex configured.
301+
$commandTester = $this->executeCommand($this->pluginDir, ['--license-regex' => '~v[345] or later~']);
302+
$output = $commandTester->getDisplay();
303+
$this->assertSame(0, $commandTester->getStatusCode());
304+
$this->assertMatchesRegularExpression('/\.{10} 10 \/ 10 \(100%\)/', $output);
305+
306+
// With a "GNU GPL" regex configured.
307+
$commandTester = $this->executeCommand($this->pluginDir, ['--license-regex' => '~GNU GPL~']);
308+
$output = $commandTester->getDisplay();
309+
$this->assertSame(0, $commandTester->getStatusCode());
310+
$this->assertMatchesRegularExpression('/FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE/', $output);
311+
$this->assertMatchesRegularExpression('/Value ".+invented-license IL v3 or later" does not match/', $output);
312+
}
313+
257314
public function testExecuteNoFiles()
258315
{
259316
// Just random directory with no PHP files.

tests/Fixture/moodle-local_ci/lib.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @package local_ci
2121
* @copyright Copyright (c) 2015 Blackboard Inc. (http://www.blackboard.com)
22-
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22+
* @license http://some.invented.license.com Not checked License v5 or later
2323
*/
2424

2525
// TODO: This todo comment without any MDL link is good for moodle-plugin-ci

0 commit comments

Comments
 (0)