This commit is contained in:
TomasVotruba 2017-09-02 15:50:47 +02:00
parent c5b52323d9
commit 338dcdb339
5 changed files with 59 additions and 23 deletions

View File

@ -28,7 +28,7 @@ checkers:
Symplify\CodingStandard\Sniffs\DependencyInjection\NoClassInstantiationSniff:
extraAllowedClasses:
- 'PhpParser\Node\*'
# - 'PhpParser\Node\*'
- 'PhpParser\Comment\Doc'
parameters:

View File

@ -2,8 +2,12 @@
namespace Rector\NodeFactory;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
final class NodeFactory
{
@ -19,4 +23,23 @@ final class NodeFactory
$propertyName
);
}
public function createNullConstant(): ConstFetch
{
return new ConstFetch(new Name('null'));
}
public function createClassConstantReference(string $className): ClassConstFetch
{
$nameNode = new Name('\\' . $className);
return new ClassConstFetch($nameNode, 'class');
}
public function createMethodCall(string $variableName, string $methodName): MethodCall
{
$varNode = new Variable($variableName);
return new MethodCall($varNode, $methodName);
}
}

View File

@ -7,6 +7,7 @@ use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use Rector\Deprecation\SetNames;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
/**
@ -18,6 +19,16 @@ use Rector\Rector\AbstractRector;
*/
final class FormIsValidRector extends AbstractRector
{
/**
* @var NodeFactory
*/
private $nodeFactory;
public function __construct(NodeFactory $nodeFactory)
{
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::SYMFONY;
@ -54,18 +65,11 @@ final class FormIsValidRector extends AbstractRector
*/
public function refactor(Node $node): ?Node
{
$varName = $node->var->name;
$variableName = $node->var->name;
return new BooleanAnd(
$this->createMethodCall($varName, 'isSubmitted'),
$this->createMethodCall($varName, 'isValid')
$this->nodeFactory->createMethodCall($variableName, 'isSubmitted'),
$this->nodeFactory->createMethodCall($variableName, 'isValid')
);
}
private function createMethodCall(string $varName, string $methodName): MethodCall
{
$varNode = new Variable($varName);
return new MethodCall($varNode, $methodName);
}
}

View File

@ -3,10 +3,10 @@
namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Deprecation\SetNames;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
/**
@ -55,6 +55,16 @@ final class StringFormTypeToClassRector extends AbstractRector
'form.type.reset' => 'Symfony\Component\Form\Extension\Core\Type\ResetType',
];
/**
* @var NodeFactory
*/
private $nodeFactory;
public function __construct(NodeFactory $nodeFactory)
{
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::SYMFONY;
@ -76,8 +86,7 @@ final class StringFormTypeToClassRector extends AbstractRector
public function refactor(Node $node): ?Node
{
$class = $this->nameToClassMap[$node->value];
$nameNode = new Name('\\' . $class);
return new ClassConstFetch($nameNode, 'class');
return $this->nodeFactory->createClassConstantReference($class);
}
}

View File

@ -3,12 +3,11 @@
namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Deprecation\SetNames;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
/**
@ -32,9 +31,15 @@ final class VarDumperTestTraitMethodArgsRector 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
@ -74,7 +79,7 @@ final class VarDumperTestTraitMethodArgsRector extends AbstractRector
if ($methodArguments[2]->value instanceof String_) {
$methodArguments[3] = $methodArguments[2];
$methodArguments[2] = $this->createNullConstant();
$methodArguments[2] = $this->nodeFactory->createNullConstant();
$node->args = $methodArguments;
@ -83,9 +88,4 @@ final class VarDumperTestTraitMethodArgsRector extends AbstractRector
return null;
}
private function createNullConstant(): ConstFetch
{
return new ConstFetch(new Name('null'));
}
}