move ValidateControlRector to Provider

This commit is contained in:
TomasVotruba 2018-02-24 12:05:49 +01:00
parent 1aadb0cee5
commit d464401aae
3 changed files with 42 additions and 73 deletions

View File

@ -1,69 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\Nette\Application;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeChanger\IdentifierRenamer;
use Rector\Rector\AbstractRector;
/**
* Before::
* - $myControl->validateControl(?$snippet)
*
* After:
* - $myControl->redrawControl(?$snippet, false);
*/
final class ValidateControlRector extends AbstractRector
{
/**
* @var MethodCallAnalyzer
*/
private $methodCallAnalyzer;
/**
* @var IdentifierRenamer
*/
private $identifierRenamer;
/**
* @var NodeFactory
*/
private $nodeFactory;
public function __construct(
MethodCallAnalyzer $methodCallAnalyzer,
IdentifierRenamer $identifierRenamer,
NodeFactory $nodeFactory
) {
$this->methodCallAnalyzer = $methodCallAnalyzer;
$this->identifierRenamer = $identifierRenamer;
$this->nodeFactory = $nodeFactory;
}
public function isCandidate(Node $node): bool
{
return $this->methodCallAnalyzer->isTypeAndMethod(
$node,
'Nette\Application\UI\Control',
'validateControl'
);
}
/**
* @param MethodCall $methodCallNode
*/
public function refactor(Node $methodCallNode): Node
{
$this->identifierRenamer->renameNode($methodCallNode, 'redrawControl');
$methodCallNode->args[0] = $methodCallNode->args[0] ?? $this->nodeFactory->createArg(
$this->nodeFactory->createNullConstant()
);
$methodCallNode->args[1] = $this->nodeFactory->createArg($this->nodeFactory->createFalseConstant());
return $methodCallNode;
}
}

View File

@ -0,0 +1,36 @@
<?php declare(strict_types=1);
namespace Rector\Rector\Contrib\Nette\Application;
use Rector\Contract\Rector\RectorInterface;
use Rector\RectorBuilder\BuilderRectorFactory;
use Rector\RectorBuilder\Contract\RectorProviderInterface;
final class ValidateControlRectorProvider implements RectorProviderInterface
{
/**
* @var BuilderRectorFactory
*/
private $builderRectorFactory;
public function __construct(BuilderRectorFactory $builderRectorFactory)
{
$this->builderRectorFactory = $builderRectorFactory;
}
/**
* Before::
* - $myControl->validateControl(?$snippet)
*
* After:
* - $myControl->redrawControl(?$snippet, false);
*/
public function provide(): RectorInterface
{
return $this->builderRectorFactory->create()
->matchMethodCallByType('Nette\Application\UI\Control')
->matchMethodName('validateControl')
->changeMethodNameTo('redrawControl')
->addArgument(1, false);
}
}

View File

@ -1,12 +1,14 @@
services:
_defaults:
autowire: true
Rector\Rector\Contrib\Nette\Application\ValidateControlRectorProvider: ~
rectors:
Rector\Rector\Contrib\Nette\Utils\NetteObjectToSmartTraitRector: ~
Rector\Rector\Contrib\Nette\Utils\MagicMethodRector: ~
# application
# todo: turn to RectorProvider with fluent setup
Rector\Rector\Contrib\Nette\Application\ValidateControlRector: ~
Rector\Rector\Contrib\Nette\Application\TemplateMagicInvokeFilterCallRector: ~
Rector\Rector\Contrib\Nette\Application\TemplateRegisterHelperRector: ~