Skip to content

Commit 79c6925

Browse files
committed
Implement --exclude option in phpcs command
1 parent f783423 commit 79c6925

File tree

4 files changed

+49
-22
lines changed

4 files changed

+49
-22
lines changed

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).
1010

1111
## [Unreleased]
12+
### Added
13+
- Added support for the `--exclude` option to the `phpcs` command.
14+
1215
## [4.2.0] - 2023-11-30
1316
### Added
1417
- Added support for the `--tags` and `--name` options to the `behat` command.

docs/CLI.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,16 @@ Version or range of version to test with PHPCompatibility
16191619
* Is negatable: no
16201620
* Default: `0`
16211621

1622+
#### `--exclude|-x`
1623+
1624+
Comma separated list of sniff codes to exclude from checking
1625+
1626+
* Accept value: yes
1627+
* Is value required: yes
1628+
* Is multiple: no
1629+
* Is negatable: no
1630+
* Default: `''`
1631+
16221632
#### `--help|-h`
16231633

16241634
Display help for the given command. When no command is given display help for the list command
@@ -2365,4 +2375,4 @@ Do not ask any interactive question
23652375
* Is value required: no
23662376
* Is multiple: no
23672377
* Is negatable: no
2368-
* Default: `false`
2378+
* Default: `false`

src/Command/CodeCheckerCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ protected function configure(): void
3939
->setAliases(['codechecker'])
4040
->setDescription('Run Moodle CodeSniffer standard on a plugin')
4141
->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', '')
4243
->addOption('max-warnings', null, InputOption::VALUE_REQUIRED,
4344
'Number of warnings to trigger nonzero exit code - default: -1', -1)
4445
->addOption('test-version', null, InputOption::VALUE_REQUIRED,
@@ -77,13 +78,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7778
}
7879
// @codeCoverageIgnoreEnd
7980

80-
$cmd = array_merge($basicCMD, [
81+
$exclude = $input->getOption('exclude');
82+
$cmd = array_merge($basicCMD, [
8183
'--standard=' . ($input->getOption('standard') ?: 'moodle'),
8284
'--extensions=php',
8385
'-p',
8486
'-w',
8587
'-s',
8688
'--no-cache',
89+
empty($exclude) ? '' : ('--exclude=' . $exclude),
8790
$output->isDecorated() ? '--colors' : '--no-colors',
8891
'--report-full',
8992
'--report-width=132',

tests/Command/CodeCheckerCommandTest.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected function setUp(): void
3434
$this->fs->dumpFile($this->pluginDir . '/.moodle-plugin-ci.yml', Yaml::dump($config));
3535
}
3636

37-
protected function executeCommand($pluginDir = null, $maxWarnings = -1, $testVersion = null): CommandTester
37+
protected function executeCommand($pluginDir = null, array $options = []): CommandTester
3838
{
3939
if ($pluginDir === null) {
4040
$pluginDir = $this->pluginDir;
@@ -46,14 +46,7 @@ protected function executeCommand($pluginDir = null, $maxWarnings = -1, $testVer
4646
$application = new Application();
4747
$application->add($command);
4848

49-
$options = ['plugin' => $pluginDir];
50-
if ($maxWarnings >= 0) {
51-
$options['--max-warnings'] = $maxWarnings;
52-
}
53-
54-
if (null !== $testVersion) {
55-
$options['--test-version'] = $testVersion;
56-
}
49+
$options = array_merge(['plugin' => $pluginDir], $options);
5750

5851
$commandTester = new CommandTester($application->find('codechecker'));
5952
$commandTester->execute($options);
@@ -133,19 +126,19 @@ public function testExecuteWithWarningsAndThreshold()
133126
$this->assertSame(0, $commandTester->getStatusCode());
134127

135128
// Allowing 0 warning, it fails.
136-
$commandTester = $this->executeCommand($this->pluginDir, 0);
129+
$commandTester = $this->executeCommand($this->pluginDir, ['--max-warnings' => 0]);
137130
$this->assertSame(1, $commandTester->getStatusCode());
138131

139132
// Allowing 1 warning, it fails.
140-
$commandTester = $this->executeCommand($this->pluginDir, 1);
133+
$commandTester = $this->executeCommand($this->pluginDir, ['--max-warnings' => 1]);
141134
$this->assertSame(1, $commandTester->getStatusCode());
142135

143136
// Allowing 2 warnings, it passes.
144-
$commandTester = $this->executeCommand($this->pluginDir, 2);
137+
$commandTester = $this->executeCommand($this->pluginDir, ['--max-warnings' => 2]);
145138
$this->assertSame(0, $commandTester->getStatusCode());
146139

147140
// Allowing 3 warnings, it passes.
148-
$commandTester = $this->executeCommand($this->pluginDir, 3);
141+
$commandTester = $this->executeCommand($this->pluginDir, ['--max-warnings' => 3]);
149142
$this->assertSame(0, $commandTester->getStatusCode());
150143
}
151144

@@ -167,48 +160,66 @@ public function testExecuteWithTestVersion()
167160
$this->fs->dumpFile($this->pluginDir . '/test_versions.php', $content);
168161

169162
// By default, without specify test-version, only reports deprecation warnings and returns 0.
170-
$commandTester = $this->executeCommand($this->pluginDir, -1, null);
163+
$commandTester = $this->executeCommand($this->pluginDir);
171164
$output = $commandTester->getDisplay();
172165
$this->assertSame(0, $commandTester->getStatusCode());
173166
$this->assertMatchesRegularExpression('/FOUND 0 ERRORS AND 4 WARNINGS AFFECTING 4 LINES/', $output);
174167

175168
// With test-version 7.4, reports 3 new errors and <= 7.4 specific warnings and returns 1.
176-
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4');
169+
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '7.4']);
177170
$output = $commandTester->getDisplay();
178171
$this->assertSame(1, $commandTester->getStatusCode());
179172
$this->assertMatchesRegularExpression('/FOUND 3 ERRORS AND 1 WARNING AFFECTING 4 LINES/', $output);
180173

181174
// With test-version 8.0, reports 2 new errors and <= 8.0 specific warnings and returns 1.
182-
$commandTester = $this->executeCommand($this->pluginDir, -1, '8.0');
175+
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '8.0']);
183176
$output = $commandTester->getDisplay();
184177
$this->assertSame(1, $commandTester->getStatusCode());
185178
$this->assertMatchesRegularExpression('/FOUND 2 ERRORS AND 2 WARNINGS AFFECTING 4 LINES/', $output);
186179

187180
// With test-version 8.1, reports 1 new errors and <= 8.1 specific warnings and returns 0.
188-
$commandTester = $this->executeCommand($this->pluginDir, -1, '8.1');
181+
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '8.1']);
189182
$output = $commandTester->getDisplay();
190183
$this->assertSame(1, $commandTester->getStatusCode());
191184
$this->assertMatchesRegularExpression('/FOUND 1 ERROR AND 3 WARNINGS AFFECTING 4 LINES/', $output);
192185

193186
// With test-version 7.4-8.0, reports 3 new errors and <= 8.0 specific warnings and returns 1.
194-
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4-8.0');
187+
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '7.4-8.0']);
195188
$output = $commandTester->getDisplay();
196189
$this->assertSame(1, $commandTester->getStatusCode());
197190
$this->assertMatchesRegularExpression('/FOUND 3 ERRORS AND 2 WARNINGS AFFECTING 5 LINES/', $output);
198191

199192
// With test-version 7.4-8.1, reports 3 new errors and <= 8.1 specific warnings and returns 1.
200-
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4-8.1');
193+
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '7.4-8.1']);
201194
$output = $commandTester->getDisplay();
202195
$this->assertSame(1, $commandTester->getStatusCode());
203196
$this->assertMatchesRegularExpression('/FOUND 3 ERRORS AND 3 WARNINGS AFFECTING 6 LINES/', $output);
204197

205198
// With test-version 7.4- (open range), reports 3 new errors and <= 8.2 specific warnings and returns 1.
206-
$commandTester = $this->executeCommand($this->pluginDir, -1, '7.4-');
199+
$commandTester = $this->executeCommand($this->pluginDir, ['--test-version' => '7.4-']);
207200
$output = $commandTester->getDisplay();
208201
$this->assertSame(1, $commandTester->getStatusCode());
209202
$this->assertMatchesRegularExpression('/FOUND 3 ERRORS AND 4 WARNINGS AFFECTING 7 LINES/', $output);
210203
}
211204

205+
public function testExecuteWithExclusions()
206+
{
207+
// Add a file with errors and warnings, and verify that they are suppressed with the exclusions.
208+
$content = "<?php require(__DIR__.'/../../config.php');\n";
209+
210+
$this->fs->dumpFile($this->pluginDir . '/warnings.php', $content);
211+
212+
// Without exclusions.
213+
$commandTester = $this->executeCommand($this->pluginDir);
214+
$output = $commandTester->getDisplay();
215+
$this->assertSame(1, $commandTester->getStatusCode());
216+
$this->assertMatchesRegularExpression('/FOUND 9 ERRORS AND 1 WARNING AFFECTING 1 LINE/', $output);
217+
218+
// With exclusions.
219+
$commandTester = $this->executeCommand($this->pluginDir, ['--exclude' => 'moodle.Files.RequireLogin,moodle.Files.BoilerplateComment']);
220+
$this->assertSame(0, $commandTester->getStatusCode());
221+
}
222+
212223
public function testExecuteNoFiles()
213224
{
214225
// Just random directory with no PHP files.

0 commit comments

Comments
 (0)