From e3e26f8ccf332bd4b5b94c53729a564bee9e2715 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 5 Sep 2017 21:28:49 +0200 Subject: [PATCH] [Nette] fix FormSetRequiredRector --- src/NodeAnalyzer/MethodCallAnalyzer.php | 19 ++++++++-- src/NodeFactory/NodeFactory.php | 5 +++ .../Contrib/Nette/FormSetRequiredRector.php | 37 ++++++++++++++++--- .../Correct/correct.php.inc | 3 +- 4 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/NodeAnalyzer/MethodCallAnalyzer.php b/src/NodeAnalyzer/MethodCallAnalyzer.php index ccc4786bf4d..489afe251f7 100644 --- a/src/NodeAnalyzer/MethodCallAnalyzer.php +++ b/src/NodeAnalyzer/MethodCallAnalyzer.php @@ -13,6 +13,8 @@ final class MethodCallAnalyzer if (! $this->isMethodCallType($node, $type)) { return false; } + + return in_array((string) $node->name, $methodsNames, true); } private function isMethodCallType(Node $node, string $type): bool @@ -21,10 +23,8 @@ final class MethodCallAnalyzer return false; } - dump($node->getAttribute('type')); - die; - - if ($node->class->toString() !== $type) { + $variableType = $this->findVariableType($node); + if ($variableType !== $type) { return false; } @@ -65,4 +65,15 @@ final class MethodCallAnalyzer return true; } + + private function findVariableType(MethodCall $methodCallNode): string + { + $varNode = $methodCallNode->var; + + while ($varNode->getAttribute('type') === null) { + $varNode = $varNode->var; + } + + return (string) $varNode->getAttribute('type'); + } } diff --git a/src/NodeFactory/NodeFactory.php b/src/NodeFactory/NodeFactory.php index 59e7d68f6ca..157d8874e38 100644 --- a/src/NodeFactory/NodeFactory.php +++ b/src/NodeFactory/NodeFactory.php @@ -35,6 +35,11 @@ final class NodeFactory return new ConstFetch(new Name('null')); } + public function createFalseConstant(): ConstFetch + { + return new ConstFetch(new Name('false')); + } + public function createClassConstant(string $className, string $constantName): ClassConstFetch { $classNameNode = new FullyQualified($className); diff --git a/src/Rector/Contrib/Nette/FormSetRequiredRector.php b/src/Rector/Contrib/Nette/FormSetRequiredRector.php index aeaa083fe2f..57b440bcda1 100644 --- a/src/Rector/Contrib/Nette/FormSetRequiredRector.php +++ b/src/Rector/Contrib/Nette/FormSetRequiredRector.php @@ -3,8 +3,10 @@ namespace Rector\Rector\Contrib\Nette; use PhpParser\Node; +use PhpParser\Node\Expr\MethodCall; use Rector\Deprecation\SetNames; use Rector\NodeAnalyzer\MethodCallAnalyzer; +use Rector\NodeFactory\NodeFactory; use Rector\Rector\AbstractRector; /** @@ -22,9 +24,15 @@ final class FormSetRequiredRector extends AbstractRector */ private $methodCallAnalyzer; - public function __construct(MethodCallAnalyzer $methodCallAnalyzer) + /** + * @var NodeFactory + */ + private $nodeFactory; + + public function __construct(MethodCallAnalyzer $methodCallAnalyzer, NodeFactory $nodeFactory) { $this->methodCallAnalyzer = $methodCallAnalyzer; + $this->nodeFactory = $nodeFactory; } public function getSetName(): string @@ -43,13 +51,32 @@ final class FormSetRequiredRector extends AbstractRector return false; } -// dump($node); - die; + /** @var MethodCall $node */ + if (count($node->args) !== 1) { + return false; + } + + $arg = $node->args[0]; + if (! $arg->value instanceof Node\Expr\ClassConstFetch) { + return false; + } + + if ($arg->value->class->getAttribute('type') !== self::FORM_CLASS) { + return false; + } + + return $arg->value->name->name === 'FILLED'; } + /** + * @param MethodCall $node + */ public function refactor(Node $node): ?Node { - // replace ->addCondition($form::FILLED) by ->setRequired(FALSE) - // TODO: Implement refactor() method. + $args = [ + new Node\Arg($this->nodeFactory->createFalseConstant()) + ]; + + return new MethodCall($node->var, 'setRequired', $args); } } diff --git a/tests/Rector/Contrib/Nette/FormSetRequiredRector/Correct/correct.php.inc b/tests/Rector/Contrib/Nette/FormSetRequiredRector/Correct/correct.php.inc index e6f6866b272..6fab176418c 100644 --- a/tests/Rector/Contrib/Nette/FormSetRequiredRector/Correct/correct.php.inc +++ b/tests/Rector/Contrib/Nette/FormSetRequiredRector/Correct/correct.php.inc @@ -5,8 +5,7 @@ class SomePresenter public function createNetteForm() { $form = new \Nette\Application\UI\Form; - $form->addText('name') - ->setRequired(FALSE) + $form->addText('name')->setRequired(false) ->addRule('...') ->addRule('...');