[DX] Update dump nodes to not-use symfony style (#4356)

* [DX] Update dump nodes to not-use symfony style

* [rector] [DX] Update dump nodes to not-use symfony style

* [cs] [DX] Update dump nodes to not-use symfony style

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Tomas Votruba 2020-10-05 11:29:07 +02:00 committed by GitHub
parent 1c8fac5242
commit a8f55f339f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 450 additions and 887 deletions

View File

@ -304,7 +304,7 @@
],
"docs": [
"bin/rector dump-rectors --output-file docs/rector_rules_overview.md --ansi",
"bin/rector dump-nodes > docs/nodes_overview.md"
"bin/rector dump-nodes --output-file docs/nodes_overview.md --ansi"
],
"rector-ci": "bin/rector process --config rector-ci.php --dry-run --ansi",
"rector": "bin/rector process --config rector-ci.php --ansi",

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -6,30 +6,53 @@ namespace Rector\Utils\NodeDocumentationGenerator\Command;
use Rector\Core\Console\Command\AbstractCommand;
use Rector\Utils\NodeDocumentationGenerator\NodeInfosFactory;
use Rector\Utils\NodeDocumentationGenerator\OutputFormatter\MarkdownDumpNodesOutputFormatter;
use Rector\Utils\NodeDocumentationGenerator\Printer\MarkdownNodeInfosPrinter;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symplify\PackageBuilder\Console\Command\CommandNaming;
use Symplify\PackageBuilder\Console\ShellCode;
use Symplify\SmartFileSystem\SmartFileInfo;
use Symplify\SmartFileSystem\SmartFileSystem;
final class DumpNodesCommand extends AbstractCommand
{
/**
* @var MarkdownDumpNodesOutputFormatter
* @var string
*/
private $markdownDumpNodesOutputFormatter;
private const OUTPUT_FILE = 'output-file';
/**
* @var MarkdownNodeInfosPrinter
*/
private $markdownNodeInfosPrinter;
/**
* @var NodeInfosFactory
*/
private $nodeInfosFactory;
/**
* @var SmartFileSystem
*/
private $smartFileSystem;
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
public function __construct(
MarkdownDumpNodesOutputFormatter $markdownDumpNodesOutputFormatter,
NodeInfosFactory $nodeInfosFactory
MarkdownNodeInfosPrinter $markdownNodeInfosPrinter,
NodeInfosFactory $nodeInfosFactory,
SmartFileSystem $smartFileSystem,
SymfonyStyle $symfonyStyle
) {
$this->markdownDumpNodesOutputFormatter = $markdownDumpNodesOutputFormatter;
$this->markdownNodeInfosPrinter = $markdownNodeInfosPrinter;
$this->nodeInfosFactory = $nodeInfosFactory;
$this->smartFileSystem = $smartFileSystem;
$this->symfonyStyle = $symfonyStyle;
parent::__construct();
}
@ -38,12 +61,32 @@ final class DumpNodesCommand extends AbstractCommand
{
$this->setName(CommandNaming::classToName(self::class));
$this->setDescription('[DOCS] Dump overview of all Nodes');
$this->addOption(
self::OUTPUT_FILE,
null,
InputOption::VALUE_REQUIRED,
'Where to output the file',
getcwd() . '/docs/nodes_overview.md'
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$outputFile = (string) $input->getOption(self::OUTPUT_FILE);
$nodeInfos = $this->nodeInfosFactory->create();
$this->markdownDumpNodesOutputFormatter->format($nodeInfos);
$printedContent = $this->markdownNodeInfosPrinter->print($nodeInfos);
$this->smartFileSystem->dumpFile($outputFile, $printedContent);
$outputFileFileInfo = new SmartFileInfo($outputFile);
$message = sprintf(
'Documentation for "%d" PhpParser Nodes was generated to "%s"',
count($nodeInfos),
$outputFileFileInfo->getRelativeFilePathFromCwd()
);
$this->symfonyStyle->success($message);
return ShellCode::SUCCESS;
}

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Rector\Utils\NodeDocumentationGenerator;
use Rector\Utils\NodeDocumentationGenerator\Sorter\NodeInfoSorter;
use Rector\Utils\NodeDocumentationGenerator\ValueObject\NodeInfo;
use Rector\Utils\NodeDocumentationGenerator\ValueObject\NodeInfos;
/**
* @see \Rector\Utils\NodeDocumentationGenerator\Tests\NodeInfosFactoryTest
@ -17,18 +17,27 @@ final class NodeInfosFactory
*/
private $nodeCodeSampleProvider;
public function __construct(NodeCodeSampleProvider $nodeCodeSampleProvider)
/**
* @var NodeInfoSorter
*/
private $nodeInfoSorter;
public function __construct(NodeCodeSampleProvider $nodeCodeSampleProvider, NodeInfoSorter $nodeInfoSorter)
{
$this->nodeCodeSampleProvider = $nodeCodeSampleProvider;
$this->nodeInfoSorter = $nodeInfoSorter;
}
public function create(): NodeInfos
/**
* @return NodeInfo[]
*/
public function create(): array
{
$nodeInfos = [];
foreach ($this->nodeCodeSampleProvider->provide() as $nodeClass => $nodeCodeSamples) {
$nodeInfos[] = new NodeInfo($nodeClass, $nodeCodeSamples);
}
return new NodeInfos($nodeInfos);
return $this->nodeInfoSorter->sortNodeInfosByClass($nodeInfos);
}
}

View File

@ -1,96 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Utils\NodeDocumentationGenerator\OutputFormatter;
use Rector\Utils\NodeDocumentationGenerator\ValueObject\NodeCodeSample;
use Rector\Utils\NodeDocumentationGenerator\ValueObject\NodeInfo;
use Rector\Utils\NodeDocumentationGenerator\ValueObject\NodeInfos;
use Symfony\Component\Console\Style\SymfonyStyle;
final class MarkdownDumpNodesOutputFormatter
{
/**
* @var SymfonyStyle
*/
private $symfonyStyle;
public function __construct(SymfonyStyle $symfonyStyle)
{
$this->symfonyStyle = $symfonyStyle;
}
public function format(NodeInfos $nodeInfos): void
{
$this->symfonyStyle->writeln('# Node Overview');
$this->symfonyStyle->newLine();
$this->symfonyStyle->newLine();
foreach ($nodeInfos->provide() as $nodeInfo) {
$message = sprintf('## `%s`', $nodeInfo->getClass());
$this->symfonyStyle->writeln($message);
$this->symfonyStyle->newLine();
$this->printCodeExample($nodeInfo);
$this->printPublicProperties($nodeInfo);
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln('<br>');
$this->symfonyStyle->newLine();
}
}
private function printCodeExample(NodeInfo $nodeInfo): void
{
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln('### Example PHP Code');
$this->symfonyStyle->newLine();
foreach ($nodeInfo->getNodeCodeSamples() as $nodeCodeSample) {
$this->printNodeCodeSample($nodeCodeSample);
$this->newLine();
}
}
private function printPublicProperties(NodeInfo $nodeInfo): void
{
if (! $nodeInfo->hasPublicProperties()) {
return;
}
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln('### Public Properties');
$this->symfonyStyle->newLine();
foreach ($nodeInfo->getPublicPropertyInfos() as $publicPropertyInfo) {
$this->symfonyStyle->writeln($publicPropertyInfo);
}
}
private function printNodeCodeSample(NodeCodeSample $nodeCodeSample): void
{
$this->printPhpSnippet($nodeCodeSample->getPhpCode());
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln('↓');
$this->symfonyStyle->newLine();
$this->printPhpSnippet($nodeCodeSample->getPrintedContent());
}
private function newLine(): void
{
$this->symfonyStyle->newLine();
$this->symfonyStyle->writeln('<br>');
$this->symfonyStyle->newLine();
}
private function printPhpSnippet(string $printedContent): void
{
$message = sprintf('```php%s%s%s```', PHP_EOL, $printedContent, PHP_EOL);
$this->symfonyStyle->writeln($message);
}
}

View File

@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace Rector\Utils\NodeDocumentationGenerator\Printer;
use Rector\Utils\NodeDocumentationGenerator\ValueObject\NodeCodeSample;
use Rector\Utils\NodeDocumentationGenerator\ValueObject\NodeInfo;
final class MarkdownNodeInfosPrinter
{
/**
* @param NodeInfo[] $nodeInfos
*/
public function print(array $nodeInfos): string
{
$contentLines = [];
$contentLines[] = '# Node Overview';
foreach ($nodeInfos as $nodeInfo) {
$contentLines[] = sprintf('## `%s`', $nodeInfo->getClass());
$contentLines[] = $this->printCodeExample($nodeInfo);
$contentLines[] = $this->printPublicProperties($nodeInfo);
$contentLines[] = '<br>';
}
return $this->implodeLinesWithSpace($contentLines);
}
private function printCodeExample(NodeInfo $nodeInfo): string
{
$contentLines = [];
$contentLines[] = '### Example PHP Code';
foreach ($nodeInfo->getNodeCodeSamples() as $nodeCodeSample) {
$contentLines[] = $this->printNodeCodeSample($nodeCodeSample);
$contentLines[] = '<br>';
}
return $this->implodeLinesWithSpace($contentLines);
}
private function printPublicProperties(NodeInfo $nodeInfo): string
{
if (! $nodeInfo->hasPublicProperties()) {
return '';
}
$contentLines = [];
$contentLines[] = '### Public Properties';
$contentLines[] = $this->implodeLines($nodeInfo->getPublicPropertyInfos());
return $this->implodeLinesWithSpace($contentLines);
}
private function implodeLinesWithSpace(array $contentLines): string
{
return implode(PHP_EOL . PHP_EOL, $contentLines);
}
private function printNodeCodeSample(NodeCodeSample $nodeCodeSample): string
{
$contentLines = [
$this->printPhpSnippet($nodeCodeSample->getPhpCode()),
'↓',
$this->printPhpSnippet($nodeCodeSample->getPrintedContent()),
];
return $this->implodeLinesWithSpace($contentLines);
}
private function implodeLines(array $contentLines): string
{
return implode(PHP_EOL, $contentLines);
}
private function printPhpSnippet(string $printedContent): string
{
return sprintf('```php%s%s%s```', PHP_EOL, $printedContent, PHP_EOL);
}
}

View File

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Rector\Utils\NodeDocumentationGenerator\Sorter;
use Rector\Utils\NodeDocumentationGenerator\ValueObject\NodeInfo;
final class NodeInfoSorter
{
/**
* @param NodeInfo[] $nodeInfos
* @return NodeInfo[]
*/
public function sortNodeInfosByClass(array $nodeInfos): array
{
usort($nodeInfos, function (NodeInfo $firstNodeInfo, NodeInfo $secondNodeInfo): int {
return $firstNodeInfo->getClass() <=> $secondNodeInfo->getClass();
});
return $nodeInfos;
}
}

View File

@ -1,42 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Utils\NodeDocumentationGenerator\ValueObject;
final class NodeInfos
{
/**
* @var NodeInfo[]
*/
private $nodeInfos = [];
/**
* @param NodeInfo[] $nodeInfos
*/
public function __construct(array $nodeInfos)
{
$this->nodeInfos = $this->sortNodeInfosByClass($nodeInfos);
}
/**
* @return NodeInfo[]
*/
public function provide(): array
{
return $this->nodeInfos;
}
/**
* @param NodeInfo[] $nodeInfos
* @return NodeInfo[]
*/
private function sortNodeInfosByClass(array $nodeInfos): array
{
usort($nodeInfos, function (NodeInfo $firstNodeInfo, NodeInfo $secondNodeInfo): int {
return $firstNodeInfo->getClass() <=> $secondNodeInfo->getClass();
});
return $nodeInfos;
}
}

View File

@ -26,7 +26,7 @@ final class NodeInfosFactoryTest extends AbstractKernelTestCase
{
$nodeInfos = $this->nodeInfosFactory->create();
$nodeInfoCount = count($nodeInfos->provide());
$nodeInfoCount = count($nodeInfos);
$this->assertGreaterThan(130, $nodeInfoCount);
}
}