mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-30 07:50:53 +00:00
decouple RectorMetadataResolver, simplify category to package
This commit is contained in:
parent
12b5395c85
commit
ff67ab139e
File diff suppressed because it is too large
Load Diff
|
@ -7,11 +7,22 @@ use Nette\Utils\Json;
|
|||
use Nette\Utils\Strings;
|
||||
use Rector\Contract\Rector\RectorInterface;
|
||||
use Rector\ContributorTools\Contract\OutputFormatterInterface;
|
||||
use Rector\ContributorTools\RectorMetadataResolver;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
final class JsonOutputFormatter implements OutputFormatterInterface
|
||||
{
|
||||
/**
|
||||
* @var RectorMetadataResolver
|
||||
*/
|
||||
private $rectorMetadataResolver;
|
||||
|
||||
public function __construct(RectorMetadataResolver $rectorMetadataResolver)
|
||||
{
|
||||
$this->rectorMetadataResolver = $rectorMetadataResolver;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'json';
|
||||
|
@ -32,9 +43,10 @@ final class JsonOutputFormatter implements OutputFormatterInterface
|
|||
|
||||
$rectorData[] = [
|
||||
'class' => get_class($rector),
|
||||
'package' => $this->rectorMetadataResolver->resolvePackageFromRectorClass(get_class($rector)),
|
||||
'tags' => $this->createTagsFromClass(get_class($rector)),
|
||||
'description' => $rectorConfiguration->getDescription(),
|
||||
'code_samples' => $this->prepareCodeSamples($rectorConfiguration),
|
||||
'code_samples' => $this->resolveCodeSamples($rectorConfiguration),
|
||||
'is_configurable' => $this->resolveIsConfigurable($rectorConfiguration),
|
||||
];
|
||||
}
|
||||
|
@ -77,7 +89,7 @@ final class JsonOutputFormatter implements OutputFormatterInterface
|
|||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
private function prepareCodeSamples(RectorDefinition $rectorDefinition): array
|
||||
private function resolveCodeSamples(RectorDefinition $rectorDefinition): array
|
||||
{
|
||||
$codeSamplesData = [];
|
||||
foreach ($rectorDefinition->getCodeSamples() as $codeSample) {
|
||||
|
|
|
@ -7,7 +7,7 @@ use Rector\ConsoleDiffer\MarkdownDifferAndFormatter;
|
|||
use Rector\Contract\Rector\RectorInterface;
|
||||
use Rector\Contract\RectorDefinition\CodeSampleInterface;
|
||||
use Rector\ContributorTools\Contract\OutputFormatterInterface;
|
||||
use Rector\Exception\ShouldNotHappenException;
|
||||
use Rector\ContributorTools\RectorMetadataResolver;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
@ -24,10 +24,19 @@ final class MarkdownOutputFormatter implements OutputFormatterInterface
|
|||
*/
|
||||
private $markdownDifferAndFormatter;
|
||||
|
||||
public function __construct(SymfonyStyle $symfonyStyle, MarkdownDifferAndFormatter $markdownDifferAndFormatter)
|
||||
{
|
||||
/**
|
||||
* @var RectorMetadataResolver
|
||||
*/
|
||||
private $rectorMetadataResolver;
|
||||
|
||||
public function __construct(
|
||||
SymfonyStyle $symfonyStyle,
|
||||
MarkdownDifferAndFormatter $markdownDifferAndFormatter,
|
||||
RectorMetadataResolver $rectorMetadataResolver
|
||||
) {
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
$this->markdownDifferAndFormatter = $markdownDifferAndFormatter;
|
||||
$this->rectorMetadataResolver = $rectorMetadataResolver;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
|
@ -41,7 +50,9 @@ final class MarkdownOutputFormatter implements OutputFormatterInterface
|
|||
*/
|
||||
public function format(array $genericRectors, array $packageRectors): void
|
||||
{
|
||||
$this->symfonyStyle->writeln('# All Rectors Overview');
|
||||
$totalRectorCount = count($genericRectors) + count($packageRectors);
|
||||
|
||||
$this->symfonyStyle->writeln(sprintf('# All %d Rectors Overview', $totalRectorCount));
|
||||
$this->symfonyStyle->newLine();
|
||||
|
||||
$this->symfonyStyle->writeln('- [Projects](#projects)');
|
||||
|
@ -51,26 +62,25 @@ final class MarkdownOutputFormatter implements OutputFormatterInterface
|
|||
$this->symfonyStyle->writeln('## Projects');
|
||||
$this->symfonyStyle->newLine();
|
||||
|
||||
$rectorsByGroup = $this->groupRectorsByCategory($packageRectors);
|
||||
$this->printRectorsByGroup($rectorsByGroup);
|
||||
$this->printRectors($packageRectors);
|
||||
|
||||
$this->symfonyStyle->writeln('---');
|
||||
|
||||
$this->symfonyStyle->writeln('## General');
|
||||
$this->symfonyStyle->newLine();
|
||||
|
||||
$rectorsByGroup = $this->groupRectorsByCategory($genericRectors);
|
||||
$this->printRectorsByGroup($rectorsByGroup);
|
||||
$this->printRectors($genericRectors);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RectorInterface[][] $rectorsByGroup
|
||||
* @param RectorInterface[] $rectors
|
||||
*/
|
||||
private function printRectorsByGroup(array $rectorsByGroup): void
|
||||
private function printRectors(array $rectors): void
|
||||
{
|
||||
$this->printGroupsMenu($rectorsByGroup);
|
||||
$groupedRectors = $this->groupRectorsByPackage($rectors);
|
||||
$this->printGroupsMenu($groupedRectors);
|
||||
|
||||
foreach ($rectorsByGroup as $group => $rectors) {
|
||||
foreach ($groupedRectors as $group => $rectors) {
|
||||
$this->symfonyStyle->writeln('## ' . $group);
|
||||
$this->symfonyStyle->newLine();
|
||||
|
||||
|
@ -138,43 +148,18 @@ final class MarkdownOutputFormatter implements OutputFormatterInterface
|
|||
* @param RectorInterface[] $rectors
|
||||
* @return RectorInterface[][]
|
||||
*/
|
||||
private function groupRectorsByCategory(array $rectors): array
|
||||
private function groupRectorsByPackage(array $rectors): array
|
||||
{
|
||||
$rectorsByGroup = [];
|
||||
$rectorsByPackage = [];
|
||||
foreach ($rectors as $rector) {
|
||||
$rectorCategory = $this->resolveCategoryFromRectorClass(get_class($rector));
|
||||
$rectorsByGroup[$rectorCategory][] = $rector;
|
||||
$package = $this->rectorMetadataResolver->resolvePackageFromRectorClass(get_class($rector));
|
||||
$rectorsByPackage[$package][] = $rector;
|
||||
}
|
||||
|
||||
// sort groups by name to make them more readable
|
||||
ksort($rectorsByGroup);
|
||||
ksort($rectorsByPackage);
|
||||
|
||||
return $rectorsByGroup;
|
||||
}
|
||||
|
||||
private function resolveCategoryFromRectorClass(string $rectorClass): string
|
||||
{
|
||||
$rectorClassParts = explode('\\', $rectorClass);
|
||||
|
||||
// basic Rectors
|
||||
if (Strings::startsWith($rectorClass, 'Rector\Rector\\')) {
|
||||
return $rectorClassParts[count($rectorClassParts) - 2];
|
||||
}
|
||||
|
||||
// Rector/<PackageGroup>/Rector/SomeRector
|
||||
if (count($rectorClassParts) === 4) {
|
||||
return $rectorClassParts[1];
|
||||
}
|
||||
|
||||
// Rector/<PackageGroup>/Rector/<PackageSubGroup>/SomeRector
|
||||
if (count($rectorClassParts) === 5) {
|
||||
return $rectorClassParts[1] . '\\' . $rectorClassParts[3];
|
||||
}
|
||||
|
||||
throw new ShouldNotHappenException(sprintf(
|
||||
'Failed to resolve group from Rector class. Implement a new one in %s',
|
||||
__METHOD__
|
||||
));
|
||||
return $rectorsByPackage;
|
||||
}
|
||||
|
||||
private function printConfiguration(RectorInterface $rector, CodeSampleInterface $codeSample): void
|
||||
|
|
21
packages/ContributorTools/src/RectorMetadataResolver.php
Normal file
21
packages/ContributorTools/src/RectorMetadataResolver.php
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\ContributorTools;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
|
||||
final class RectorMetadataResolver
|
||||
{
|
||||
public function resolvePackageFromRectorClass(string $rectorClass): string
|
||||
{
|
||||
$rectorClassParts = explode('\\', $rectorClass);
|
||||
|
||||
// basic Rectors
|
||||
if (Strings::startsWith($rectorClass, 'Rector\Rector\\')) {
|
||||
return 'Core';
|
||||
}
|
||||
|
||||
// Rector/<PackageGroup>/Rector/SomeRector
|
||||
return $rectorClassParts[1];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user