Skip to content

Commit

Permalink
Refactor AnnotationExtractor fomr FileDiff value object to standalone…
Browse files Browse the repository at this point in the history
… RectorsChangelogResolver service (#6088)
  • Loading branch information
TomasVotruba authored Apr 10, 2021
1 parent 4587a7a commit f82a8dc
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Iterator;
use PHPUnit\Framework\TestCase;
use Rector\ChangesReporting\Annotation\AnnotationExtractor;
use Rector\Tests\ChangesReporting\ValueObject\Source\RectorWithChangelog;
use Rector\Tests\ChangesReporting\ValueObject\Source\RectorWithOutChangelog;
use Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source\RectorWithChangelog;
use Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source\RectorWithOutChangelog;

final class AnnotationExtractorTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver;

use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
use Rector\ChangesReporting\ValueObject\RectorWithFileAndLineChange;
use Rector\Core\HttpKernel\RectorKernel;
use Rector\Core\ValueObject\Reporting\FileDiff;
use Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source\RectorWithChangelog;
use Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source\RectorWithOutChangelog;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class RectorsChangelogResolverTest extends AbstractKernelTestCase
{
/**
* @var RectorsChangelogResolver
*/
private $rectorsChangelogResolver;

/**
* @var FileDiff
*/
private $fileDiff;

protected function setUp(): void
{
$this->bootKernel(RectorKernel::class);
$this->rectorsChangelogResolver = $this->getService(RectorsChangelogResolver::class);

$this->fileDiff = $this->createFileDiff();
}

public function test(): void
{
$rectorsChangelogs = $this->rectorsChangelogResolver->resolve($this->fileDiff->getRectorClasses());

$expectedRectorsChangelogs = [
RectorWithChangelog::class => 'https://github.com/rectorphp/rector/blob/master/docs/rector_rules_overview.md',
];
$this->assertSame($expectedRectorsChangelogs, $rectorsChangelogs);
}

private function createFileDiff(): FileDiff
{
// This is by intention to test the array_unique functionality
$rectorWithFileAndLineChange1 = new RectorWithFileAndLineChange(
new RectorWithChangelog(),
__DIR__ . '/Source/RectorWithChangelog.php',
1
);

$rectorWithFileAndLineChange2 = new RectorWithFileAndLineChange(
new RectorWithChangelog(),
__DIR__ . '/Source/RectorWithChangelog.php',
1
);

$rectorWithFileAndLineChange3 = new RectorWithFileAndLineChange(
new RectorWithOutChangelog(),
__DIR__ . '/Source/RectorWithOutChangelog.php',
1
);

$rectorWithFileAndLineChanges = [
$rectorWithFileAndLineChange1,
$rectorWithFileAndLineChange2,
$rectorWithFileAndLineChange3,
];

return new FileDiff(new SmartFileInfo(__FILE__), 'foo', 'foo', $rectorWithFileAndLineChanges);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);

declare(strict_types=1);

namespace Rector\Tests\ChangesReporting\ValueObject\Source;
namespace Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source;

use Rector\Core\Contract\Rector\RectorInterface;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
declare(strict_types=1);

declare(strict_types=1);

namespace Rector\Tests\ChangesReporting\ValueObject\Source;
namespace Rector\Tests\ChangesReporting\Annotation\AppliedRectorsChangelogResolver\Source;

use Rector\Core\Contract\Rector\RectorInterface;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down
74 changes: 0 additions & 74 deletions packages-tests/ChangesReporting/ValueObject/FileDiffTest.php

This file was deleted.

This file was deleted.

5 changes: 3 additions & 2 deletions packages/ChangesReporting/Annotation/AnnotationExtractor.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\ChangesReporting\Annotation;

use Nette\Utils\Strings;
use Rector\Core\Contract\Rector\RectorInterface;
use ReflectionClass;

/**
Expand All @@ -12,7 +14,7 @@
final class AnnotationExtractor
{
/**
* @param class-string<object> $className
* @param class-string<RectorInterface> $className
*/
public function extractAnnotationFromClass(string $className, string $annotation): ?string
{
Expand All @@ -26,7 +28,6 @@ public function extractAnnotationFromClass(string $className, string $annotation
// @see https://regex101.com/r/oYGaWU/1
$pattern = '#' . preg_quote($annotation, '#') . '\s+(?<content>.*?)$#m';
$matches = Strings::match($docComment, $pattern);

return $matches['content'] ?? null;
}
}
45 changes: 45 additions & 0 deletions packages/ChangesReporting/Annotation/RectorsChangelogResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Rector\ChangesReporting\Annotation;

use Rector\Core\Contract\Rector\RectorInterface;

final class RectorsChangelogResolver
{
/**
* @var AnnotationExtractor
*/
private $annotationExtractor;

public function __construct(AnnotationExtractor $annotationExtractor)
{
$this->annotationExtractor = $annotationExtractor;
}

/**
* @param array<class-string<RectorInterface>> $rectorClasses
* @return array<class-string, string>
*/
public function resolve(array $rectorClasses): array
{
$rectorClassesToChangelogUrls = $this->resolveIncludingMissing($rectorClasses);
return array_filter($rectorClassesToChangelogUrls);
}

/**
* @param array<class-string<RectorInterface>> $rectorClasses
* @return array<class-string, string|null>
*/
public function resolveIncludingMissing(array $rectorClasses): array
{
$rectorClassesToChangelogUrls = [];
foreach ($rectorClasses as $rectorClass) {
$changelogUrl = $this->annotationExtractor->extractAnnotationFromClass($rectorClass, '@changelog');
$rectorClassesToChangelogUrls[$rectorClass] = $changelogUrl;
}

return $rectorClassesToChangelogUrls;
}
}
29 changes: 23 additions & 6 deletions packages/ChangesReporting/Output/ConsoleOutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Rector\ChangesReporting\Output;

use Nette\Utils\Strings;
use Rector\ChangesReporting\Annotation\AnnotationExtractor;
use Rector\ChangesReporting\Annotation\RectorsChangelogResolver;
use Rector\ChangesReporting\Application\ErrorAndDiffCollector;
use Rector\ChangesReporting\Contract\Output\OutputFormatterInterface;
use Rector\Core\Configuration\Configuration;
Expand Down Expand Up @@ -46,20 +46,20 @@ final class ConsoleOutputFormatter implements OutputFormatterInterface
private $betterStandardPrinter;

/**
* @var AnnotationExtractor
* @var RectorsChangelogResolver
*/
private $annotationExtractor;
private $rectorsChangelogResolver;

public function __construct(
BetterStandardPrinter $betterStandardPrinter,
Configuration $configuration,
SymfonyStyle $symfonyStyle,
AnnotationExtractor $annotationExtractor
RectorsChangelogResolver $rectorsChangelogResolver
) {
$this->symfonyStyle = $symfonyStyle;
$this->betterStandardPrinter = $betterStandardPrinter;
$this->configuration = $configuration;
$this->annotationExtractor = $annotationExtractor;
$this->rectorsChangelogResolver = $rectorsChangelogResolver;
}

public function report(ErrorAndDiffCollector $errorAndDiffCollector): void
Expand Down Expand Up @@ -119,10 +119,12 @@ private function reportFileDiffs(array $fileDiffs): void
$this->symfonyStyle->writeln($fileDiff->getDiffConsoleFormatted());
$this->symfonyStyle->newLine();

$rectorsChangelogsLines = $this->createRectorChangelogLines($fileDiff);

if ($fileDiff->getRectorChanges() !== []) {
$this->symfonyStyle->writeln('<options=underscore>Applied rules:</>');
$this->symfonyStyle->newLine();
$this->symfonyStyle->listing($fileDiff->getRectorClassesWithChangelogUrl($this->annotationExtractor));
$this->symfonyStyle->listing($rectorsChangelogsLines);
$this->symfonyStyle->newLine();
}
}
Expand Down Expand Up @@ -232,4 +234,19 @@ private function createSuccessMessage(ErrorAndDiffCollector $errorAndDiffCollect
$this->configuration->isDryRun() ? 'would have changed (dry-run)' : ($changeCount === 1 ? 'has' : 'have') . ' been changed'
);
}

/**
* @return string[]
*/
private function createRectorChangelogLines(FileDiff $fileDiff): array
{
$rectorsChangelogs = $this->rectorsChangelogResolver->resolveIncludingMissing($fileDiff->getRectorClasses());

$rectorsChangelogsLines = [];
foreach ($rectorsChangelogs as $rectorClass => $changelog) {
$rectorsChangelogsLines[] = $changelog === null ? $rectorClass : $rectorClass . ' ' . $changelog;
}

return $rectorsChangelogsLines;
}
}
Loading

0 comments on commit f82a8dc

Please sign in to comment.