Skip to content

Commit 2263c78

Browse files
committed
Exclude path: include example of suggested optional paths from current configuration
1 parent fbe14a4 commit 2263c78

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

src/Command/CommandHelper.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,19 @@ public static function begin(
379379
$errorOutput->writeLineFormatted('');
380380
}
381381

382-
$errorOutput->writeLineFormatted('If the excluded path can sometimes exist, append <fg=cyan>(?)</>');
383-
$errorOutput->writeLineFormatted('to its config entry to mark it as optional. Example:');
384-
$errorOutput->writeLineFormatted(' <fg=cyan>- path/something (?)</>');
385-
$errorOutput->writeLineFormatted('');
382+
$suggestOptional = $e->getSuggestOptional();
383+
if (count($suggestOptional) > 0) {
384+
$errorOutput->writeLineFormatted('If the excluded path can sometimes exist, append <fg=cyan>(?)</>');
385+
$errorOutput->writeLineFormatted('to its config entry to mark it as optional. Example:');
386+
$errorOutput->writeLineFormatted('<fg=cyan>excludePaths:</>');
387+
foreach ($suggestOptional as $key => $suggestOptionalPaths) {
388+
$errorOutput->writeLineFormatted(sprintf(' <fg=cyan>%s</>', $key));
389+
foreach ($suggestOptionalPaths as $suggestOptionalPath) {
390+
$errorOutput->writeLineFormatted(sprintf(' - <fg=cyan>%s (?)</>', $suggestOptionalPath));
391+
}
392+
}
393+
$errorOutput->writeLineFormatted('');
394+
}
386395

387396
throw new InceptionNotSuccessfulException();
388397
} catch (ValidationException $e) {

src/DependencyInjection/InvalidExcludePathsException.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ final class InvalidExcludePathsException extends Exception
1010

1111
/**
1212
* @param string[] $errors
13+
* @param array{analyse?: list<string>, analyseAndScan?: list<string>} $suggestOptional
1314
*/
14-
public function __construct(private array $errors)
15+
public function __construct(private array $errors, private array $suggestOptional)
1516
{
1617
parent::__construct(implode("\n", $this->errors));
1718
}
@@ -24,4 +25,11 @@ public function getErrors(): array
2425
return $this->errors;
2526
}
2627

28+
/**
29+
* @return array{analyse?: list<string>, analyseAndScan?: list<string>}
30+
*/
31+
final public function getSuggestOptional(): array
32+
{
33+
return $this->suggestOptional;
34+
}
2735
}

src/DependencyInjection/ValidateExcludePathsExtension.php

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\File\FileExcluder;
88
use function array_key_exists;
99
use function array_map;
10-
use function array_merge;
1110
use function count;
1211
use function is_dir;
1312
use function is_file;
@@ -27,41 +26,43 @@ public function loadConfiguration(): void
2726
return;
2827
}
2928

29+
$newExcludePaths = [];
30+
if (array_key_exists('analyseAndScan', $excludePaths)) {
31+
$newExcludePaths['analyseAndScan'] = $excludePaths['analyseAndScan'];
32+
}
33+
if (array_key_exists('analyse', $excludePaths)) {
34+
$newExcludePaths['analyse'] = $excludePaths['analyse'];
35+
}
36+
3037
$errors = [];
38+
$suggestOptional = [];
3139
if ($builder->parameters['__validate']) {
32-
$paths = [];
33-
if (array_key_exists('analyse', $excludePaths)) {
34-
$paths = $excludePaths['analyse'];
35-
}
36-
if (array_key_exists('analyseAndScan', $excludePaths)) {
37-
$paths = array_merge($paths, $excludePaths['analyseAndScan']);
38-
}
39-
foreach ($paths as $path) {
40-
if ($path instanceof OptionalPath) {
41-
continue;
42-
}
43-
if (FileExcluder::isAbsolutePath($path)) {
44-
if (is_dir($path)) {
40+
foreach ($newExcludePaths as $key => $paths) {
41+
foreach ($paths as $path) {
42+
if ($path instanceof OptionalPath) {
4543
continue;
4644
}
47-
if (is_file($path)) {
45+
if (FileExcluder::isAbsolutePath($path)) {
46+
if (is_dir($path)) {
47+
continue;
48+
}
49+
if (is_file($path)) {
50+
continue;
51+
}
52+
53+
$suggestOptional[$key][] = $path;
54+
}
55+
if (FileExcluder::isFnmatchPattern($path)) {
4856
continue;
4957
}
50-
}
51-
if (FileExcluder::isFnmatchPattern($path)) {
52-
continue;
53-
}
5458

55-
$errors[] = sprintf('Path %s is neither a directory, nor a file path, nor a fnmatch pattern.', $path);
59+
$errors[] = sprintf('Path %s is neither a directory, nor a file path, nor a fnmatch pattern.', $path);
60+
}
5661
}
5762
}
5863

59-
$newExcludePaths = [];
60-
if (array_key_exists('analyseAndScan', $excludePaths)) {
61-
$newExcludePaths['analyseAndScan'] = $excludePaths['analyseAndScan'];
62-
}
63-
if (array_key_exists('analyse', $excludePaths)) {
64-
$newExcludePaths['analyse'] = $excludePaths['analyse'];
64+
if (count($errors) !== 0) {
65+
throw new InvalidExcludePathsException($errors, $suggestOptional);
6566
}
6667

6768
foreach ($newExcludePaths as $key => $p) {
@@ -72,12 +73,6 @@ public function loadConfiguration(): void
7273
}
7374

7475
$builder->parameters['excludePaths'] = $newExcludePaths;
75-
76-
if (count($errors) === 0) {
77-
return;
78-
}
79-
80-
throw new InvalidExcludePathsException($errors);
8176
}
8277

8378
}

0 commit comments

Comments
 (0)