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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Rector\Monitor\Analyze\RuleProcessor\MetafileProcessor;

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\MissingECSSetsMetafileProcessor\MissingECSSetsMetafileProcessorTest
*/
final readonly class MissingECSSetsMetafileProcessor implements RuleProcessorInterface
{
public function process(
MonitorConfig $monitorConfig,
ComposerJson $composerJson,
ErrorCollector $errorCollector
): void {
$requiredECSSets = $monitorConfig->getRequiredECSSets();
if ($requiredECSSets === []) {
return;
}

foreach ($monitorConfig->getRepositoryCollection()->all() as $repository) {
foreach ($repository->getRootFiles() as $repositoryRootFile) {
if ($repositoryRootFile->getFilename() !== 'ecs.php') {
continue;
}

$configuredECSSets = $this->resolveConfiguredECSSets($repositoryRootFile->getContents());

foreach ($requiredECSSets as $requiredECSSet) {
if (in_array($requiredECSSet, $configuredECSSets, true)) {
continue;
}

$errorCollector->addErrorMessage(sprintf(
' * ECS set "<fg=yellow>%s</>" is <fg=red>missing</> in "<fg=green>%s</>". Add it via "->withPreparedSets(%s: true)"',
$requiredECSSet,
$repositoryRootFile->getFilename(),
$requiredECSSet
));
}
}
}
}

/**
* @return string[]
*/
private function resolveConfiguredECSSets(string $ecsContents): array
{
if (preg_match('#withPreparedSets\s*\((?<arguments>[^)]*)\)#s', $ecsContents, $match) !== 1) {
return [];
}

if (preg_match_all('#(?<name>\w+)\s*:\s*true\b#', $match['arguments'], $argumentMatches) === false) {
return [];
}

return $argumentMatches['name'];
}
}
26 changes: 26 additions & 0 deletions src/Config/MonitorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ final class MonitorConfig
*/
private array $minPackagesVersions = [];

/**
* @var string[]
*/
private array $requiredECSSets = [];

private bool $noPHPStanBaseline = false;

private ?int $minPHPStanLevel = null;
Expand Down Expand Up @@ -169,6 +174,27 @@ public function getMinPHPStanLevel(): ?int
return $this->minPHPStanLevel;
}

/**
* @api to be used
* @param string[] $ecsSetNames
*/
public function requireECSSets(array $ecsSetNames): self
{
Assert::allString($ecsSetNames);

$this->requiredECSSets = array_merge($this->requiredECSSets, $ecsSetNames);

return $this;
}

/**
* @return string[]
*/
public function getRequiredECSSets(): array
{
return $this->requiredECSSets;
}

/**
* @return string[]
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withPaths([__DIR__ . '/src'])
->withPreparedSets(psr12: true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withPaths([__DIR__ . '/src'])
->withPreparedSets(common: true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

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

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

final class MissingECSSetsMetafileProcessorTest extends AbstractTestCase
{
private MissingECSSetsMetafileProcessor $missingECSSetsMetafileProcessor;

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

$this->missingECSSetsMetafileProcessor = $this->make(MissingECSSetsMetafileProcessor::class);
}

public function testReportsMissingSet(): void
{
$monitorConfig = $this->createMonitorConfigWithRepository(__DIR__ . '/Fixture/without-psr12/ecs.php');
$monitorConfig->requireECSSets(['psr12']);

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

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

public function testSkipsWhenSetIsConfigured(): void
{
$monitorConfig = $this->createMonitorConfigWithRepository(__DIR__ . '/Fixture/with-psr12/ecs.php');
$monitorConfig->requireECSSets(['psr12']);

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

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

public function testSkipsWhenNoRequiredSetsConfigured(): void
{
$monitorConfig = $this->createMonitorConfigWithRepository(__DIR__ . '/Fixture/without-psr12/ecs.php');

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

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

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

$repository = $monitorConfig->getRepositoryCollection()
->all()[0];
$repository->decorateRootFiles([new SplFileInfo($ecsConfigPath, '', 'ecs.php')]);

return $monitorConfig;
}
}
Loading