Skip to content
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ return MonitorConfig::configure()
->minPackageVersion('rector/rector', '2.2')

// other rules
->noPhpstanBaseline();
->noPhpstanBaseline()
->minPHPStanLevel(8);
```

<br>
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"shipmonk/composer-dependency-analyser": "^1.8",
"symplify/phpstan-extensions": "^12.0",
"symplify/phpstan-rules": "^14.10",
"tomasvotruba/class-leak": "^2.1",
"tomasvotruba/unused-public": "^2.2",
"tracy/tracy": "^2.12"
"tracy/tracy": "^2.12",
"tomasvotruba/class-leak": "^2.1.3"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@
->minPackageVersion('rector/rector', '2.0')

// other rules
->noPhpstanBaseline();
->noPhpstanBaseline()
->minPHPStanLevel(8);
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Rector\Monitor\Analyze\RuleProcessor\MetafileProcessor;

use Nette\Neon\Neon;
use Rector\Monitor\Analyze\Contract\RuleProcessorInterface;
use Rector\Monitor\Analyze\Reporting\ErrorCollector;
use Rector\Monitor\Config\MonitorConfig;
use Rector\Monitor\ValueObject\ComposerJson;

/**
* @see \Rector\Monitor\Tests\Analyze\RuleProcessor\MetafileProcessor\TooLowPHPStanLevelMetafileProcessor\TooLowPHPStanLevelMetafileProcessorTest
*/
final readonly class TooLowPHPStanLevelMetafileProcessor implements RuleProcessorInterface
{
public function process(
MonitorConfig $monitorConfig,
ComposerJson $composerJson,
ErrorCollector $errorCollector
): void {
$minPHPStanLevel = $monitorConfig->getMinPHPStanLevel();
if ($minPHPStanLevel === null) {
return;
}

foreach ($monitorConfig->getRepositoryCollection()->all() as $repository) {
foreach ($repository->getRootFiles() as $repositoryRootFile) {
if (! str_contains($repositoryRootFile->getFilename(), 'phpstan')) {
continue;
}

if (! str_ends_with($repositoryRootFile->getFilename(), '.neon')) {
continue;
}

if (str_contains($repositoryRootFile->getFilename(), 'baseline')) {
continue;
}

$phpstanLevel = $this->resolvePHPStanLevel($repositoryRootFile->getContents());
if ($phpstanLevel === null) {
continue;
}

if ($phpstanLevel >= $minPHPStanLevel) {
continue;
}

$errorCollector->addErrorMessage(sprintf(
' * PHPStan level in "<fg=green>%s</>" is <fg=red>too low</>. Current level "<fg=yellow>%d</>", should be at least "<fg=yellow>%d</>"',
$repositoryRootFile->getFilename(),
$phpstanLevel,
$minPHPStanLevel
));
}
}
}

private function resolvePHPStanLevel(string $neonContents): ?int
{
$decoded = Neon::decode($neonContents);
if (! is_array($decoded)) {
return null;
}

$level = $decoded['parameters']['level'] ?? null;
if ($level === null) {
return null;
}

if ($level === 'max') {
return 10;
}

if (is_int($level)) {
return $level;
}

if (is_string($level) && preg_match('#^\d+$#', $level) === 1) {
return (int) $level;
}

return null;
}
}
16 changes: 16 additions & 0 deletions src/Config/MonitorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ final class MonitorConfig

private bool $noPHPStanBaseline = false;

private ?int $minPHPStanLevel = null;

private ?RepositoryCollection $repositoryCollection = null;

public static function configure(): self
Expand Down Expand Up @@ -153,6 +155,20 @@ public function isNoPHPStanBaseline(): bool
return $this->noPHPStanBaseline;
}

public function minPHPStanLevel(int $minPHPStanLevel): self
{
Assert::range($minPHPStanLevel, 0, 10);

$this->minPHPStanLevel = $minPHPStanLevel;

return $this;
}

public function getMinPHPStanLevel(): ?int
{
return $this->minPHPStanLevel;
}

/**
* @return string[]
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Helper/SymfonyColumnStyler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public static function styleHighsAndLows(array $tableRow): array
$secondVersion
)
);

$uniqueValues = array_unique($stringValues);
$highValue = array_shift($stringValues);
$lowValue = array_pop($stringValues);

Expand Down
1 change: 1 addition & 0 deletions templates/monitor.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ return MonitorConfig::configure()

// other rules
// ->noPhpstanBaseline()
// ->minPHPStanLevel(8)
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
level: max
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
level: 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace Rector\Monitor\Tests\Analyze\RuleProcessor\MetafileProcessor\TooLowPHPStanLevelMetafileProcessor;

use Override;
use Rector\Monitor\Analyze\Reporting\ErrorCollector;
use Rector\Monitor\Analyze\RuleProcessor\MetafileProcessor\TooLowPHPStanLevelMetafileProcessor;
use Rector\Monitor\Config\MonitorConfig;
use Rector\Monitor\Tests\AbstractTestCase;
use Rector\Monitor\ValueObject\ComposerJson;
use Symfony\Component\Finder\SplFileInfo;

final class TooLowPHPStanLevelMetafileProcessorTest extends AbstractTestCase
{
private TooLowPHPStanLevelMetafileProcessor $tooLowPHPStanLevelMetafileProcessor;

#[Override]
protected function setUp(): void
{
parent::setUp();

$this->tooLowPHPStanLevelMetafileProcessor = $this->make(TooLowPHPStanLevelMetafileProcessor::class);
}

public function testReportsTooLowLevel(): void
{
$monitorConfig = $this->createMonitorConfigWithRepository(__DIR__ . '/Fixture/phpstan-too-low.neon');
$monitorConfig->minPHPStanLevel(8);

$errorCollector = new ErrorCollector();
$this->tooLowPHPStanLevelMetafileProcessor->process(
$monitorConfig,
new ComposerJson('https://example.com/some/repo.git', []),
$errorCollector
);

$errorMessages = $errorCollector->getErrorMessages();
$this->assertCount(1, $errorMessages);
$this->assertStringContainsString('too low', $errorMessages[0]);
}

public function testSkipsWhenLevelIsHighEnough(): void
{
$monitorConfig = $this->createMonitorConfigWithRepository(__DIR__ . '/Fixture/phpstan-high.neon');
$monitorConfig->minPHPStanLevel(8);

$errorCollector = new ErrorCollector();
$this->tooLowPHPStanLevelMetafileProcessor->process(
$monitorConfig,
new ComposerJson('https://example.com/some/repo.git', []),
$errorCollector
);

$this->assertEmpty($errorCollector->getErrorMessages());
}

public function testSkipsWhenNoMinLevelConfigured(): void
{
$monitorConfig = $this->createMonitorConfigWithRepository(__DIR__ . '/Fixture/phpstan-too-low.neon');

$errorCollector = new ErrorCollector();
$this->tooLowPHPStanLevelMetafileProcessor->process(
$monitorConfig,
new ComposerJson('https://example.com/some/repo.git', []),
$errorCollector
);

$this->assertEmpty($errorCollector->getErrorMessages());
}

private function createMonitorConfigWithRepository(string $phpstanNeonPath): MonitorConfig
{
$monitorConfig = new MonitorConfig();
$monitorConfig->addRepositories(['https://example.com/some/repo.git']);

$repository = $monitorConfig->getRepositoryCollection()
->all()[0];
$repository->decorateRootFiles([new SplFileInfo($phpstanNeonPath, '', basename($phpstanNeonPath))]);

return $monitorConfig;
}
}
Loading