Skip to content

Commit

Permalink
add --bare
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 31, 2024
1 parent e38abea commit a12cce5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

[![Downloads total](https://img.shields.io/packagist/dt/tomasvotruba/phpstan-bodyscan.svg?style=flat-square)](https://packagist.org/packages/tomasvotruba/phpstan-bodyscan/stats)

Do you want to get quick glimpse of new project code quality?
* Do you want to get quick glimpse of new project code quality?
* Do you want to know, what PHPStan level is the best for your project?
* Do you want to know, how much errors you're facing per level to see how hard it will be to reach them?

Get error count for each PHPStan level!

<br>

## How does it work?

First, we look into the project root for `phpstan.neon` file. If found, we reuse its `parameters > paths` configuration. If not, we look for defaults source code paths like `/src`, `/app`, `/tests`, etc.
First, we look into the project root for `phpstan.neon` file.

* If found, we reuse it.
* If not, we look for defaults source code paths like `/src`, `/app`, `/tests`, etc.

Then we run PHPStan for each level from 0 to 8. We count errors and display them in a table.

Expand Down Expand Up @@ -53,6 +58,15 @@ To get errors count per level:

<br>

## Do you want to run levels without extensions?

```bash
vendor/bin/phpstan-bodyscan --bare
```

<br>


## Do you need a JSON format?

We got you covered:
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ parameters:
property: 99
return: 99
declare: 99

ignoreErrors:
- '#Parameter (.*?) expects string, string\|false given#'
17 changes: 11 additions & 6 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// 1. prepare empty phpstan config
// no baselines, ignores etc. etc :)
$phpstanConfiguration = $this->phpStanConfigFactory->create($projectDirectory, $isBare);
file_put_contents($projectDirectory . '/phpstan-bodyscan.neon', $phpstanConfiguration);
$phpstanConfig = $this->phpStanConfigFactory->create($projectDirectory, [], $isBare);
if ($phpstanConfig->isPHPFile()) {
file_put_contents($projectDirectory . '/phpstan-bodyscan.php', $phpstanConfig->getFileContents());
} else {
file_put_contents($projectDirectory . '/phpstan-bodyscan.neon', $phpstanConfig->getFileContents());
}

$levelResults = [];

Expand All @@ -94,8 +98,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// 2. measure phpstan levels
for ($phpStanLevel = $minPhpStanLevel; $phpStanLevel <= $maxPhpStanLevel; ++$phpStanLevel) {
$this->symfonyStyle->section(sprintf('Running PHPStan level %d %s', $phpStanLevel, $withExtensions ?
'with extensions' : 'without extensions'));
$this->symfonyStyle->section(sprintf('Running PHPStan level %d %s', $phpStanLevel, $isBare ?
'without extensions' : 'with extensions'));

$levelResult = $this->measureErrorCountInLevel($phpStanLevel, $projectDirectory, $envVariables);
$levelResults[] = $levelResult;
Expand All @@ -104,11 +108,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->symfonyStyle->newLine();
}

if ($withExtensions === false) {
if ($isBare) {
// restore PHPStan extension file
$this->symfonyStyle->writeln('Restoring PHPStan extensions...');
$this->symfonyStyle->newLine();

// restore PHPStan extension file
$phpstanExtensionFile = $projectDirectory . '/vendor/phpstan/extension-installer/src/GeneratedConfig.php';
rename($phpstanExtensionFile . '.bak', $phpstanExtensionFile);
}
Expand All @@ -117,6 +121,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// 3. tidy up temporary config
unlink($projectDirectory . '/phpstan-bodyscan.neon');
unlink($projectDirectory . '/phpstan-bodyscan.php');

if ($isJson) {
$this->jsonOutputFormatter->outputResult($bodyscanResult);
Expand Down
3 changes: 3 additions & 0 deletions src/PHPStanConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ private function createBasicPHPStanConfiguration(string $projectDirectory): arra
];
}

/**
* @param array<string, mixed> $phpstanConfiguration
*/
private function dumpToNeon(array $phpstanConfiguration): string
{
$encodedNeon = Neon::encode($phpstanConfiguration, true, ' ');
Expand Down
8 changes: 6 additions & 2 deletions src/ValueObject/PHPStanConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ public function getFileContents(): string
return $this->fileContents;
}

public function getOriginalFileName(): ?string
public function isPHPFile(): bool
{
return $this->originalFileName;
if ($this->originalFileName === null) {
return false;
}

return str_contains($this->originalFileName, '.php');
}
}
1 change: 0 additions & 1 deletion tests/PHPStanConfigFactory/PHPStanConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ protected function setUp(): void
public function test(string $projectDirectory, string $expectedPHPStanConfigFile): void
{
$phpstanNeon = $this->phpStanConfigFactory->create($projectDirectory);

$this->assertStringEqualsFile($expectedPHPStanConfigFile, $phpstanNeon->getFileContents());
}

Expand Down

0 comments on commit a12cce5

Please sign in to comment.