mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
simplify classes map
This commit is contained in:
parent
dba8a478af
commit
ba5385cea1
@ -32,47 +32,32 @@ final class SimpleFunctionAndFilterRector extends AbstractRector
|
|||||||
*/
|
*/
|
||||||
private $nodeTypeResolver;
|
private $nodeTypeResolver;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $oldFunctionClass;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $newFunctionClass;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var CallableNodeTraverser
|
* @var CallableNodeTraverser
|
||||||
*/
|
*/
|
||||||
private $callableNodeTraverser;
|
private $callableNodeTraverser;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string[]
|
||||||
*/
|
*/
|
||||||
private $oldFilterClass;
|
private $oldToNewClasses = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @param string[] $oldToNewClasses
|
||||||
*/
|
*/
|
||||||
private $newFilterClass;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
NodeTypeResolver $nodeTypeResolver,
|
NodeTypeResolver $nodeTypeResolver,
|
||||||
CallableNodeTraverser $callableNodeTraverser,
|
CallableNodeTraverser $callableNodeTraverser,
|
||||||
string $twigExtensionClass = 'Twig_Extension',
|
string $twigExtensionClass = 'Twig_Extension',
|
||||||
string $oldFunctionClass = 'Twig_Function_Method',
|
array $oldToNewClasses = [
|
||||||
string $newFunctionClass = 'Twig_SimpleFunction',
|
'Twig_Function_Method' => 'Twig_SimpleFunction',
|
||||||
string $oldFilterClass = 'Twig_Filter_Method',
|
'Twig_Filter_Method' => 'Twig_SimpleFilter',
|
||||||
string $newFilterClass = 'Twig_SimpleFilter'
|
]
|
||||||
) {
|
) {
|
||||||
$this->twigExtensionClass = $twigExtensionClass;
|
$this->twigExtensionClass = $twigExtensionClass;
|
||||||
$this->nodeTypeResolver = $nodeTypeResolver;
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
||||||
$this->oldFunctionClass = $oldFunctionClass;
|
|
||||||
$this->newFunctionClass = $newFunctionClass;
|
|
||||||
$this->callableNodeTraverser = $callableNodeTraverser;
|
$this->callableNodeTraverser = $callableNodeTraverser;
|
||||||
$this->oldFilterClass = $oldFilterClass;
|
$this->oldToNewClasses = $oldToNewClasses;
|
||||||
$this->newFilterClass = $newFilterClass;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDefinition(): RectorDefinition
|
public function getDefinition(): RectorDefinition
|
||||||
@ -149,9 +134,7 @@ CODE_SAMPLE
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->callableNodeTraverser->traverseNodesWithCallable([$returnNode->expr], function (Node $node) use (
|
$this->callableNodeTraverser->traverseNodesWithCallable([$returnNode->expr], function (Node $node) {
|
||||||
$methodName
|
|
||||||
) {
|
|
||||||
if (! $node instanceof ArrayItem) {
|
if (! $node instanceof ArrayItem) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -162,19 +145,7 @@ CODE_SAMPLE
|
|||||||
|
|
||||||
$newNodeTypes = $this->nodeTypeResolver->resolve($node->value);
|
$newNodeTypes = $this->nodeTypeResolver->resolve($node->value);
|
||||||
|
|
||||||
// @todo map by method
|
return $this->processArrayItem($node, $newNodeTypes);
|
||||||
if ($methodName === 'getFunctions') {
|
|
||||||
return $this->processArrayItem(
|
|
||||||
$node,
|
|
||||||
$this->oldFunctionClass,
|
|
||||||
$this->newFunctionClass,
|
|
||||||
$newNodeTypes
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($methodName === 'getFilters') {
|
|
||||||
return $this->processArrayItem($node, $this->oldFilterClass, $this->newFilterClass, $newNodeTypes);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return $returnNode;
|
return $returnNode;
|
||||||
@ -183,12 +154,16 @@ CODE_SAMPLE
|
|||||||
/**
|
/**
|
||||||
* @param string[] $newNodeTypes
|
* @param string[] $newNodeTypes
|
||||||
*/
|
*/
|
||||||
private function processArrayItem(ArrayItem $node, string $oldClass, string $newClass, array $newNodeTypes): ?Node
|
private function processArrayItem(ArrayItem $node, array $newNodeTypes): ?Node
|
||||||
{
|
{
|
||||||
if (! in_array($oldClass, $newNodeTypes, true)) {
|
$matchedOldClasses = array_intersect(array_keys($this->oldToNewClasses), $newNodeTypes);
|
||||||
|
if (! $matchedOldClasses) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$matchedOldClass = array_pop($matchedOldClasses);
|
||||||
|
$matchedNewClass = $this->oldToNewClasses[$matchedOldClass];
|
||||||
|
|
||||||
if (! $node->key instanceof String_) {
|
if (! $node->key instanceof String_) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -201,18 +176,16 @@ CODE_SAMPLE
|
|||||||
$filterName = $node->key->value;
|
$filterName = $node->key->value;
|
||||||
|
|
||||||
$node->key = null;
|
$node->key = null;
|
||||||
|
$node->value->class = new FullyQualified($matchedNewClass);
|
||||||
$node->value->class = new FullyQualified($newClass);
|
|
||||||
|
|
||||||
$oldArguments = $node->value->args;
|
$oldArguments = $node->value->args;
|
||||||
|
|
||||||
$arrayItems = [];
|
|
||||||
|
|
||||||
if ($oldArguments[0]->value instanceof Array_) {
|
if ($oldArguments[0]->value instanceof Array_) {
|
||||||
// already array, just shift it
|
// already array, just shift it
|
||||||
$node->value->args = array_merge([new Arg(new String_($filterName))], $oldArguments);
|
$node->value->args = array_merge([new Arg(new String_($filterName))], $oldArguments);
|
||||||
} else {
|
} else {
|
||||||
// not array yet, wrap to one
|
// not array yet, wrap to one
|
||||||
|
$arrayItems = [];
|
||||||
foreach ($oldArguments as $oldArgument) {
|
foreach ($oldArguments as $oldArgument) {
|
||||||
$arrayItems[] = new ArrayItem($oldArgument->value);
|
$arrayItems[] = new ArrayItem($oldArgument->value);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ services:
|
|||||||
Rector\Twig\Rector\SimpleFunctionAndFilterRector:
|
Rector\Twig\Rector\SimpleFunctionAndFilterRector:
|
||||||
arguments:
|
arguments:
|
||||||
$twigExtensionClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigExtension'
|
$twigExtensionClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigExtension'
|
||||||
$oldFunctionClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFunctionMethod'
|
$oldToNewClasses:
|
||||||
$newFunctionClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigSimpleFunction'
|
'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFunctionMethod': 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigSimpleFunction'
|
||||||
$oldFilterClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFilterMethod'
|
'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFilterMethod': 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigSimpleFilter'
|
||||||
$newFilterClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigSimpleFilter'
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user