rector/vendor/rector/rector-symfony/src/Rector/MethodCall/FormIsValidRector.php
Tomas Votruba 8afa5f961e Updated Rector to commit 074b1781b3fa7c7e23470c57ab85c0bd5fe0bfd2
074b1781b3 Remove RemoveUnusedVariableAssignRector, as could be breaking code in order of excution and hard to do reliable (#3793)
2023-05-11 07:56:32 +00:00

86 lines
2.8 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\Symfony\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\ObjectType;
use Rector\Core\NodeManipulator\MethodCallManipulator;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Symfony\Tests\Rector\MethodCall\FormIsValidRector\FormIsValidRectorTest
*/
final class FormIsValidRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\Core\NodeManipulator\MethodCallManipulator
*/
private $methodCallManipulator;
public function __construct(MethodCallManipulator $methodCallManipulator)
{
$this->methodCallManipulator = $methodCallManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Adds `$form->isSubmitted()` validation to all `$form->isValid()` calls in Form in Symfony', [new CodeSample(<<<'CODE_SAMPLE'
if ($form->isValid()) {
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
if ($form->isSubmitted() && $form->isValid()) {
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [If_::class];
}
/**
* @param If_ $node
*/
public function refactor(Node $node) : ?Node
{
if (!$node->cond instanceof MethodCall) {
return null;
}
$methodCall = $node->cond;
if (!$methodCall->var instanceof Variable) {
return null;
}
if ($this->shouldSkipMethodCall($methodCall)) {
return null;
}
if ($this->isIsSubmittedByAlreadyCalledOnVariable($methodCall->var)) {
return null;
}
/** @var string $variableName */
$variableName = $this->getName($methodCall->var);
$node->cond = new BooleanAnd($this->nodeFactory->createMethodCall($variableName, 'isSubmitted'), $this->nodeFactory->createMethodCall($variableName, 'isValid'));
return $node;
}
private function shouldSkipMethodCall(MethodCall $methodCall) : bool
{
if (!$this->isName($methodCall->name, 'isValid')) {
return \true;
}
return !$this->isObjectType($methodCall->var, new ObjectType('Symfony\\Component\\Form\\Form'));
}
private function isIsSubmittedByAlreadyCalledOnVariable(Variable $variable) : bool
{
$previousMethodCallNamesOnVariable = $this->methodCallManipulator->findMethodCallNamesOnVariable($variable);
// already checked by isSubmitted()
return \in_array('isSubmitted', $previousMethodCallNamesOnVariable, \true);
}
}