mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-06 03:10:50 +00:00
Merge pull request #23 from TomasVotruba/symplify-geter-to-property-rector
Symplify GetterToPropertyRector
This commit is contained in:
commit
6196e61adf
|
@ -44,13 +44,9 @@ parameters:
|
|||
Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer:
|
||||
# classes might not exist
|
||||
- */src/Rector/Contrib/*/*Rector.php
|
||||
Symplify\CodingStandard\Sniffs\Debug\CommentedOutCodeSniff:
|
||||
# examples of code to be found
|
||||
- src/Rector/Contrib/SymfonyExtra/CommandToConstructorInjectionRector.php
|
||||
SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateElementsSniff:
|
||||
# will be used soon
|
||||
- packages/NodeTypeResolver/src/TypeContext.php
|
||||
|
||||
PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff:
|
||||
# long FQN classes that might not exist
|
||||
- src/Rector/Contrib/Symfony/FrameworkBundleClassReplacementsRector.php
|
||||
|
|
|
@ -20,7 +20,6 @@ final class ServiceFromKernelResolver
|
|||
return $serviceType;
|
||||
}
|
||||
|
||||
|
||||
private function resolveServiceClassByNameFromKernel(string $serviceName, string $kernelClass): ?string
|
||||
{
|
||||
$container = $this->createContainerFromKernelClass($kernelClass);
|
||||
|
|
45
src/NodeAnalyzer/SymfonyContainerCallsAnalyzer.php
Normal file
45
src/NodeAnalyzer/SymfonyContainerCallsAnalyzer.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
|
||||
final class SymfonyContainerCallsAnalyzer
|
||||
{
|
||||
/**
|
||||
* Finds $this->get(...);
|
||||
*/
|
||||
public function isThisCall(MethodCall $methodCall): bool
|
||||
{
|
||||
if ($methodCall->var->name !== 'this' || (string) $methodCall->name !== 'get') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->hasOneStringArgument($methodCall);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds $this->getContainer()->get(...);
|
||||
*/
|
||||
public function isGetContainerCall(MethodCall $methodCall): bool
|
||||
{
|
||||
if (! $methodCall->var instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((string) $methodCall->var->var->name !== 'this' || (string) $methodCall->name !== 'get') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->hasOneStringArgument($methodCall);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds ('some_service')
|
||||
*/
|
||||
private function hasOneStringArgument(MethodCall $methodCall): bool
|
||||
{
|
||||
return count($methodCall->args) === 1 && $methodCall->args[0]->value instanceof String_;
|
||||
}
|
||||
}
|
|
@ -2,8 +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
|
||||
{
|
||||
|
@ -12,11 +23,64 @@ 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
|
||||
{
|
||||
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 FullyQualified($className);
|
||||
|
||||
return new ClassConstFetch($nameNode, 'class');
|
||||
}
|
||||
|
||||
public function createMethodCall(string $variableName, string $methodName): MethodCall
|
||||
{
|
||||
$varNode = new Variable($variableName);
|
||||
|
||||
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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Rector\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
|
||||
abstract class AbstractChangeParentClassRector extends AbstractRector
|
||||
|
@ -14,7 +14,7 @@ abstract class AbstractChangeParentClassRector extends AbstractRector
|
|||
return false;
|
||||
}
|
||||
|
||||
return $this->getParentClassName($node) === $this->getOldClassName();
|
||||
return $this->getParentClassName() === $this->getOldClassName();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,7 +22,7 @@ abstract class AbstractChangeParentClassRector extends AbstractRector
|
|||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$node->extends = new Name('\\' . $this->getNewClassName());
|
||||
$node->extends = new FullyQualified($this->getNewClassName());
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,11 +34,6 @@ abstract class AbstractRector extends NodeVisitorAbstract implements Deprecation
|
|||
return null;
|
||||
}
|
||||
|
||||
protected function getClassName(): string
|
||||
{
|
||||
return $this->classNode->namespacedName->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|int|Node
|
||||
*/
|
||||
|
@ -54,4 +49,27 @@ abstract class AbstractRector extends NodeVisitorAbstract implements Deprecation
|
|||
|
||||
return null;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -12,21 +12,17 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector
|
|||
{
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if ($node instanceof ClassConstFetch) {
|
||||
$className = $this->getClassNameFromClassConstFetch($node);
|
||||
|
||||
if ($className !== $this->getDesiredClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! in_array((string) $node->name, ['DEVELOPMENT', 'PRODUCTION'], true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
if (! $node instanceof ClassConstFetch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
$className = $this->getClassNameFromClassConstFetch($node);
|
||||
|
||||
if ($className !== $this->getDesiredClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return in_array((string) $node->name, ['DEVELOPMENT', 'PRODUCTION'], true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,9 +31,10 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector
|
|||
public function refactor(Node $classConstFetchNode): ?Node
|
||||
{
|
||||
$constantName = (string) $classConstFetchNode->name;
|
||||
$string = strtolower($constantName);
|
||||
|
||||
return new String_($string);
|
||||
$originalConstantValue = strtolower($constantName);
|
||||
|
||||
return new String_($originalConstantValue);
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,8 @@ 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;
|
||||
|
||||
/**
|
||||
|
@ -18,6 +18,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 +64,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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,11 +6,11 @@ use Nette\Utils\Strings;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Builder\Class_\ClassPropertyCollector;
|
||||
use Rector\Builder\Kernel\ServiceFromKernelResolver;
|
||||
use Rector\Builder\Naming\NameResolver;
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
|
||||
use Rector\NodeFactory\NodeFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\LocalKernel;
|
||||
|
@ -59,17 +59,23 @@ final class CommandToConstructorInjectionRector extends AbstractRector
|
|||
* @var NodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
/**
|
||||
* @var SymfonyContainerCallsAnalyzer
|
||||
*/
|
||||
private $symfonyContainerCallsAnalyzer;
|
||||
|
||||
public function __construct(
|
||||
ServiceFromKernelResolver $serviceFromKernelResolver,
|
||||
ClassPropertyCollector $classPropertyCollector,
|
||||
NameResolver $nameResolver,
|
||||
NodeFactory $nodeFactory
|
||||
NodeFactory $nodeFactory,
|
||||
SymfonyContainerCallsAnalyzer $symfonyContainerCallsAnalyzer
|
||||
) {
|
||||
$this->serviceFromKernelResolver = $serviceFromKernelResolver;
|
||||
$this->classPropertyCollector = $classPropertyCollector;
|
||||
$this->nameResolver = $nameResolver;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->symfonyContainerCallsAnalyzer = $symfonyContainerCallsAnalyzer;
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
|
@ -88,22 +94,11 @@ final class CommandToConstructorInjectionRector extends AbstractRector
|
|||
return false;
|
||||
}
|
||||
|
||||
// finds **$this->getContainer()->get**('some_service');
|
||||
if (! $node instanceof MethodCall || ! $node->var instanceof MethodCall) {
|
||||
if (! $node instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// finds **$this**->getContainer()->**get**('some_service');
|
||||
if ((string) $node->var->var->name !== 'this' || (string) $node->name !== 'get') {
|
||||
return false;
|
||||
}
|
||||
|
||||
// finds $this->getContainer()->get**('some_service')**;
|
||||
if (count($node->args) !== 1 || ! $node->args[0]->value instanceof String_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return $this->symfonyContainerCallsAnalyzer->isGetContainerCall($node);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -113,7 +108,11 @@ final class CommandToConstructorInjectionRector extends AbstractRector
|
|||
{
|
||||
$this->replaceParentContainerAwareCommandWithCommand();
|
||||
|
||||
$serviceType = $this->serviceFromKernelResolver->resolveServiceClassFromArgument($node->args[0], LocalKernel::class);
|
||||
$serviceType = $this->serviceFromKernelResolver->resolveServiceClassFromArgument(
|
||||
$node->args[0],
|
||||
LocalKernel::class
|
||||
);
|
||||
|
||||
if ($serviceType === null) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -3,14 +3,12 @@
|
|||
namespace Rector\Rector\Contrib\SymfonyExtra;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
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;
|
||||
use Rector\Builder\Naming\NameResolver;
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
|
||||
use Rector\NodeFactory\NodeFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\LocalKernel;
|
||||
|
@ -43,56 +41,53 @@ final class GetterToPropertyRector extends AbstractRector
|
|||
* @var NodeFactory
|
||||
*/
|
||||
private $nodeFactory;
|
||||
/**
|
||||
* @var SymfonyContainerCallsAnalyzer
|
||||
*/
|
||||
private $symfonyContainerCallsAnalyzer;
|
||||
|
||||
public function __construct(
|
||||
NameResolver $nameResolver,
|
||||
ServiceFromKernelResolver $serviceFromKernelResolver,
|
||||
ClassPropertyCollector $classPropertyCollector,
|
||||
NodeFactory $nodeFactory
|
||||
NodeFactory $nodeFactory,
|
||||
SymfonyContainerCallsAnalyzer $symfonyContainerCallsAnalyzer
|
||||
) {
|
||||
$this->nameResolver = $nameResolver;
|
||||
$this->serviceFromKernelResolver = $serviceFromKernelResolver;
|
||||
$this->classPropertyCollector = $classPropertyCollector;
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
$this->symfonyContainerCallsAnalyzer = $symfonyContainerCallsAnalyzer;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
// finds $var = $this->get('some_service');
|
||||
// finds $var = $this->get('some_service')->getData();
|
||||
if ($node instanceof Assign && ($node->expr instanceof MethodCall || $node->var instanceof MethodCall)) {
|
||||
if ($this->isContainerGetCall($node->expr)) {
|
||||
return true;
|
||||
}
|
||||
if (! $node instanceof MethodCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// finds ['var => $this->get('some_service')->getData()]
|
||||
if ($node instanceof MethodCall && $node->var instanceof MethodCall) {
|
||||
if ($this->isContainerGetCall($node->var)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->symfonyContainerCallsAnalyzer->isThisCall($node);
|
||||
}
|
||||
|
||||
public function refactor(Node $assignOrMethodCallNode): ?Node
|
||||
/**
|
||||
* @param MethodCall $methodCallNode
|
||||
*/
|
||||
public function refactor(Node $methodCallNode): ?Node
|
||||
{
|
||||
if ($assignOrMethodCallNode instanceof Assign) {
|
||||
$refactoredMethodCall = $this->processMethodCallNode($assignOrMethodCallNode->expr);
|
||||
if ($refactoredMethodCall) {
|
||||
$assignOrMethodCallNode->expr = $refactoredMethodCall;
|
||||
}
|
||||
$serviceType = $this->serviceFromKernelResolver->resolveServiceClassFromArgument(
|
||||
$methodCallNode->args[0],
|
||||
LocalKernel::class
|
||||
);
|
||||
|
||||
if ($serviceType === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($assignOrMethodCallNode instanceof MethodCall) {
|
||||
$refactoredMethodCall = $this->processMethodCallNode($assignOrMethodCallNode->var);
|
||||
if ($refactoredMethodCall) {
|
||||
$assignOrMethodCallNode->var = $refactoredMethodCall;
|
||||
}
|
||||
}
|
||||
$propertyName = $this->nameResolver->resolvePropertyNameFromType($serviceType);
|
||||
|
||||
return $assignOrMethodCallNode;
|
||||
$this->classPropertyCollector->addPropertyForClass($this->getClassName(), $serviceType, $propertyName);
|
||||
|
||||
return $this->nodeFactory->createLocalPropertyFetch($propertyName);
|
||||
}
|
||||
|
||||
public function getSetName(): string
|
||||
|
@ -104,38 +99,4 @@ final class GetterToPropertyRector extends AbstractRector
|
|||
{
|
||||
return 3.3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is "$this->get('string')" statements?
|
||||
*/
|
||||
private function isContainerGetCall(MethodCall $methodCall): bool
|
||||
{
|
||||
if ($methodCall->var->name !== 'this') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((string) $methodCall->name !== 'get') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $methodCall->args[0]->value instanceof String_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function processMethodCallNode(MethodCall $methodCall): ?PropertyFetch
|
||||
{
|
||||
$serviceType = $this->serviceFromKernelResolver->resolveServiceClassFromArgument($methodCall->args[0], LocalKernel::class);
|
||||
if ($serviceType === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$propertyName = $this->nameResolver->resolvePropertyNameFromType($serviceType);
|
||||
|
||||
$this->classPropertyCollector->addPropertyForClass($this->getClassName(), $serviceType, $propertyName);
|
||||
|
||||
return $this->nodeFactory->createLocalPropertyFetch($propertyName);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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]);
|
||||
|
|
Loading…
Reference in New Issue
Block a user