Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add --only-remove-errors analyse command option #3847

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

Open
N-Silbernagel wants to merge 10 commits into phpstan:2.1.x
base: 2.1.x
Choose a base branch
Loading
from N-Silbernagel:add-ignore-new-errors-option
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit Hold shift + click to select a range
cca5977
Add --ignore-new-errors analyse command option
N-Silbernagel Mar 2, 2025
4076f4a
Revert "Add --ignore-new-errors analyse command option"
N-Silbernagel Mar 3, 2025
0f6eaeb
Add --ignore-new-errors option for baseline generation
N-Silbernagel Mar 11, 2025
6f1584a
Handle more errors than ignore count in --ignore-new-errors
N-Silbernagel Mar 12, 2025
bcc7816
Add test for increasing error count
N-Silbernagel Mar 13, 2025
cc56f29
Add ignore-new-errors test files to collision ignores
N-Silbernagel Mar 13, 2025
f285fc4
Shorten assert text of testGenerateBaselineIgnoreNewErrorsEmptyBaseline
N-Silbernagel Mar 13, 2025
3464d8a
Rename ignore-new-errors option to only-remove-errors
N-Silbernagel Mar 19, 2025
e69bb2c
Extract ignore-new-errors logic into BaselineIgnoredErrorHelper
N-Silbernagel Mar 19, 2025
0f27eea
Add BaselineIgnoredErrorsHelperTest, remove AnalyseCommandTest cases
N-Silbernagel Mar 20, 2025
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
Next Next commit
Add --ignore-new-errors analyse command option
  • Loading branch information
N-Silbernagel committed Mar 20, 2025
commit cca5977c64adcccc3905ef79148c95c4f9cedb8d
53 changes: 38 additions & 15 deletions src/Analyser/Ignore/IgnoredErrorHelperResult.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,15 @@ public function process(
bool $onlyFiles,
array $analysedFiles,
bool $hasInternalErrors,
bool $generateBaseline,
bool $ignoreNewErrors,
): IgnoredErrorHelperProcessedResult
{
$unmatchedIgnoredErrors = $this->ignoreErrors;
// if we are generating the baseline, we dont want ignore Errors configuration to affect the errors we output
$unmatchedIgnoredErrors = $generateBaseline ? [] : $this->ignoreErrors;
$stringErrors = [];

$processIgnoreError = function (Error $error, int $i, $ignore) use (&$unmatchedIgnoredErrors, &$stringErrors): bool {
$processIgnoreError = function (Error $error, int $i, $ignore) use ($generateBaseline, &$unmatchedIgnoredErrors, &$stringErrors): bool {
$shouldBeIgnored = false;
if (is_string($ignore)) {
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore, null, null);
Expand All @@ -65,7 +68,8 @@ public function process(
} else {
if (isset($ignore['path'])) {
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore['message'] ?? null, $ignore['identifier'] ?? null, $ignore['path']);
if ($shouldBeIgnored) {
// only ignore errors when not generating the baseline, because it need to contain all errors
if ($shouldBeIgnored && !$generateBaseline) {
if (isset($ignore['count'])) {
$realCount = $unmatchedIgnoredErrors[$i]['realCount'] ?? 0;
$realCount++;
Expand Down Expand Up @@ -115,12 +119,12 @@ public function process(
'Error message "%s" cannot be ignored, use excludePaths instead.',
$error->getMessage(),
);
return true;
return false;
}
return false;
return true;
}

return true;
return false;
};

$ignoredErrors = [];
Expand All @@ -130,12 +134,16 @@ public function process(
foreach ($this->ignoreErrorsByFile[$filePath] as $ignoreError) {
$i = $ignoreError['index'];
$ignore = $ignoreError['ignoreError'];
$result = $processIgnoreError($error, $i, $ignore);
if (!$result) {
$isIgnoreMatch = $processIgnoreError($error, $i, $ignore);
if (!$isIgnoreMatch) {
continue;
}

if (!$generateBaseline) {
unset($errors[$errorIndex]);
$ignoredErrors[] = [$error, $ignore];
continue 2;
}
continue 2;
}
}

Expand All @@ -146,12 +154,16 @@ public function process(
foreach ($this->ignoreErrorsByFile[$normalizedTraitFilePath] as $ignoreError) {
$i = $ignoreError['index'];
$ignore = $ignoreError['ignoreError'];
$result = $processIgnoreError($error, $i, $ignore);
if (!$result) {
$isIgnoreMatch = $processIgnoreError($error, $i, $ignore);
if (!$isIgnoreMatch) {
continue;
}

if (!$generateBaseline) {
unset($errors[$errorIndex]);
$ignoredErrors[] = [$error, $ignore];
continue 2;
}
continue 2;
}
}
}
Expand All @@ -160,13 +172,24 @@ public function process(
$i = $ignoreError['index'];
$ignore = $ignoreError['ignoreError'];

$result = $processIgnoreError($error, $i, $ignore);
if (!$result) {
$isIgnoreMatch = $processIgnoreError($error, $i, $ignore);
if (!$isIgnoreMatch) {
continue;
}

if (!$generateBaseline) {
unset($errors[$errorIndex]);
$ignoredErrors[] = [$error, $ignore];
continue 2;
}
continue 2;
}

if (!$ignoreNewErrors) {
continue;
}

// if the error was not ignored, it is a new error, don't return it when $ignoreNewErrors is set
unset($errors[$errorIndex]);
}

$errors = array_values($errors);
Expand Down
4 changes: 3 additions & 1 deletion src/Command/AnalyseApplication.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public function analyse(
?string $projectConfigFile,
?array $projectConfigArray,
InputInterface $input,
bool $generateBaseline,
bool $ignoreNewErrors,
): AnalysisResult
{
$isResultCacheUsed = false;
Expand Down Expand Up @@ -142,7 +144,7 @@ public function analyse(
}
}

$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process($errors, $onlyFiles, $files, $hasInternalErrors);
$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process($errors, $onlyFiles, $files, $hasInternalErrors, $generateBaseline, $ignoreNewErrors);
$fileSpecificErrors = $ignoredErrorHelperProcessedResult->getNotIgnoredErrors();
$notFileSpecificErrors = $ignoredErrorHelperProcessedResult->getOtherIgnoreMessages();
$collectedData = $analyserResult->getCollectedData();
Expand Down
9 changes: 9 additions & 0 deletions src/Command/AnalyseCommand.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ protected function configure(): void
new InputOption('watch', null, InputOption::VALUE_NONE, 'Launch PHPStan Pro'),
new InputOption('pro', null, InputOption::VALUE_NONE, 'Launch PHPStan Pro'),
new InputOption('fail-without-result-cache', null, InputOption::VALUE_NONE, 'Return non-zero exit code when result cache is not used'),
new InputOption('ignore-new-errors', null, InputOption::VALUE_NONE, 'Ignore new errors when generating the baseline.'),
]);
}

Expand Down Expand Up @@ -136,6 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$debugEnabled = (bool) $input->getOption('debug');
$fix = (bool) $input->getOption('fix') || (bool) $input->getOption('watch') || (bool) $input->getOption('pro');
$failWithoutResultCache = (bool) $input->getOption('fail-without-result-cache');
$ignoreNewErrors = (bool) $input->getOption('ignore-new-errors');

/** @var string|false|null $generateBaselineFile */
$generateBaselineFile = $input->getOption('generate-baseline');
Expand Down Expand Up @@ -182,6 +184,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
}

if ($generateBaselineFile === null && $ignoreNewErrors) {
$inceptionResult->getStdOutput()->getStyle()->error('You must pass the --generate-baseline option alongside --ignore-new-errors.');
return $inceptionResult->handleReturn(1, null, $this->analysisStartTime);
}

$errorOutput = $inceptionResult->getErrorOutput();
$errorFormat = $input->getOption('error-format');

Expand Down Expand Up @@ -290,6 +297,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$inceptionResult->getProjectConfigFile(),
$inceptionResult->getProjectConfigArray(),
$input,
$generateBaselineFile !== null,
$ignoreNewErrors,
);
} catch (Throwable $t) {
if ($debug) {
Expand Down
2 changes: 0 additions & 2 deletions src/Command/CommandHelper.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,8 @@ public static function begin(
}

$loader = (new LoaderFactory(
$currentWorkingDirectoryFileHelper,
$containerFactory->getRootDirectory(),
$containerFactory->getCurrentWorkingDirectory(),
$generateBaselineFile,
))->createLoader();

try {
Expand Down
11 changes: 10 additions & 1 deletion src/Command/FixerWorkerCommand.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ function (array $errors, array $locallyIgnoredErrors, array $analysedFiles) use
$isOnlyFiles,
$inceptionFiles,
$hasInternalErrors,
false,
false,
);
$ignoreFileErrors = [];
foreach ($ignoredErrorHelperProcessedResult->getNotIgnoredErrors() as $error) {
Expand Down Expand Up @@ -355,7 +357,14 @@ private function transformErrorIntoInternalError(Error $error): InternalError
*/
private function filterErrors(array $errors, IgnoredErrorHelperResult $ignoredErrorHelperResult, bool $onlyFiles, array $inceptionFiles, bool $hasInternalErrors): array
{
$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process($errors, $onlyFiles, $inceptionFiles, $hasInternalErrors);
$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process(
$errors,
$onlyFiles,
$inceptionFiles,
$hasInternalErrors,
false,
false,
);
$finalErrors = [];
foreach ($ignoredErrorHelperProcessedResult->getNotIgnoredErrors() as $error) {
if ($error->getIdentifier() === null) {
Expand Down
2 changes: 0 additions & 2 deletions src/DependencyInjection/ContainerFactory.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@ public function create(
);

$configurator = new Configurator(new LoaderFactory(
$this->fileHelper,
$this->rootDirectory,
$this->currentWorkingDirectory,
$generateBaselineFile,
), $this->journalContainer);
$configurator->defaultExtensions = [
'php' => PhpExtension::class,
Expand Down
5 changes: 1 addition & 4 deletions src/DependencyInjection/LoaderFactory.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
namespace PHPStan\DependencyInjection;

use Nette\DI\Config\Loader;
use PHPStan\File\FileHelper;
use function getenv;

final class LoaderFactory
{

public function __construct(
private FileHelper $fileHelper,
private string $rootDir,
private string $currentWorkingDirectory,
private ?string $generateBaselineFile,
)
{
}

public function createLoader(): Loader
{
$loader = new NeonLoader($this->fileHelper, $this->generateBaselineFile);
$loader = new Loader();
$loader->addAdapter('dist', NeonAdapter::class);
$loader->addAdapter('neon', NeonAdapter::class);
$loader->setParameters([
Expand Down
35 changes: 0 additions & 35 deletions src/DependencyInjection/NeonLoader.php
View file Open in desktop

This file was deleted.

32 changes: 31 additions & 1 deletion tests/PHPStan/Analyser/AnalyserTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ public function testReturnErrorIfIgnoredMessagesDoesNotOccur(): void
], $result);
}

public function testDontReturnErrorIfIgnoredMessagesDoesNotOccurWhenGeneratingBaseline(): void
{
$result = $this->runAnalyser(['#Unknown error#'], false, __DIR__ . '/data/empty/empty.php', false, true);

$this->assertEmpty($result);
}

public function testDontReturnErrorOnNewErrorWhenIgnoringNewErrors(): void
{
$result = $this->runAnalyser(['#Unknown error#'], false, __DIR__ . '/data/bootstrap-error.php', false, true, true);

$this->assertEmpty($result);
}

public function testReturnErrorOnIgnoredErrorWhenIgnoringNewErrorsAndGeneratingBaseline(): void
{
$result = $this->runAnalyser(['#Fail\.#'], false, __DIR__ . '/data/bootstrap-error.php', false, true, true);

$this->assertNotEmpty($result);
}

public function testDoNotReturnErrorIfIgnoredMessagesDoesNotOccurWithReportUnmatchedIgnoredErrorsOff(): void
{
$result = $this->runAnalyser(['#Unknown error#'], false, __DIR__ . '/data/empty/empty.php', false);
Expand Down Expand Up @@ -644,6 +665,8 @@ private function runAnalyser(
bool $reportUnmatchedIgnoredErrors,
$filePaths,
bool $onlyFiles,
bool $generateBaseline = false,
bool $ignoreNewErrors = false,
): array
{
$analyser = $this->createAnalyser();
Expand Down Expand Up @@ -679,7 +702,14 @@ private function runAnalyser(
);
$analyserResult = $finalizer->finalize($analyserResult, $onlyFiles, false)->getAnalyserResult();

$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $normalizedFilePaths, $analyserResult->hasReachedInternalErrorsCountLimit());
$ignoredErrorHelperProcessedResult = $ignoredErrorHelperResult->process(
$analyserResult->getErrors(),
$onlyFiles,
$normalizedFilePaths,
$analyserResult->hasReachedInternalErrorsCountLimit(),
$generateBaseline,
$ignoreNewErrors,
);
$errors = $ignoredErrorHelperProcessedResult->getNotIgnoredErrors();
$errors = array_merge($errors, $ignoredErrorHelperProcessedResult->getOtherIgnoreMessages());
if ($analyserResult->hasReachedInternalErrorsCountLimit()) {
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Command/AnalyseApplicationIntegrationTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ private function runPath(string $path, int $expectedStatusCode): string
null,
null,
$this->createMock(InputInterface::class),
false,
false,
);
$statusCode = $errorFormatter->formatErrors($analysisResult, $symfonyOutput);

Expand Down

AltStyle によって変換されたページ (->オリジナル) /