mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
render configuratoin to configurabe rectors as well
This commit is contained in:
parent
9c060dfd15
commit
7667c5230a
@ -962,7 +962,6 @@ Turns old method call with specfici type to new one with arguments
|
||||
```diff
|
||||
$serviceDefinition = new Nette\DI\ServiceDefinition;
|
||||
-$serviceDefinition->setInject();
|
||||
-$END
|
||||
+$serviceDefinition->addTag('inject');
|
||||
```
|
||||
|
||||
|
@ -31,8 +31,11 @@ final class GetMockRector extends AbstractPHPUnitRector
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Turns getMock*() methods to createMock()', [
|
||||
new CodeSample('$this->getMock("Class")', '$this->createMock("Class")'),
|
||||
new CodeSample('$this->getMockWithoutInvokingTheOriginalConstructor("Class")', '$this->createMock("Class"'),
|
||||
new CodeSample('$this->getMock("Class");', '$this->createMock("Class");'),
|
||||
new CodeSample(
|
||||
'$this->getMockWithoutInvokingTheOriginalConstructor("Class");',
|
||||
'$this->createMock("Class");'
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -7,12 +7,13 @@ use Nette\Utils\Strings;
|
||||
use Rector\Console\ConsoleStyle;
|
||||
use Rector\ConsoleDiffer\MarkdownDifferAndFormatter;
|
||||
use Rector\Contract\Rector\RectorInterface;
|
||||
use Rector\Contract\RectorDefinition\CodeSampleInterface;
|
||||
use Rector\Exception\ShouldNotHappenException;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use ReflectionClass;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symplify\PackageBuilder\Console\Command\CommandNaming;
|
||||
|
||||
final class GenerateRectorOverviewCommand extends Command
|
||||
@ -101,40 +102,35 @@ final class GenerateRectorOverviewCommand extends Command
|
||||
$this->consoleStyle->writeln($rectorDefinition->getDescription());
|
||||
}
|
||||
|
||||
foreach ($rectorDefinition->getCodeSamples() as $codeSample) {
|
||||
$this->consoleStyle->newLine();
|
||||
$this->consoleStyle->writeln('```diff');
|
||||
|
||||
[$codeBefore, $codeAfter] = $this->joinBeforeAndAfter($rectorDefinition->getCodeSamples());
|
||||
$diff = $this->markdownDifferAndFormatter->bareDiffAndFormatWithoutColors($codeBefore, $codeAfter);
|
||||
$this->consoleStyle->write($diff);
|
||||
if ($codeSample instanceof ConfiguredCodeSample) {
|
||||
$configuration = [
|
||||
'services' => [
|
||||
get_class($rector) => $codeSample->getConfiguration(),
|
||||
],
|
||||
];
|
||||
|
||||
$configuration = Yaml::dump($configuration, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
|
||||
|
||||
$this->printCodeWrapped($configuration, 'yaml');
|
||||
|
||||
$this->consoleStyle->newLine();
|
||||
$this->consoleStyle->writeln('```');
|
||||
$this->consoleStyle->writeln('↓');
|
||||
$this->consoleStyle->newLine();
|
||||
}
|
||||
|
||||
$diff = $this->markdownDifferAndFormatter->bareDiffAndFormatWithoutColors(
|
||||
$codeSample->getCodeBefore(),
|
||||
$codeSample->getCodeAfter()
|
||||
);
|
||||
$this->printCodeWrapped($diff, 'diff');
|
||||
}
|
||||
|
||||
$this->consoleStyle->newLine(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CodeSampleInterface[] $codeSamples
|
||||
* @return string[]
|
||||
*/
|
||||
private function joinBeforeAndAfter(array $codeSamples): array
|
||||
{
|
||||
$separator = PHP_EOL . PHP_EOL;
|
||||
|
||||
$codesBefore = [];
|
||||
$codesAfter = [];
|
||||
foreach ($codeSamples as $codeSample) {
|
||||
$codesBefore[] = $codeSample->getCodeBefore();
|
||||
$codesAfter[] = $codeSample->getCodeAfter();
|
||||
}
|
||||
|
||||
$codeBefore = implode($separator, $codesBefore);
|
||||
$codeAfter = implode($separator, $codesAfter);
|
||||
|
||||
return [$codeBefore, $codeAfter];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RectorInterface[] $rectors
|
||||
* @return RectorInterface[][]
|
||||
@ -147,6 +143,7 @@ final class GenerateRectorOverviewCommand extends Command
|
||||
$rectorsByGroup[$rectorGroup][] = $rector;
|
||||
}
|
||||
|
||||
// sort groups by name to make them more readable
|
||||
ksort($rectorsByGroup);
|
||||
|
||||
return $rectorsByGroup;
|
||||
@ -157,12 +154,12 @@ final class GenerateRectorOverviewCommand extends Command
|
||||
$rectorClassParts = explode('\\', $rectorClass);
|
||||
|
||||
// basic Rectors
|
||||
if (Strings::match($rectorClass, '#^Rector\\\\Rector\\\\#')) {
|
||||
if (Strings::startsWith($rectorClass, 'Rector\Rector\\')) {
|
||||
return $rectorClassParts[count($rectorClassParts) - 2];
|
||||
}
|
||||
|
||||
// Yaml
|
||||
if (Strings::match($rectorClass, '#^Rector\\\\YamlRector\\\\#')) {
|
||||
if (Strings::startsWith($rectorClass, 'Rector\YamlRector\\')) {
|
||||
return 'Yaml';
|
||||
}
|
||||
|
||||
@ -185,7 +182,7 @@ final class GenerateRectorOverviewCommand extends Command
|
||||
/**
|
||||
* @param RectorInterface[][] $rectorsByGroup
|
||||
*/
|
||||
private function printMenu(array $rectorsByGroup): void
|
||||
private function printGroupsMenu(array $rectorsByGroup): void
|
||||
{
|
||||
foreach ($rectorsByGroup as $group => $rectors) {
|
||||
$escapedGroup = str_replace('\\', '', $group);
|
||||
@ -252,7 +249,7 @@ final class GenerateRectorOverviewCommand extends Command
|
||||
*/
|
||||
private function printRectorsByGroup(array $rectorsByGroup): void
|
||||
{
|
||||
$this->printMenu($rectorsByGroup);
|
||||
$this->printGroupsMenu($rectorsByGroup);
|
||||
|
||||
foreach ($rectorsByGroup as $group => $rectors) {
|
||||
$this->consoleStyle->writeln('## ' . $group);
|
||||
@ -263,4 +260,12 @@ final class GenerateRectorOverviewCommand extends Command
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $configuration
|
||||
*/
|
||||
private function printCodeWrapped($configuration, string $format): void
|
||||
{
|
||||
$this->consoleStyle->writeln(sprintf('```%s%s%s%s```', $format, PHP_EOL, $configuration, PHP_EOL));
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ final class NamespaceReplacerRector extends AbstractRector
|
||||
'$someObject = new SomeOldNamespace\SomeClass;',
|
||||
'$someObject = new SomeNewNamespace\SomeClass;',
|
||||
[
|
||||
'oldToNewNamespaces' => [
|
||||
'$oldToNewNamespaces' => [
|
||||
'SomeOldNamespace' => 'SomeNewNamespace',
|
||||
],
|
||||
]
|
||||
|
@ -76,7 +76,6 @@ final class MethodCallToAnotherMethodCallWithArgumentsRector extends AbstractRec
|
||||
<<<'CODE_SAMPLE'
|
||||
$serviceDefinition = new Nette\DI\ServiceDefinition;
|
||||
$serviceDefinition->setInject();
|
||||
$END
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
|
Loading…
x
Reference in New Issue
Block a user