From eb97c2f5172435fd3e802aa7832e9889270d4ae9 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 1 Jun 2024 00:35:00 +0900 Subject: [PATCH] misc --- composer.json | 4 +-- src/Command/RunCommand.php | 5 ++-- src/PHPStanConfigFactory.php | 42 ++++++++++++++++++++++++++---- src/ValueObject/BodyscanResult.php | 15 ++++++++--- 4 files changed, 54 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 95060f0..d755802 100644 --- a/composer.json +++ b/composer.json @@ -18,9 +18,9 @@ "phpstan/extension-installer": "^1.3", "phpstan/phpstan": "^1.11", "phpunit/phpunit": "^11.1", - "rector/rector": "^1.0", + "rector/rector": "^1.1", "symplify/easy-coding-standard": "^12.1", - "symplify/phpstan-rules": "^12.4", + "symplify/phpstan-rules": "^12.7", "tomasvotruba/class-leak": "^0.2.13", "tomasvotruba/type-coverage": "^0.3", "tomasvotruba/unused-public": "^0.3", diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 287687e..f015e03 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -58,7 +58,7 @@ protected function configure(): void 'Without any extensions, without ignores, without baselines, just pure PHPStan' ); - // @todo nobaseline - without ignores and baseline files + $this->addOption('no-ignore', null, InputOption::VALUE_NONE, 'Run PHPStan without any ignores/baselines'); } /** @@ -76,6 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $isBare = (bool) $input->getOption('bare'); $isJson = (bool) $input->getOption('json'); + $isNoIgnore = (bool) $input->getOption('no-ignore'); // silence output till the end to avoid invalid json format if ($isJson) { @@ -86,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int // 1. prepare empty phpstan config // no baselines, ignores etc. etc :) - $phpstanConfig = $this->phpStanConfigFactory->create($projectDirectory, [], $isBare); + $phpstanConfig = $this->phpStanConfigFactory->create($projectDirectory, [], $isBare, $isNoIgnore); file_put_contents($projectDirectory . '/phpstan-bodyscan.neon', $phpstanConfig->getFileContents()); $levelResults = []; diff --git a/src/PHPStanConfigFactory.php b/src/PHPStanConfigFactory.php index a2bcf15..7a284bf 100644 --- a/src/PHPStanConfigFactory.php +++ b/src/PHPStanConfigFactory.php @@ -25,8 +25,12 @@ final class PHPStanConfigFactory /** * @param array $extraConfiguration */ - public function create(string $projectDirectory, array $extraConfiguration = [], bool $bare = false): PHPStanConfig - { + public function create( + string $projectDirectory, + array $extraConfiguration = [], + bool $bare = false, + bool $isNoIgnore = false + ): PHPStanConfig { $existingPHPStanFile = null; foreach (self::PHPSTAN_FILE_NAMES as $phpstanFileName) { @@ -50,7 +54,8 @@ public function create(string $projectDirectory, array $extraConfiguration = [], // disable ignored error reporting, to make no fatal errors 'reportUnmatchedIgnoredErrors' => false, ], - ]); + ], $isNoIgnore); + return new PHPStanConfig($phpStanNeonContents); } @@ -102,11 +107,18 @@ private function createBasicPHPStanConfiguration(string $projectDirectory): arra /** * @param array $extraContents */ - private function loadFileAndMergeParameters(string $existingPHPStanFile, array $extraContents): string - { + private function loadFileAndMergeParameters( + string $existingPHPStanFile, + array $extraContents, + bool $isNoIgnore + ): string { $neon = Neon::decodeFile($existingPHPStanFile); $neon = array_merge_recursive($neon, $extraContents); + if ($isNoIgnore) { + $neon = $this->removeIgnoredErrors($neon); + } + return $this->dumpNeonToString($neon); } @@ -118,4 +130,24 @@ private function dumpNeonToString(array $phpstanConfiguration): string $encodedNeon = Neon::encode($phpstanConfiguration, true, ' '); return trim($encodedNeon) . PHP_EOL; } + + /** + * @param array $neon + * @return array + */ + private function removeIgnoredErrors(array $neon): array + { + // remove included baseline and other ignored errors + if (isset($neon['includes'])) { + foreach ($neon['includes'] as $key => $includedFile) { + if (str_contains($includedFile, 'baseline')) { + unset($neon['includes'][$key]); + } + } + } + + unset($neon['parameters']['ignoreErrors']); + + return $neon; + } } diff --git a/src/ValueObject/BodyscanResult.php b/src/ValueObject/BodyscanResult.php index e708295..02330ea 100644 --- a/src/ValueObject/BodyscanResult.php +++ b/src/ValueObject/BodyscanResult.php @@ -4,6 +4,8 @@ namespace TomasVotruba\PHPStanBodyscan\ValueObject; +use Webmozart\Assert\Assert; + final readonly class BodyscanResult { /** @@ -12,6 +14,8 @@ public function __construct( private array $levelResults ) { + Assert::allIsInstanceOf($levelResults, LevelResult::class); + Assert::notEmpty($levelResults); } /** @@ -19,8 +23,15 @@ public function __construct( */ public function getLevelResults(): array { - // add relative to previous level + $this->computeChangesToPreviousLevels(); + + return $this->levelResults; + } + + private function computeChangesToPreviousLevels(): void + { $previousLevelResult = null; + foreach ($this->levelResults as $levelResult) { if ($previousLevelResult instanceof LevelResult) { $changeToPreviousLevel = $levelResult->getErrorCount() - $previousLevelResult->getErrorCount(); @@ -29,7 +40,5 @@ public function getLevelResults(): array $previousLevelResult = $levelResult; } - - return $this->levelResults; } }