working prototype

This commit is contained in:
TomasVotruba 2018-02-24 11:31:19 +01:00
parent 866227ebe4
commit 7b019d47b6
5 changed files with 103 additions and 42 deletions

View File

@ -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;
}
}

View File

@ -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);

View File

@ -1,10 +1,21 @@
<?php declare (strict_types=1);
final class CustomControl extends Nette\Application\UI\Control
{
namespace {
final class CustomControl extends Stub_Nette\Application\UI\Control
{
}
$myControl = new CustomControl;
$myControl->redrawControl();
$myControl->redrawControl('snippet', false);
}
$myControl = new CustomControl;
$myControl->redrawControl(null, false);
$myControl->redrawControl('snippet', false);
namespace Stub_Nette\Application\UI
{
class Control
{
}
}

View File

@ -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);
}
}

View File

@ -1,10 +1,21 @@
<?php declare (strict_types=1);
final class CustomControl extends Nette\Application\UI\Control
{
namespace {
final class CustomControl extends Stub_Nette\Application\UI\Control
{
}
$myControl = new CustomControl;
$myControl->validateControl();
$myControl->validateControl('snippet');
}
$myControl = new CustomControl;
$myControl->validateControl();
$myControl->validateControl('snippet');
namespace Stub_Nette\Application\UI
{
class Control
{
}
}