This commit is contained in:
TomasVotruba 2017-09-03 14:08:17 +02:00
parent 338dcdb339
commit 748e8218fe
9 changed files with 130 additions and 110 deletions

View File

@ -2,12 +2,19 @@
namespace Rector\NodeFactory;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
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\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\TraitUse;
final class NodeFactory
{
@ -16,12 +23,11 @@ final class NodeFactory
*/
public function createLocalPropertyFetch(string $propertyName): PropertyFetch
{
return new PropertyFetch(
new Variable('this', [
'name' => $propertyName,
]),
$propertyName
);
$localVariable = new Variable('this', [
'name' => $propertyName,
]);
return new PropertyFetch($localVariable, $propertyName);
}
public function createNullConstant(): ConstFetch
@ -29,9 +35,16 @@ final class NodeFactory
return new ConstFetch(new Name('null'));
}
public function createClassConstant(string $className, string $constantName): ClassConstFetch
{
$classNameNode = new FullyQualified($className);
return new ClassConstFetch($classNameNode, $constantName);
}
public function createClassConstantReference(string $className): ClassConstFetch
{
$nameNode = new Name('\\' . $className);
$nameNode = new FullyQualified($className);
return new ClassConstFetch($nameNode, 'class');
}
@ -42,4 +55,32 @@ final class NodeFactory
return new MethodCall($varNode, $methodName);
}
public function createTraitUse(string $traitName): TraitUse
{
$traitNameNode = new FullyQualified($traitName);
return new TraitUse([$traitNameNode]);
}
/**
* @param mixed|Node[] ...$items
*/
public function createArray(...$items): Array_
{
$arrayItems = [];
foreach ($items as $item) {
if ($item instanceof Variable) {
$arrayItems[] = new ArrayItem($item);
} elseif ($item instanceof Identifier) {
$string = new String_((string) $item);
$arrayItems[] = new ArrayItem($string);
}
}
return new Array_($arrayItems, [
'kind' => Array_::KIND_SHORT,
]);
}
}

View File

@ -14,7 +14,7 @@ abstract class AbstractChangeParentClassRector extends AbstractRector
return false;
}
return $this->getParentClassName($node) === $this->getOldClassName();
return $this->getParentClassName() === $this->getOldClassName();
}
/**
@ -30,19 +30,4 @@ abstract class AbstractChangeParentClassRector extends AbstractRector
abstract protected function getOldClassName(): string;
abstract protected function getNewClassName(): string;
private function getParentClassName(Class_ $classNode): string
{
if (! $classNode->extends) {
return '';
}
/** @var Name $parentClassName */
$parentClassNameNode = $classNode->extends;
/** @var Node\Name\FullyQualified $fsqName */
$fsqName = $parentClassNameNode->getAttribute('resolvedName');
return $fsqName->toString();
}
}

View File

@ -52,6 +52,24 @@ abstract class AbstractRector extends NodeVisitorAbstract implements Deprecation
protected function getClassName(): string
{
if ($this->classNode === null) {
return '';
}
return $this->classNode->namespacedName->toString();
}
protected function getParentClassName(): string
{
if ($this->classNode === null) {
return '';
}
$parentClass = $this->classNode->extends;
/** @var Node\Name\FullyQualified $fqnParentClassName */
$fqnParentClassName = $parentClass->getAttribute('resolvedName');
return $fqnParentClassName->toString();
}
}

View File

@ -3,26 +3,39 @@
namespace Rector\Rector\Contrib\Nette;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Scalar\String_;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\Deprecation\DeprecationInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\Deprecation\SetNames;
use Rector\NodeFactory\NodeFactory;
/**
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject.
*/
final class FormCallbackRector extends NodeVisitorAbstract implements DeprecationInterface, RectorInterface
{
/**
* @var string
*/
public const FORM_CLASS = 'Nette\Application\UI\Form';
/**
* @var Node
*/
private $previousNode;
/**
* @var NodeFactory
*/
private $nodeFactory;
public function __construct(NodeFactory $nodeFactory)
{
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
return SetNames::NETTE;
@ -43,7 +56,7 @@ final class FormCallbackRector extends NodeVisitorAbstract implements Deprecatio
return null;
}
return $this->createShortArray($node);
return $this->nodeFactory->createArray($node->var, $node->name);
}
$this->previousNode = $node;
@ -71,7 +84,7 @@ final class FormCallbackRector extends NodeVisitorAbstract implements Deprecatio
return false;
}
if ($node->var->getAttribute('type') !== $this->getDesiredClass()) {
if ($node->var->getAttribute('type') !== self::FORM_CLASS) {
return false;
}
@ -82,26 +95,4 @@ final class FormCallbackRector extends NodeVisitorAbstract implements Deprecatio
return true;
}
/**
* [$this, 'something']
*/
private function createShortArray(Node $node): Array_
{
return new Array_([
new ArrayItem($node->var),
new ArrayItem(
new String_(
(string) $node->name
)
),
], [
'kind' => Array_::KIND_SHORT,
]);
}
private function getDesiredClass(): string
{
return 'Nette\Application\UI\Form';
}
}

View File

@ -3,11 +3,10 @@
namespace Rector\Rector\Contrib\Nette;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\TraitUse;
use Rector\Builder\StatementGlue;
use Rector\Deprecation\SetNames;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
/**
@ -15,14 +14,30 @@ use Rector\Rector\AbstractRector;
*/
final class NetteObjectToSmartTraitRector extends AbstractRector
{
/**
* @var string
*/
private const PARENT_CLASS = 'Nette\Object';
/**
* @var string
*/
private const TRAIT_NAME = 'Nette\SmartObject';
/**
* @var StatementGlue
*/
private $statementGlue;
public function __construct(StatementGlue $statementGlue)
/**
* @var NodeFactory
*/
private $nodeFactory;
public function __construct(StatementGlue $statementGlue, NodeFactory $nodeFactory)
{
$this->statementGlue = $statementGlue;
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
@ -45,9 +60,7 @@ final class NetteObjectToSmartTraitRector extends AbstractRector
return false;
}
$parentClassName = $this->getParentClassName($node);
return $parentClassName === $this->getParentClass();
return $this->getParentClassName() === self::PARENT_CLASS;
}
/**
@ -55,7 +68,7 @@ final class NetteObjectToSmartTraitRector extends AbstractRector
*/
public function refactor(Node $classNode): ?Node
{
$traitUseNode = $this->createTraitUse($this->getTraitName());
$traitUseNode = $this->nodeFactory->createTraitUse(self::TRAIT_NAME);
$this->statementGlue->addAsFirstTrait($classNode, $traitUseNode);
$this->removeParentClass($classNode);
@ -63,33 +76,6 @@ final class NetteObjectToSmartTraitRector extends AbstractRector
return $classNode;
}
private function createTraitUse(string $traitName): TraitUse
{
return new TraitUse([
new FullyQualified($traitName),
]);
}
private function getParentClass(): string
{
return 'Nette\Object';
}
private function getTraitName(): string
{
return 'Nette\SmartObject';
}
private function getParentClassName(Class_ $classNode): string
{
$parentClass = $classNode->extends;
/** @var FullyQualified $fqnParentClassName */
$fqnParentClassName = $parentClass->getAttribute('resolvedName');
return $fqnParentClassName->toString();
}
private function removeParentClass(Class_ $classNode): void
{
$classNode->extends = null;

View File

@ -3,11 +3,10 @@
namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Deprecation\SetNames;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
/**
@ -22,10 +21,19 @@ use Rector\Rector\AbstractRector;
final class ConstraintUrlOptionRector extends AbstractRector
{
/**
* @todo complete FQN
* @var string
*/
private const URL_CONSTRAINT_CLASS = 'Url';
private const URL_CONSTRAINT_CLASS = 'Symfony\Component\Validator\Constraints\Url';
/**
* @var NodeFactory
*/
private $nodeFactory;
public function __construct(NodeFactory $nodeFactory)
{
$this->nodeFactory = $nodeFactory;
}
public function getSetName(): string
{
@ -60,8 +68,6 @@ final class ConstraintUrlOptionRector extends AbstractRector
*/
public function refactor(Node $node): ?Node
{
$classNameNode = new Name(self::URL_CONSTRAINT_CLASS);
return new ClassConstFetch($classNameNode, 'CHECK_DNS_TYPE_ANY');
return $this->nodeFactory->createClassConstant(self::URL_CONSTRAINT_CLASS, 'CHECK_DNS_TYPE_ANY');
}
}

View File

@ -5,7 +5,6 @@ namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
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;

View File

@ -4,7 +4,6 @@ namespace Rector\Rector\Contrib\SymfonyExtra;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Scalar\String_;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\Kernel\ServiceFromKernelResolver;
@ -78,24 +77,9 @@ final class GetterToPropertyRector extends AbstractRector
* @param MethodCall $methodCallNode
*/
public function refactor(Node $methodCallNode): ?Node
{
return $this->processMethodCallNode($methodCallNode);
}
public function getSetName(): string
{
return SetNames::SYMFONY_EXTRA;
}
public function sinceVersion(): float
{
return 3.3;
}
private function processMethodCallNode(MethodCall $methodCall): ?PropertyFetch
{
$serviceType = $this->serviceFromKernelResolver->resolveServiceClassFromArgument(
$methodCall->args[0],
$methodCallNode->args[0],
LocalKernel::class
);
@ -109,4 +93,14 @@ final class GetterToPropertyRector extends AbstractRector
return $this->nodeFactory->createLocalPropertyFetch($propertyName);
}
public function getSetName(): string
{
return SetNames::SYMFONY_EXTRA;
}
public function sinceVersion(): float
{
return 3.3;
}
}

View File

@ -1,3 +1,3 @@
<?php declare (strict_types=1);
$containt = new Url(['checkDNS' => Url::CHECK_DNS_TYPE_ANY]);
$containt = new Url(['checkDNS' => \Symfony\Component\Validator\Constraints\Url::CHECK_DNS_TYPE_ANY]);