Skip to content

Commit 7dd1a98

Browse files
committed
Runner: allow suppressing "No files were checked" error
Fixes squizlabs/PHP_CodeSniffer#3556
1 parent 892320f commit 7dd1a98

File tree

3 files changed

+104
-37
lines changed

3 files changed

+104
-37
lines changed

src/Config.php

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* @property string $stdinContent Content passed directly to PHPCS on STDIN.
6666
* @property string $stdinPath The path to use for content passed on STDIN.
6767
* @property bool $trackTime Whether or not to track sniff run time.
68+
* @property bool $allowEmptyFileList Suppresses "No files were checked" error.
6869
*
6970
* @property array<string, string> $extensions File extensions that should be checked, and what tokenizer is used.
7071
* E.g., array('inc' => 'PHP');
@@ -160,42 +161,43 @@ class Config
160161
* @var array<string, mixed>
161162
*/
162163
private $settings = [
163-
'files' => null,
164-
'standards' => null,
165-
'verbosity' => null,
166-
'interactive' => null,
167-
'parallel' => null,
168-
'cache' => null,
169-
'cacheFile' => null,
170-
'colors' => null,
171-
'explain' => null,
172-
'local' => null,
173-
'showSources' => null,
174-
'showProgress' => null,
175-
'quiet' => null,
176-
'annotations' => null,
177-
'tabWidth' => null,
178-
'encoding' => null,
179-
'extensions' => null,
180-
'sniffs' => null,
181-
'exclude' => null,
182-
'ignored' => null,
183-
'reportFile' => null,
184-
'generator' => null,
185-
'filter' => null,
186-
'bootstrap' => null,
187-
'reports' => null,
188-
'basepath' => null,
189-
'reportWidth' => null,
190-
'errorSeverity' => null,
191-
'warningSeverity' => null,
192-
'recordErrors' => null,
193-
'suffix' => null,
194-
'stdin' => null,
195-
'stdinContent' => null,
196-
'stdinPath' => null,
197-
'trackTime' => null,
198-
'unknown' => null,
164+
'files' => null,
165+
'standards' => null,
166+
'verbosity' => null,
167+
'interactive' => null,
168+
'parallel' => null,
169+
'cache' => null,
170+
'cacheFile' => null,
171+
'colors' => null,
172+
'explain' => null,
173+
'local' => null,
174+
'showSources' => null,
175+
'showProgress' => null,
176+
'quiet' => null,
177+
'annotations' => null,
178+
'tabWidth' => null,
179+
'encoding' => null,
180+
'extensions' => null,
181+
'sniffs' => null,
182+
'exclude' => null,
183+
'ignored' => null,
184+
'reportFile' => null,
185+
'generator' => null,
186+
'filter' => null,
187+
'bootstrap' => null,
188+
'reports' => null,
189+
'basepath' => null,
190+
'reportWidth' => null,
191+
'errorSeverity' => null,
192+
'warningSeverity' => null,
193+
'recordErrors' => null,
194+
'suffix' => null,
195+
'stdin' => null,
196+
'stdinContent' => null,
197+
'stdinPath' => null,
198+
'trackTime' => null,
199+
'unknown' => null,
200+
'allowEmptyFileList' => null,
199201
];
200202

201203
/**
@@ -581,6 +583,7 @@ public function restoreDefaults()
581583
$this->stdinPath = null;
582584
$this->trackTime = false;
583585
$this->unknown = [];
586+
$this->allowEmptyFileList = false;
584587

585588
$standard = self::getConfigData('default_standard');
586589
if ($standard !== null) {
@@ -828,6 +831,14 @@ public function processLongArgument(string $arg, int $pos)
828831
$this->annotations = false;
829832
$this->overriddenDefaults['annotations'] = true;
830833
break;
834+
case 'allow-empty-file-list':
835+
if (isset($this->overriddenDefaults['allowEmptyFileList']) === true) {
836+
break;
837+
}
838+
839+
$this->allowEmptyFileList = true;
840+
$this->overriddenDefaults['allowEmptyFileList'] = true;
841+
break;
831842
case 'config-set':
832843
if (isset($this->cliArgs[($pos + 1)]) === false
833844
|| isset($this->cliArgs[($pos + 2)]) === false

src/Runner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ private function run()
351351
}
352352

353353
$numFiles = count($todo);
354-
if ($numFiles === 0) {
354+
if ($numFiles === 0 && $this->config->allowEmptyFileList === false) {
355355
$error = 'ERROR: No files were checked.' . PHP_EOL;
356356
$error .= 'All specified files were excluded or did not match filtering rules.' . PHP_EOL . PHP_EOL;
357357
throw new DeepExitException($error, ExitCode::PROCESS_ERROR);

tests/Core/Runner/RunAllFilesExcludedErrorTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,62 @@ public function testPhpcbf($sourceDir, $extraArgs)
7373
}
7474

7575

76+
/**
77+
* Verify that the "All files were excluded" error message is not shown when all files are excluded and it is
78+
* allowed via --allow-empty-file-list (PHPCS).
79+
*
80+
* @param string $sourceDir The (fixture) directory to scan for files.
81+
* @param array<string> $extraArgs Any extra arguments to pass on the command line.
82+
*
83+
* @dataProvider data
84+
*
85+
* @return void
86+
*/
87+
public function testPhpcsAllowEmpty($sourceDir, $extraArgs)
88+
{
89+
if (PHP_CODESNIFFER_CBF === true) {
90+
$this->markTestSkipped('This test needs CS mode to run');
91+
}
92+
93+
$extraArgs[] = '--allow-empty-file-list';
94+
$this->setupTest($sourceDir, $extraArgs);
95+
96+
$runner = new Runner();
97+
$runner->runPHPCS();
98+
99+
$regex = '`^(Time: [0-9]+ms; Memory: [0-9\.]+MB' . PHP_EOL . ')?$`';
100+
$this->assertStderrOutputMatchesRegex($regex);
101+
}
102+
103+
104+
/**
105+
* Verify that the "All files were excluded" error message is not shown when all files are excluded and it is
106+
* * allowed via --allow-empty-file-list (PHPCBF).
107+
*
108+
* @param string $sourceDir The (fixture) directory to scan for files.
109+
* @param array<string> $extraArgs Any extra arguments to pass on the command line.
110+
*
111+
* @dataProvider data
112+
* @group CBF
113+
*
114+
* @return void
115+
*/
116+
public function testPhpcbfAllowEmpty($sourceDir, $extraArgs)
117+
{
118+
if (PHP_CODESNIFFER_CBF === false) {
119+
$this->markTestSkipped('This test needs CBF mode to run');
120+
}
121+
122+
$extraArgs[] = '--allow-empty-file-list';
123+
$this->setupTest($sourceDir, $extraArgs);
124+
125+
$runner = new Runner();
126+
$runner->runPHPCBF();
127+
128+
$this->assertStderrOutputSameString('');
129+
}
130+
131+
76132
/**
77133
* Data provider.
78134
*

0 commit comments

Comments
 (0)