mirror of
https://github.com/rectorphp/rector.git
synced 2025-03-14 12:29:43 +01:00
update docs with PHP snippets
This commit is contained in:
parent
9c08060925
commit
8d556bf2fc
File diff suppressed because it is too large
Load Diff
@ -15,9 +15,9 @@ use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\DocumentationGenerator\PhpKeywordHighlighter;
|
||||
use Rector\DocumentationGenerator\RectorMetadataResolver;
|
||||
use Rector\PHPUnit\TestClassResolver\TestClassResolver;
|
||||
use Rector\SymfonyPhpConfig\Printer\ReturnClosurePrinter;
|
||||
use ReflectionClass;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class MarkdownDumpRectorsOutputFormatter
|
||||
@ -47,18 +47,25 @@ final class MarkdownDumpRectorsOutputFormatter
|
||||
*/
|
||||
private $phpKeywordHighlighter;
|
||||
|
||||
/**
|
||||
* @var ReturnClosurePrinter
|
||||
*/
|
||||
private $returnClosurePrinter;
|
||||
|
||||
public function __construct(
|
||||
SymfonyStyle $symfonyStyle,
|
||||
MarkdownDifferAndFormatter $markdownDifferAndFormatter,
|
||||
RectorMetadataResolver $rectorMetadataResolver,
|
||||
TestClassResolver $testClassResolver,
|
||||
PhpKeywordHighlighter $phpKeywordHighlighter
|
||||
PhpKeywordHighlighter $phpKeywordHighlighter,
|
||||
ReturnClosurePrinter $returnClosurePrinter
|
||||
) {
|
||||
$this->symfonyStyle = $symfonyStyle;
|
||||
$this->markdownDifferAndFormatter = $markdownDifferAndFormatter;
|
||||
$this->rectorMetadataResolver = $rectorMetadataResolver;
|
||||
$this->testClassResolver = $testClassResolver;
|
||||
$this->phpKeywordHighlighter = $phpKeywordHighlighter;
|
||||
$this->returnClosurePrinter = $returnClosurePrinter;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -206,13 +213,11 @@ final class MarkdownDumpRectorsOutputFormatter
|
||||
}
|
||||
|
||||
$configuration = [
|
||||
'services' => [
|
||||
get_class($rector) => $codeSample->getConfiguration(),
|
||||
],
|
||||
get_class($rector) => $codeSample->getConfiguration(),
|
||||
];
|
||||
$configuration = Yaml::dump($configuration, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
|
||||
|
||||
$this->printCodeWrapped($configuration, 'yaml');
|
||||
$phpConfigContent = $this->returnClosurePrinter->printServices($configuration);
|
||||
$this->printCodeWrapped($phpConfigContent, 'php');
|
||||
|
||||
$this->symfonyStyle->newLine();
|
||||
$this->symfonyStyle->writeln('↓');
|
||||
|
@ -41,6 +41,7 @@ parameters:
|
||||
# prevent PHPStorm autocomplete mess
|
||||
'Symfony\Component\DependencyInjection\Variable': 'PhpParser\Node\Expr\Variable'
|
||||
'Closure': 'PhpParser\Node\Expr\Closure'
|
||||
'phpDocumentor\Reflection\DocBlock\Tags\Return_': 'PhpParser\Node\Stmt\Return_'
|
||||
|
||||
# to allow installing with various phsptan versions without reporting old errors here
|
||||
reportUnmatchedIgnoredErrors: false
|
||||
|
152
rules/symfony-php-config/src/Printer/ReturnClosurePrinter.php
Normal file
152
rules/symfony-php-config/src/Printer/ReturnClosurePrinter.php
Normal file
@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\SymfonyPhpConfig\Printer;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Nop;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use Rector\CodingStyle\Naming\ClassNaming;
|
||||
use Rector\Core\PhpParser\Builder\ParamBuilder;
|
||||
use Rector\Core\PhpParser\Builder\UseBuilder;
|
||||
use Rector\Core\PhpParser\Node\NodeFactory;
|
||||
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
|
||||
|
||||
final class ReturnClosurePrinter
|
||||
{
|
||||
/**
|
||||
* @var BetterStandardPrinter
|
||||
*/
|
||||
private $betterStandardPrinter;
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
|
||||
/**
|
||||
* @var Node[]
|
||||
*/
|
||||
private $useStmts = [];
|
||||
|
||||
/**
|
||||
* @var ClassNaming
|
||||
*/
|
||||
private $classNaming;
|
||||
|
||||
public function __construct(
|
||||
BetterStandardPrinter $betterStandardPrinter,
|
||||
ClassNaming $classNaming,
|
||||
NodeFactory $nodeFactory
|
||||
) {
|
||||
$this->betterStandardPrinter = $betterStandardPrinter;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
|
||||
$this->classNaming = $classNaming;
|
||||
}
|
||||
|
||||
public function printServices(array $services): string
|
||||
{
|
||||
// reset for each services
|
||||
$this->useStmts = [];
|
||||
$this->addUseStmts('Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator');
|
||||
|
||||
$stmts = $this->createClosureStmts($services);
|
||||
|
||||
$closure = new Closure([
|
||||
'params' => [$this->createClosureParam()],
|
||||
'returnType' => new Identifier('void'),
|
||||
'stmts' => $stmts,
|
||||
]);
|
||||
|
||||
$return = new Return_($closure);
|
||||
|
||||
$rootStmts = array_merge($this->useStmts, [new Nop(), $return]);
|
||||
$printedContent = $this->betterStandardPrinter->prettyPrintFile($rootStmts);
|
||||
|
||||
return $this->indentFluentCallToNewline($printedContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $services
|
||||
* @return Expression[]
|
||||
*/
|
||||
private function createClosureStmts(array $services): array
|
||||
{
|
||||
$stmts = [];
|
||||
|
||||
$servicesVariable = new Variable('services');
|
||||
|
||||
$servicesMethodCall = new MethodCall(new Variable('containerConfiguration'), 'services');
|
||||
$assign = new Assign($servicesVariable, $servicesMethodCall);
|
||||
$stmts[] = new Expression($assign);
|
||||
|
||||
foreach ($services as $serviceName => $serviceParameters) {
|
||||
$this->addUseStmts($serviceName);
|
||||
$methodCall = $this->createServicesSetMethodCall($serviceName, $servicesVariable, $serviceParameters);
|
||||
$stmts[] = new Expression($methodCall);
|
||||
}
|
||||
|
||||
return $stmts;
|
||||
}
|
||||
|
||||
private function createClosureParam(): Param
|
||||
{
|
||||
$paramBuilder = new ParamBuilder('containerConfigurator');
|
||||
$paramBuilder->setType('ContainerConfigurator');
|
||||
return $paramBuilder->getNode();
|
||||
}
|
||||
|
||||
private function addUseStmts(string $useImport): void
|
||||
{
|
||||
$useBuilder = new UseBuilder($useImport);
|
||||
$this->useStmts[] = $useBuilder->getNode();
|
||||
}
|
||||
|
||||
private function createServicesSetMethodCall(
|
||||
string $serviceName,
|
||||
Variable $servicesVariable,
|
||||
array $serviceParameters
|
||||
): MethodCall {
|
||||
$shortClassName = $this->classNaming->getShortName($serviceName);
|
||||
$classConstFetch = new ClassConstFetch(new Name($shortClassName), new Identifier('class'));
|
||||
$args = [new Arg($classConstFetch)];
|
||||
|
||||
$methodCall = new MethodCall($servicesVariable, 'set', $args);
|
||||
|
||||
foreach ($serviceParameters as $argument => $value) {
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $singleValue) {
|
||||
// new PHP configuraiton style
|
||||
if (is_object($singleValue)) {
|
||||
// @todo
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$args = $this->nodeFactory->createArgs([$argument, $value]);
|
||||
$methodCall = new MethodCall($methodCall, 'arg', $args);
|
||||
}
|
||||
|
||||
return $methodCall;
|
||||
}
|
||||
|
||||
private function indentFluentCallToNewline(string $content): string
|
||||
{
|
||||
$nextCallIndentReplacement = ')' . PHP_EOL . Strings::indent('->', 8, ' ');
|
||||
return Strings::replace($content, '#\)->#', $nextCallIndentReplacement);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user