[Nette] fix FormSetRequiredRector

This commit is contained in:
TomasVotruba 2017-09-05 21:28:49 +02:00
parent b230f5201f
commit e3e26f8ccf
4 changed files with 53 additions and 11 deletions

View File

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

View File

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

View File

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

View File

@ -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('...');