Skip to content

Excluded path: include example for optional path #3883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PHPStan\File\FileExcluder;
use PHPStan\File\FileFinder;
use PHPStan\File\FileHelper;
use PHPStan\File\ParentDirectoryRelativePathHelper;
use PHPStan\File\SimpleRelativePathHelper;
use PHPStan\Internal\ComposerHelper;
use PHPStan\Internal\DirectoryCreator;
Expand Down Expand Up @@ -379,9 +380,30 @@ public static function begin(
$errorOutput->writeLineFormatted('');
}

$errorOutput->writeLineFormatted('If the excluded path can sometimes exist, append <fg=cyan>(?)</>');
$errorOutput->writeLineFormatted('to its config entry to mark it as optional.');
$errorOutput->writeLineFormatted('');
$suggestOptional = $e->getSuggestOptional();
if (count($suggestOptional) > 0) {
$baselinePathHelper = null;
if ($projectConfigFile !== null) {
$baselinePathHelper = new ParentDirectoryRelativePathHelper(dirname($projectConfigFile));
}
$errorOutput->writeLineFormatted('If the excluded path can sometimes exist, append <fg=cyan>(?)</>');
$errorOutput->writeLineFormatted('to its config entry to mark it as optional. Example:');
$errorOutput->writeLineFormatted('');
$errorOutput->writeLineFormatted('<fg=cyan>parameters:</>');
$errorOutput->writeLineFormatted("\t<fg=cyan>excludePaths:</>");
foreach ($suggestOptional as $key => $suggestOptionalPaths) {
$errorOutput->writeLineFormatted(sprintf("\t\t<fg=cyan>%s:</>", $key));
foreach ($suggestOptionalPaths as $suggestOptionalPath) {
if ($baselinePathHelper === null) {
$errorOutput->writeLineFormatted(sprintf("\t\t\t- <fg=cyan>%s (?)</>", $suggestOptionalPath));
continue;
}

$errorOutput->writeLineFormatted(sprintf("\t\t\t- <fg=cyan>%s (?)</>", $baselinePathHelper->getRelativePath($suggestOptionalPath)));
}
}
$errorOutput->writeLineFormatted('');
}

throw new InceptionNotSuccessfulException();
} catch (ValidationException $e) {
Expand Down
11 changes: 10 additions & 1 deletion src/DependencyInjection/InvalidExcludePathsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ final class InvalidExcludePathsException extends Exception

/**
* @param string[] $errors
* @param array{analyse?: list<string>, analyseAndScan?: list<string>} $suggestOptional
*/
public function __construct(private array $errors)
public function __construct(private array $errors, private array $suggestOptional)
{
parent::__construct(implode("\n", $this->errors));
}
Expand All @@ -24,4 +25,12 @@ public function getErrors(): array
return $this->errors;
}

/**
* @return array{analyse?: list<string>, analyseAndScan?: list<string>}
*/
public function getSuggestOptional(): array
{
return $this->suggestOptional;
}

}
58 changes: 26 additions & 32 deletions src/DependencyInjection/ValidateExcludePathsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\File\FileExcluder;
use function array_key_exists;
use function array_map;
use function array_merge;
use function count;
use function is_dir;
use function is_file;
Expand All @@ -27,41 +26,42 @@ public function loadConfiguration(): void
return;
}

$newExcludePaths = [];
if (array_key_exists('analyseAndScan', $excludePaths)) {
$newExcludePaths['analyseAndScan'] = $excludePaths['analyseAndScan'];
}
if (array_key_exists('analyse', $excludePaths)) {
$newExcludePaths['analyse'] = $excludePaths['analyse'];
}

$errors = [];
$suggestOptional = [];
if ($builder->parameters['__validate']) {
$paths = [];
if (array_key_exists('analyse', $excludePaths)) {
$paths = $excludePaths['analyse'];
}
if (array_key_exists('analyseAndScan', $excludePaths)) {
$paths = array_merge($paths, $excludePaths['analyseAndScan']);
}
foreach ($paths as $path) {
if ($path instanceof OptionalPath) {
continue;
}
if (FileExcluder::isAbsolutePath($path)) {
if (is_dir($path)) {
foreach ($newExcludePaths as $key => $paths) {
foreach ($paths as $path) {
if ($path instanceof OptionalPath) {
continue;
}
if (is_file($path)) {
if (FileExcluder::isAbsolutePath($path)) {
if (is_dir($path)) {
continue;
}
if (is_file($path)) {
continue;
}
}
if (FileExcluder::isFnmatchPattern($path)) {
continue;
}
}
if (FileExcluder::isFnmatchPattern($path)) {
continue;
}

$errors[] = sprintf('Path %s is neither a directory, nor a file path, nor a fnmatch pattern.', $path);
$suggestOptional[$key][] = $path;
$errors[] = sprintf('Path "%s" is neither a directory, nor a file path, nor a fnmatch pattern.', $path);
}
}
}

$newExcludePaths = [];
if (array_key_exists('analyseAndScan', $excludePaths)) {
$newExcludePaths['analyseAndScan'] = $excludePaths['analyseAndScan'];
}
if (array_key_exists('analyse', $excludePaths)) {
$newExcludePaths['analyse'] = $excludePaths['analyse'];
if (count($errors) !== 0) {
throw new InvalidExcludePathsException($errors, $suggestOptional);
}

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

$builder->parameters['excludePaths'] = $newExcludePaths;

if (count($errors) === 0) {
return;
}

throw new InvalidExcludePathsException($errors);
}

}
2 changes: 1 addition & 1 deletion src/DependencyInjection/ValidateIgnoredErrorsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function getRegistry(): OperatorTypeSpecifyingExtensionRegistry
continue;
}

$errors[] = sprintf('Path %s is neither a directory, nor a file path, nor a fnmatch pattern.', $ignorePath);
$errors[] = sprintf('Path "%s" is neither a directory, nor a file path, nor a fnmatch pattern.', $ignorePath);
}
}
}
Expand Down
Loading