render configuratoin to configurabe rectors as well

This commit is contained in:
Tomas Votruba 2018-08-01 17:37:39 +02:00
parent 9c060dfd15
commit 7667c5230a
6 changed files with 1409 additions and 35 deletions

View File

@ -962,7 +962,6 @@ Turns old method call with specfici type to new one with arguments
```diff ```diff
$serviceDefinition = new Nette\DI\ServiceDefinition; $serviceDefinition = new Nette\DI\ServiceDefinition;
-$serviceDefinition->setInject(); -$serviceDefinition->setInject();
-$END
+$serviceDefinition->addTag('inject'); +$serviceDefinition->addTag('inject');
``` ```

View File

@ -31,8 +31,11 @@ final class GetMockRector extends AbstractPHPUnitRector
public function getDefinition(): RectorDefinition public function getDefinition(): RectorDefinition
{ {
return new RectorDefinition('Turns getMock*() methods to createMock()', [ return new RectorDefinition('Turns getMock*() methods to createMock()', [
new CodeSample('$this->getMock("Class")', '$this->createMock("Class")'), new CodeSample('$this->getMock("Class");', '$this->createMock("Class");'),
new CodeSample('$this->getMockWithoutInvokingTheOriginalConstructor("Class")', '$this->createMock("Class"'), new CodeSample(
'$this->getMockWithoutInvokingTheOriginalConstructor("Class");',
'$this->createMock("Class");'
),
]); ]);
} }

View File

@ -7,12 +7,13 @@ use Nette\Utils\Strings;
use Rector\Console\ConsoleStyle; use Rector\Console\ConsoleStyle;
use Rector\ConsoleDiffer\MarkdownDifferAndFormatter; use Rector\ConsoleDiffer\MarkdownDifferAndFormatter;
use Rector\Contract\Rector\RectorInterface; use Rector\Contract\Rector\RectorInterface;
use Rector\Contract\RectorDefinition\CodeSampleInterface;
use Rector\Exception\ShouldNotHappenException; use Rector\Exception\ShouldNotHappenException;
use Rector\RectorDefinition\ConfiguredCodeSample;
use ReflectionClass; use ReflectionClass;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Yaml;
use Symplify\PackageBuilder\Console\Command\CommandNaming; use Symplify\PackageBuilder\Console\Command\CommandNaming;
final class GenerateRectorOverviewCommand extends Command final class GenerateRectorOverviewCommand extends Command
@ -101,38 +102,33 @@ final class GenerateRectorOverviewCommand extends Command
$this->consoleStyle->writeln($rectorDefinition->getDescription()); $this->consoleStyle->writeln($rectorDefinition->getDescription());
} }
$this->consoleStyle->newLine(); foreach ($rectorDefinition->getCodeSamples() as $codeSample) {
$this->consoleStyle->writeln('```diff'); $this->consoleStyle->newLine();
[$codeBefore, $codeAfter] = $this->joinBeforeAndAfter($rectorDefinition->getCodeSamples()); if ($codeSample instanceof ConfiguredCodeSample) {
$diff = $this->markdownDifferAndFormatter->bareDiffAndFormatWithoutColors($codeBefore, $codeAfter); $configuration = [
$this->consoleStyle->write($diff); 'services' => [
get_class($rector) => $codeSample->getConfiguration(),
],
];
$this->consoleStyle->newLine(); $configuration = Yaml::dump($configuration, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
$this->consoleStyle->writeln('```');
$this->consoleStyle->newLine(1); $this->printCodeWrapped($configuration, 'yaml');
}
/** $this->consoleStyle->newLine();
* @param CodeSampleInterface[] $codeSamples $this->consoleStyle->writeln('↓');
* @return string[] $this->consoleStyle->newLine();
*/ }
private function joinBeforeAndAfter(array $codeSamples): array
{
$separator = PHP_EOL . PHP_EOL;
$codesBefore = []; $diff = $this->markdownDifferAndFormatter->bareDiffAndFormatWithoutColors(
$codesAfter = []; $codeSample->getCodeBefore(),
foreach ($codeSamples as $codeSample) { $codeSample->getCodeAfter()
$codesBefore[] = $codeSample->getCodeBefore(); );
$codesAfter[] = $codeSample->getCodeAfter(); $this->printCodeWrapped($diff, 'diff');
} }
$codeBefore = implode($separator, $codesBefore); $this->consoleStyle->newLine(1);
$codeAfter = implode($separator, $codesAfter);
return [$codeBefore, $codeAfter];
} }
/** /**
@ -147,6 +143,7 @@ final class GenerateRectorOverviewCommand extends Command
$rectorsByGroup[$rectorGroup][] = $rector; $rectorsByGroup[$rectorGroup][] = $rector;
} }
// sort groups by name to make them more readable
ksort($rectorsByGroup); ksort($rectorsByGroup);
return $rectorsByGroup; return $rectorsByGroup;
@ -157,12 +154,12 @@ final class GenerateRectorOverviewCommand extends Command
$rectorClassParts = explode('\\', $rectorClass); $rectorClassParts = explode('\\', $rectorClass);
// basic Rectors // basic Rectors
if (Strings::match($rectorClass, '#^Rector\\\\Rector\\\\#')) { if (Strings::startsWith($rectorClass, 'Rector\Rector\\')) {
return $rectorClassParts[count($rectorClassParts) - 2]; return $rectorClassParts[count($rectorClassParts) - 2];
} }
// Yaml // Yaml
if (Strings::match($rectorClass, '#^Rector\\\\YamlRector\\\\#')) { if (Strings::startsWith($rectorClass, 'Rector\YamlRector\\')) {
return 'Yaml'; return 'Yaml';
} }
@ -185,7 +182,7 @@ final class GenerateRectorOverviewCommand extends Command
/** /**
* @param RectorInterface[][] $rectorsByGroup * @param RectorInterface[][] $rectorsByGroup
*/ */
private function printMenu(array $rectorsByGroup): void private function printGroupsMenu(array $rectorsByGroup): void
{ {
foreach ($rectorsByGroup as $group => $rectors) { foreach ($rectorsByGroup as $group => $rectors) {
$escapedGroup = str_replace('\\', '', $group); $escapedGroup = str_replace('\\', '', $group);
@ -252,7 +249,7 @@ final class GenerateRectorOverviewCommand extends Command
*/ */
private function printRectorsByGroup(array $rectorsByGroup): void private function printRectorsByGroup(array $rectorsByGroup): void
{ {
$this->printMenu($rectorsByGroup); $this->printGroupsMenu($rectorsByGroup);
foreach ($rectorsByGroup as $group => $rectors) { foreach ($rectorsByGroup as $group => $rectors) {
$this->consoleStyle->writeln('## ' . $group); $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));
}
} }

View File

@ -38,7 +38,7 @@ final class NamespaceReplacerRector extends AbstractRector
'$someObject = new SomeOldNamespace\SomeClass;', '$someObject = new SomeOldNamespace\SomeClass;',
'$someObject = new SomeNewNamespace\SomeClass;', '$someObject = new SomeNewNamespace\SomeClass;',
[ [
'oldToNewNamespaces' => [ '$oldToNewNamespaces' => [
'SomeOldNamespace' => 'SomeNewNamespace', 'SomeOldNamespace' => 'SomeNewNamespace',
], ],
] ]

View File

@ -76,7 +76,6 @@ final class MethodCallToAnotherMethodCallWithArgumentsRector extends AbstractRec
<<<'CODE_SAMPLE' <<<'CODE_SAMPLE'
$serviceDefinition = new Nette\DI\ServiceDefinition; $serviceDefinition = new Nette\DI\ServiceDefinition;
$serviceDefinition->setInject(); $serviceDefinition->setInject();
$END
CODE_SAMPLE CODE_SAMPLE
, ,
<<<'CODE_SAMPLE' <<<'CODE_SAMPLE'

1368
test.md Normal file

File diff suppressed because it is too large Load Diff