Skip to content

Commit

Permalink
Add --no-ignore (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Jun 1, 2024
1 parent dc4deb7 commit 3783dac
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 12 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ To get errors count per level:

<br>

## Do you want to run levels including all ignored messages?

```bash
vendor/bin/phpstan-bodyscan --no-ignore
```

<br>


## Do you want to run levels without extensions?

```bash
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions phpstan-bodyscan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
includes:
- vendor/symplify/phpstan-rules/config/code-complexity-rules.neon
- vendor/symplify/phpstan-rules/config/collector-rules.neon
- vendor/symplify/phpstan-rules/config/naming-rules.neon
- vendor/symplify/phpstan-rules/config/regex-rules.neon
- vendor/symplify/phpstan-rules/config/static-rules.neon

parameters:
level: 8
excludePaths:
- */Fixture/*

paths:
- bin
- src
- tests

unused_public:
methods: true
constants: true
properties: true

type_coverage:
param: 99
property: 99
return: 99
declare: 99

reportUnmatchedIgnoredErrors: false
5 changes: 3 additions & 2 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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) {
Expand All @@ -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 = [];
Expand Down
42 changes: 37 additions & 5 deletions src/PHPStanConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ final class PHPStanConfigFactory
/**
* @param array<string, mixed[]> $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) {
Expand All @@ -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);
}

Expand Down Expand Up @@ -102,11 +107,18 @@ private function createBasicPHPStanConfiguration(string $projectDirectory): arra
/**
* @param array<string, mixed> $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);
}

Expand All @@ -118,4 +130,24 @@ private function dumpNeonToString(array $phpstanConfiguration): string
$encodedNeon = Neon::encode($phpstanConfiguration, true, ' ');
return trim($encodedNeon) . PHP_EOL;
}

/**
* @param array<string, mixed> $neon
* @return array<string, mixed>
*/
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((string) $includedFile, 'baseline')) {
unset($neon['includes'][$key]);
}
}
}

unset($neon['parameters']['ignoreErrors']);

return $neon;
}
}
15 changes: 12 additions & 3 deletions src/ValueObject/BodyscanResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace TomasVotruba\PHPStanBodyscan\ValueObject;

use Webmozart\Assert\Assert;

final readonly class BodyscanResult
{
/**
Expand All @@ -12,15 +14,24 @@
public function __construct(
private array $levelResults
) {
Assert::allIsInstanceOf($levelResults, LevelResult::class);
Assert::notEmpty($levelResults);
}

/**
* @return LevelResult[]
*/
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();
Expand All @@ -29,7 +40,5 @@ public function getLevelResults(): array

$previousLevelResult = $levelResult;
}

return $this->levelResults;
}
}

0 comments on commit 3783dac

Please sign in to comment.