From 7b019d47b6faf60e3f27613460d5f009e8794a50 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sat, 24 Feb 2018 11:31:19 +0100 Subject: [PATCH] working prototype --- packages/RectorBuilder/src/CaseRector.php | 77 ++++++++++++------- .../RectorBuilder/src/CaseRectorBuilder.php | 18 ++++- .../Correct/correct.php.inc | 21 +++-- .../ValidateControlRector/RectorProvider.php | 8 +- .../ValidateControlRector/Wrong/wrong.php.inc | 21 +++-- 5 files changed, 103 insertions(+), 42 deletions(-) diff --git a/packages/RectorBuilder/src/CaseRector.php b/packages/RectorBuilder/src/CaseRector.php index ca211d70c6e..1c03702ce64 100644 --- a/packages/RectorBuilder/src/CaseRector.php +++ b/packages/RectorBuilder/src/CaseRector.php @@ -3,7 +3,10 @@ namespace Rector\RectorBuilder; use PhpParser\Node; +use PhpParser\Node\Expr\MethodCall; +use Rector\Node\NodeFactory; use Rector\NodeAnalyzer\MethodCallAnalyzer; +use Rector\NodeChanger\IdentifierRenamer; use Rector\Rector\AbstractRector; final class CaseRector extends AbstractRector @@ -33,34 +36,20 @@ final class CaseRector extends AbstractRector */ private $newArguments = []; - public function __construct(MethodCallAnalyzer $methodCallAnalyzer) + /** + * @var IdentifierRenamer + */ + private $identifierRenamer; + /** + * @var NodeFactory + */ + private $nodeFactory; + + public function __construct(MethodCallAnalyzer $methodCallAnalyzer, IdentifierRenamer $identifierRenamer, NodeFactory $nodeFactory) { $this->methodCallAnalyzer = $methodCallAnalyzer; - } - - public function isCandidate(Node $node): bool - { - if ($this->methodCallType) { - if (! $this->methodCallAnalyzer->isType($node, $this->methodCallType)) { - return false; - } - } - - if ($this->methodName) { - if (! $this->methodCallAnalyzer->isMethod($node, $this->methodName)) { - return false; - } - } - - dump('EE'); - die; - // TODO: Implement isCandidate() method. - } - - public function refactor(Node $node): ?Node - { - dump('refactor'); - die; + $this->identifierRenamer = $identifierRenamer; + $this->nodeFactory = $nodeFactory; } public function matchMethodCallByType(string $methodCallType): self @@ -89,4 +78,40 @@ final class CaseRector extends AbstractRector $this->newArguments[$position] = $value; return $this; } + + public function isCandidate(Node $node): bool + { + if ($this->methodCallType) { + if (! $this->methodCallAnalyzer->isType($node, $this->methodCallType)) { + return false; + } + } + + + if ($this->methodName) { + if (! $this->methodCallAnalyzer->isMethod($node, $this->methodName)) { + return false; + } + } + + return true; + } + + public function refactor(Node $node): ?Node + { + if ($this->newMethodName && $node instanceof MethodCall) { + $this->identifierRenamer->renameNode($node, $this->newMethodName); + } + + if ($this->newArguments && $node instanceof MethodCall) { + foreach ($this->newArguments as $position => $argument) { + // to check adding arguments in order + if (! isset($node->args[$position]) && isset($node->args[$position - 1])) { + $node->args[$position] = $this->nodeFactory->createArg($argument); + } + } + } + + return $node; + } } diff --git a/packages/RectorBuilder/src/CaseRectorBuilder.php b/packages/RectorBuilder/src/CaseRectorBuilder.php index ea3a230a71c..58f72751515 100644 --- a/packages/RectorBuilder/src/CaseRectorBuilder.php +++ b/packages/RectorBuilder/src/CaseRectorBuilder.php @@ -2,8 +2,10 @@ namespace Rector\RectorBuilder; +use Rector\Node\NodeFactory; use Rector\NodeAnalyzer\MethodCallAnalyzer; use Rector\NodeChanger\ExpressionAdder; +use Rector\NodeChanger\IdentifierRenamer; use Rector\NodeChanger\PropertyAdder; final class CaseRectorBuilder @@ -22,20 +24,32 @@ final class CaseRectorBuilder * @var PropertyAdder */ private $propertyAdder; + /** + * @var IdentifierRenamer + */ + private $identifierRenamer; + /** + * @var NodeFactory + */ + private $nodeFactory; public function __construct( MethodCallAnalyzer $methodCallAnalyzer, ExpressionAdder $expressionAdder, - PropertyAdder $propertyAdder + PropertyAdder $propertyAdder, + IdentifierRenamer $identifierRenamer, + NodeFactory $nodeFactory ) { $this->methodCallAnalyzer = $methodCallAnalyzer; $this->expressionAdder = $expressionAdder; $this->propertyAdder = $propertyAdder; + $this->identifierRenamer = $identifierRenamer; + $this->nodeFactory = $nodeFactory; } public function create(): CaseRector { - $caseRector = new CaseRector($this->methodCallAnalyzer); + $caseRector = new CaseRector($this->methodCallAnalyzer, $this->identifierRenamer, $this->nodeFactory); // @required setter DI replacement $caseRector->setExpressionAdder($this->expressionAdder); diff --git a/tests/Rector/Contrib/Nette/Application/ValidateControlRector/Correct/correct.php.inc b/tests/Rector/Contrib/Nette/Application/ValidateControlRector/Correct/correct.php.inc index 7dc12160de3..b4b47b3fc2a 100644 --- a/tests/Rector/Contrib/Nette/Application/ValidateControlRector/Correct/correct.php.inc +++ b/tests/Rector/Contrib/Nette/Application/ValidateControlRector/Correct/correct.php.inc @@ -1,10 +1,21 @@ redrawControl(); + $myControl->redrawControl('snippet', false); } -$myControl = new CustomControl; -$myControl->redrawControl(null, false); -$myControl->redrawControl('snippet', false); + +namespace Stub_Nette\Application\UI +{ + class Control + { + + } +} diff --git a/tests/Rector/Contrib/Nette/Application/ValidateControlRector/RectorProvider.php b/tests/Rector/Contrib/Nette/Application/ValidateControlRector/RectorProvider.php index 489bbacf6a8..cfe9a94503d 100644 --- a/tests/Rector/Contrib/Nette/Application/ValidateControlRector/RectorProvider.php +++ b/tests/Rector/Contrib/Nette/Application/ValidateControlRector/RectorProvider.php @@ -21,9 +21,9 @@ final class RectorProvider implements RectorProviderInterface public function provide(): RectorInterface { return $this->caseRectorBuilder->create() - ->matchMethodCallByType('@todo') - ->matchMethodName('@todo') - ->changeMethodNameTo('@todo') - ->addArgument(2, '@todo'); + ->matchMethodCallByType('Stub_Nette\Application\UI\Control') + ->matchMethodName('validateControl') + ->changeMethodNameTo('redrawControl') + ->addArgument(1, false); } } diff --git a/tests/Rector/Contrib/Nette/Application/ValidateControlRector/Wrong/wrong.php.inc b/tests/Rector/Contrib/Nette/Application/ValidateControlRector/Wrong/wrong.php.inc index fb9a5c662b3..2e83f215713 100644 --- a/tests/Rector/Contrib/Nette/Application/ValidateControlRector/Wrong/wrong.php.inc +++ b/tests/Rector/Contrib/Nette/Application/ValidateControlRector/Wrong/wrong.php.inc @@ -1,10 +1,21 @@ validateControl(); + $myControl->validateControl('snippet'); } -$myControl = new CustomControl; -$myControl->validateControl(); -$myControl->validateControl('snippet'); + +namespace Stub_Nette\Application\UI +{ + class Control + { + + } +}