add case with already callback

This commit is contained in:
Tomas Votruba 2018-08-29 19:04:21 +02:00
parent 8e88fd24b1
commit dba8a478af
5 changed files with 68 additions and 31 deletions

View File

@ -35,12 +35,12 @@ final class SimpleFunctionAndFilterRector extends AbstractRector
/**
* @var string
*/
private $twigOldFunctionClass;
private $oldFunctionClass;
/**
* @var string
*/
private $twigNewFunctionClass;
private $newFunctionClass;
/**
* @var CallableNodeTraverser
@ -50,29 +50,29 @@ final class SimpleFunctionAndFilterRector extends AbstractRector
/**
* @var string
*/
private $twigOldFilterClass;
private $oldFilterClass;
/**
* @var string
*/
private $twigNewFilterClass;
private $newFilterClass;
public function __construct(
NodeTypeResolver $nodeTypeResolver,
CallableNodeTraverser $callableNodeTraverser,
string $twigExtensionClass = 'Twig_Extension',
string $twigOldFunctionClass = 'Twig_Function_Method',
string $twigNewFunctionClass = 'Twig_SimpleFunction',
string $twigOldFilterClass = 'Twig_Filter_Method',
string $twigNewFilterClass = 'Twig_SimpleFilter'
string $oldFunctionClass = 'Twig_Function_Method',
string $newFunctionClass = 'Twig_SimpleFunction',
string $oldFilterClass = 'Twig_Filter_Method',
string $newFilterClass = 'Twig_SimpleFilter'
) {
$this->twigExtensionClass = $twigExtensionClass;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->twigOldFunctionClass = $twigOldFunctionClass;
$this->twigNewFunctionClass = $twigNewFunctionClass;
$this->oldFunctionClass = $oldFunctionClass;
$this->newFunctionClass = $newFunctionClass;
$this->callableNodeTraverser = $callableNodeTraverser;
$this->twigOldFilterClass = $twigOldFilterClass;
$this->twigNewFilterClass = $twigNewFilterClass;
$this->oldFilterClass = $oldFilterClass;
$this->newFilterClass = $newFilterClass;
}
public function getDefinition(): RectorDefinition
@ -162,22 +162,18 @@ CODE_SAMPLE
$newNodeTypes = $this->nodeTypeResolver->resolve($node->value);
// @todo map by method
if ($methodName === 'getFunctions') {
return $this->processArrayItem(
$node,
$this->twigOldFunctionClass,
$this->twigNewFunctionClass,
$this->oldFunctionClass,
$this->newFunctionClass,
$newNodeTypes
);
}
if ($methodName === 'getFilters') {
return $this->processArrayItem(
$node,
$this->twigOldFilterClass,
$this->twigNewFilterClass,
$newNodeTypes
);
return $this->processArrayItem($node, $this->oldFilterClass, $this->newFilterClass, $newNodeTypes);
}
});
@ -211,14 +207,21 @@ CODE_SAMPLE
$oldArguments = $node->value->args;
$arrayItems = [];
foreach ($oldArguments as $oldArgument) {
$arrayItems[] = new ArrayItem($oldArgument->value);
}
$node->value->args[0] = new Arg(new String_($filterName));
$node->value->args[1] = new Arg(new Array_($arrayItems, [
'kind' => Array_::KIND_SHORT,
]));
if ($oldArguments[0]->value instanceof Array_) {
// already array, just shift it
$node->value->args = array_merge([new Arg(new String_($filterName))], $oldArguments);
} else {
// not array yet, wrap to one
foreach ($oldArguments as $oldArgument) {
$arrayItems[] = new ArrayItem($oldArgument->value);
}
$node->value->args[0] = new Arg(new String_($filterName));
$node->value->args[1] = new Arg(new Array_($arrayItems, [
'kind' => Array_::KIND_SHORT,
]));
}
return $node;
}

View File

@ -0,0 +1,16 @@
<?php declare(strict_types=1);
namespace Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Wrong;
use Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigExtension;
use Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFilterMethod;
final class SomeExtension extends TwigExtension
{
public function getFilters()
{
return [
new \Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigSimpleFilter('sphinx_highlight', [$this, 'sphinx_highlight'], ['is_safe' => ['html']]),
];
}
}

View File

@ -22,6 +22,8 @@ final class SimpleFunctionAndFilterRectorTest extends AbstractRectorTestCase
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
# see https://github.com/IAkumaI/SphinxsearchBundle/pull/10/files
yield [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'];
}
protected function provideConfig(): string

View File

@ -0,0 +1,16 @@
<?php declare(strict_types=1);
namespace Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Wrong;
use Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigExtension;
use Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFilterMethod;
final class SomeExtension extends TwigExtension
{
public function getFilters()
{
return [
'sphinx_highlight' => new TwigFilterMethod([$this, 'sphinx_highlight'], ['is_safe' => ['html']]),
];
}
}

View File

@ -2,7 +2,7 @@ services:
Rector\Twig\Rector\SimpleFunctionAndFilterRector:
arguments:
$twigExtensionClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigExtension'
$twigOldFunctionClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFunctionMethod'
$twigNewFunctionClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigSimpleFunction'
$twigOldFilterClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFilterMethod'
$twigNewFilterClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigSimpleFilter'
$oldFunctionClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFunctionMethod'
$newFunctionClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigSimpleFunction'
$oldFilterClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigFilterMethod'
$newFilterClass: 'Rector\Twig\Tests\Rector\SimpleFunctionAndFilterRector\Source\TwigSimpleFilter'