mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-18 05:48:21 +01:00
[AbstractRector] add getClassName() method
This commit is contained in:
parent
3514870d12
commit
ffa4d0c6ef
@ -2,23 +2,29 @@
|
||||
|
||||
namespace Rector\Builder\Kernel;
|
||||
|
||||
use PhpParser\Node\Arg;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
|
||||
final class ServiceFromKernelResolver
|
||||
{
|
||||
public function resolveServiceClassByNameFromKernel(string $serviceName, string $kernelClass): ?string
|
||||
public function resolveServiceClassFromArgument(Arg $argNode, string $kernelClass): ?string
|
||||
{
|
||||
/** @var Kernel $kernel */
|
||||
$kernel = new $kernelClass('dev', true);
|
||||
$kernel->boot();
|
||||
$serviceName = $argNode->value->value;
|
||||
$serviceType = $this->resolveServiceClassByNameFromKernel($serviceName, $kernelClass);
|
||||
|
||||
// @todo: cache
|
||||
// @todo: initialize without creating cache or log directory
|
||||
// @todo: call only loadBundles() and initializeContainer() methods
|
||||
if ($serviceType === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $serviceType;
|
||||
}
|
||||
|
||||
|
||||
private function resolveServiceClassByNameFromKernel(string $serviceName, string $kernelClass): ?string
|
||||
{
|
||||
$container = $this->createContainerFromKernelClass($kernelClass);
|
||||
|
||||
/** @var ContainerInterface $container */
|
||||
$container = $kernel->getContainer();
|
||||
if (! $container->has($serviceName)) {
|
||||
// service name could not be found
|
||||
return null;
|
||||
@ -28,4 +34,24 @@ final class ServiceFromKernelResolver
|
||||
|
||||
return get_class($service);
|
||||
}
|
||||
|
||||
private function createContainerFromKernelClass(string $kernelClass): ContainerInterface
|
||||
{
|
||||
$kernel = $this->createKernelFromKernelClass($kernelClass);
|
||||
|
||||
return $kernel->getContainer();
|
||||
}
|
||||
|
||||
private function createKernelFromKernelClass(string $kernelClass): Kernel
|
||||
{
|
||||
/** @var Kernel $kernel */
|
||||
$kernel = new $kernelClass('dev', true);
|
||||
$kernel->boot();
|
||||
|
||||
return $kernel;
|
||||
|
||||
// @todo: cache
|
||||
// @todo: initialize without creating cache or log directory
|
||||
// @todo: call only loadBundles() and initializeContainer() methods
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace Rector\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\Contract\Deprecation\DeprecationInterface;
|
||||
@ -10,6 +11,34 @@ use Rector\Contract\Rector\RectorInterface;
|
||||
|
||||
abstract class AbstractRector extends NodeVisitorAbstract implements DeprecationInterface, RectorInterface
|
||||
{
|
||||
/**
|
||||
* @var Class_|null
|
||||
*/
|
||||
protected $classNode;
|
||||
|
||||
/**
|
||||
* @param Node[] $nodes
|
||||
* @return null|Node[]
|
||||
*/
|
||||
public function beforeTraverse(array $nodes): ?array
|
||||
{
|
||||
$this->classNode = null;
|
||||
|
||||
foreach ($nodes as $node) {
|
||||
if ($node instanceof Class_) {
|
||||
$this->classNode = $node;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getClassName(): string
|
||||
{
|
||||
return $this->classNode->namespacedName->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|int|Node
|
||||
*/
|
||||
|
@ -13,7 +13,7 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if ($node instanceof ClassConstFetch) {
|
||||
$className = $this->getClassName($node);
|
||||
$className = $this->getClassNameFromClassConstFetch($node);
|
||||
|
||||
if ($className !== $this->getDesiredClass()) {
|
||||
return false;
|
||||
@ -50,7 +50,7 @@ final class RemoveConfiguratorConstantsRector extends AbstractRector
|
||||
return 2.3;
|
||||
}
|
||||
|
||||
private function getClassName(ClassConstFetch $classConstFetchNode): string
|
||||
private function getClassNameFromClassConstFetch(ClassConstFetch $classConstFetchNode): string
|
||||
{
|
||||
/** @var Node\Name\FullyQualified $fqnName */
|
||||
$fqnName = $classConstFetchNode->class->getAttribute('resolvedName');
|
||||
|
@ -7,7 +7,6 @@ use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Builder\Class_\ClassPropertyCollector;
|
||||
use Rector\Builder\Kernel\ServiceFromKernelResolver;
|
||||
use Rector\Builder\Naming\NameResolver;
|
||||
@ -56,11 +55,6 @@ final class CommandToConstructorInjectionRector extends AbstractRector
|
||||
*/
|
||||
private $nameResolver;
|
||||
|
||||
/**
|
||||
* @var Class_
|
||||
*/
|
||||
private $classNode;
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
@ -88,25 +82,6 @@ final class CommandToConstructorInjectionRector extends AbstractRector
|
||||
return 3.3;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo add node traverser for this or to AbstractRector
|
||||
* @param Node[] $nodes
|
||||
* @return null|Node[]
|
||||
*/
|
||||
public function beforeTraverse(array $nodes): ?array
|
||||
{
|
||||
$this->classNode = null;
|
||||
|
||||
foreach ($nodes as $node) {
|
||||
if ($node instanceof Class_) {
|
||||
$this->classNode = $node;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! Strings::endsWith($this->getClassName(), 'Command')) {
|
||||
@ -138,13 +113,7 @@ final class CommandToConstructorInjectionRector extends AbstractRector
|
||||
{
|
||||
$this->replaceParentContainerAwareCommandWithCommand();
|
||||
|
||||
$serviceName = $node->args[0]->value->value;
|
||||
|
||||
$serviceType = $this->serviceFromKernelResolver->resolveServiceClassByNameFromKernel(
|
||||
$serviceName,
|
||||
LocalKernel::class
|
||||
);
|
||||
|
||||
$serviceType = $this->serviceFromKernelResolver->resolveServiceClassFromArgument($node->args[0], LocalKernel::class);
|
||||
if ($serviceType === null) {
|
||||
return null;
|
||||
}
|
||||
@ -156,14 +125,6 @@ final class CommandToConstructorInjectionRector extends AbstractRector
|
||||
return $this->nodeFactory->createLocalPropertyFetch($propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo move to parent class?
|
||||
*/
|
||||
private function getClassName(): string
|
||||
{
|
||||
return $this->classNode->namespacedName->toString();
|
||||
}
|
||||
|
||||
private function replaceParentContainerAwareCommandWithCommand(): void
|
||||
{
|
||||
$this->classNode->extends = new Name('\Symfony\Component\Console\Command\Command');
|
||||
|
@ -7,7 +7,6 @@ use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Builder\Class_\ClassPropertyCollector;
|
||||
use Rector\Builder\Kernel\ServiceFromKernelResolver;
|
||||
use Rector\Builder\Naming\NameResolver;
|
||||
@ -40,11 +39,6 @@ final class GetterToPropertyRector extends AbstractRector
|
||||
*/
|
||||
private $classPropertyCollector;
|
||||
|
||||
/**
|
||||
* @var Class_
|
||||
*/
|
||||
private $classNode;
|
||||
|
||||
/**
|
||||
* @var NodeFactory
|
||||
*/
|
||||
@ -62,25 +56,6 @@ final class GetterToPropertyRector extends AbstractRector
|
||||
$this->nodeFactory = $nodeFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo add node traverser for this or to AbstractRector
|
||||
* @param Node[] $nodes
|
||||
* @return null|Node[]
|
||||
*/
|
||||
public function beforeTraverse(array $nodes): ?array
|
||||
{
|
||||
$this->classNode = null;
|
||||
|
||||
foreach ($nodes as $node) {
|
||||
if ($node instanceof Class_) {
|
||||
$this->classNode = $node;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
// finds $var = $this->get('some_service');
|
||||
@ -152,15 +127,7 @@ final class GetterToPropertyRector extends AbstractRector
|
||||
|
||||
private function processMethodCallNode(MethodCall $methodCall): ?PropertyFetch
|
||||
{
|
||||
/** @var String_ $argument */
|
||||
$argument = $methodCall->args[0]->value;
|
||||
$serviceName = $argument->value;
|
||||
|
||||
$serviceType = $this->serviceFromKernelResolver->resolveServiceClassByNameFromKernel(
|
||||
$serviceName,
|
||||
LocalKernel::class
|
||||
);
|
||||
|
||||
$serviceType = $this->serviceFromKernelResolver->resolveServiceClassFromArgument($methodCall->args[0], LocalKernel::class);
|
||||
if ($serviceType === null) {
|
||||
return null;
|
||||
}
|
||||
@ -171,9 +138,4 @@ final class GetterToPropertyRector extends AbstractRector
|
||||
|
||||
return $this->nodeFactory->createLocalPropertyFetch($propertyName);
|
||||
}
|
||||
|
||||
private function getClassName(): string
|
||||
{
|
||||
return $this->classNode->namespacedName->toString();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user