mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-29 07:20:52 +00:00
e9e85a1300
a8b01f217b
[automated] Re-Generate Nodes/Rectors Documentation (#619)
167 lines
5.7 KiB
PHP
167 lines
5.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Transform\Rector\MethodCall;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
use PhpParser\Node\Expr\Variable;
|
|
use PhpParser\Node\Identifier;
|
|
use PhpParser\Node\Stmt\Class_;
|
|
use PHPStan\Type\ObjectType;
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\Naming\Naming\PropertyNaming;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Rector\PostRector\Collector\PropertyToAddCollector;
|
|
use Rector\PostRector\ValueObject\PropertyMetadata;
|
|
use Rector\Transform\ValueObject\ServiceGetterToConstructorInjection;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
use RectorPrefix20210808\Webmozart\Assert\Assert;
|
|
/**
|
|
* @see \Rector\Tests\Transform\Rector\MethodCall\ServiceGetterToConstructorInjectionRector\ServiceGetterToConstructorInjectionRectorTest
|
|
*/
|
|
final class ServiceGetterToConstructorInjectionRector extends \Rector\Core\Rector\AbstractRector implements \Rector\Core\Contract\Rector\ConfigurableRectorInterface
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
public const METHOD_CALL_TO_SERVICES = 'method_call_to_services';
|
|
/**
|
|
* @var ServiceGetterToConstructorInjection[]
|
|
*/
|
|
private $methodCallToServices = [];
|
|
/**
|
|
* @var \Rector\Naming\Naming\PropertyNaming
|
|
*/
|
|
private $propertyNaming;
|
|
/**
|
|
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
|
|
*/
|
|
private $classAnalyzer;
|
|
/**
|
|
* @var \Rector\PostRector\Collector\PropertyToAddCollector
|
|
*/
|
|
private $propertyToAddCollector;
|
|
public function __construct(\Rector\Naming\Naming\PropertyNaming $propertyNaming, \Rector\Core\NodeAnalyzer\ClassAnalyzer $classAnalyzer, \Rector\PostRector\Collector\PropertyToAddCollector $propertyToAddCollector)
|
|
{
|
|
$this->propertyNaming = $propertyNaming;
|
|
$this->classAnalyzer = $classAnalyzer;
|
|
$this->propertyToAddCollector = $propertyToAddCollector;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Get service call to constructor injection', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
/**
|
|
* @var FirstService
|
|
*/
|
|
private $firstService;
|
|
|
|
public function __construct(FirstService $firstService)
|
|
{
|
|
$this->firstService = $firstService;
|
|
}
|
|
|
|
public function run()
|
|
{
|
|
$anotherService = $this->firstService->getAnotherService();
|
|
$anotherService->run();
|
|
}
|
|
}
|
|
|
|
class FirstService
|
|
{
|
|
/**
|
|
* @var AnotherService
|
|
*/
|
|
private $anotherService;
|
|
|
|
public function __construct(AnotherService $anotherService)
|
|
{
|
|
$this->anotherService = $anotherService;
|
|
}
|
|
|
|
public function getAnotherService(): AnotherService
|
|
{
|
|
return $this->anotherService;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
/**
|
|
* @var FirstService
|
|
*/
|
|
private $firstService;
|
|
|
|
/**
|
|
* @var AnotherService
|
|
*/
|
|
private $anotherService;
|
|
|
|
public function __construct(FirstService $firstService, AnotherService $anotherService)
|
|
{
|
|
$this->firstService = $firstService;
|
|
$this->anotherService = $anotherService;
|
|
}
|
|
|
|
public function run()
|
|
{
|
|
$anotherService = $this->anotherService;
|
|
$anotherService->run();
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, [self::METHOD_CALL_TO_SERVICES => [new \Rector\Transform\ValueObject\ServiceGetterToConstructorInjection('FirstService', 'getAnotherService', 'AnotherService')]])]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Expr\MethodCall::class];
|
|
}
|
|
/**
|
|
* @param MethodCall $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
$classLike = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
|
if (!$classLike instanceof \PhpParser\Node\Stmt\Class_) {
|
|
return null;
|
|
}
|
|
if ($this->classAnalyzer->isAnonymousClass($classLike)) {
|
|
return null;
|
|
}
|
|
foreach ($this->methodCallToServices as $methodCallToService) {
|
|
if (!$this->isObjectType($node->var, $methodCallToService->getOldObjectType())) {
|
|
continue;
|
|
}
|
|
if (!$this->isName($node->name, $methodCallToService->getOldMethod())) {
|
|
continue;
|
|
}
|
|
$serviceObjectType = new \PHPStan\Type\ObjectType($methodCallToService->getServiceType());
|
|
$propertyName = $this->propertyNaming->fqnToVariableName($serviceObjectType);
|
|
$propertyMetadata = new \Rector\PostRector\ValueObject\PropertyMetadata($propertyName, $serviceObjectType, \PhpParser\Node\Stmt\Class_::MODIFIER_PRIVATE);
|
|
$this->propertyToAddCollector->addPropertyToClass($classLike, $propertyMetadata);
|
|
return new \PhpParser\Node\Expr\PropertyFetch(new \PhpParser\Node\Expr\Variable('this'), new \PhpParser\Node\Identifier($propertyName));
|
|
}
|
|
return $node;
|
|
}
|
|
/**
|
|
* @param array<string, ServiceGetterToConstructorInjection[]> $configuration
|
|
*/
|
|
public function configure(array $configuration) : void
|
|
{
|
|
$methodCallToServices = $configuration[self::METHOD_CALL_TO_SERVICES] ?? [];
|
|
\RectorPrefix20210808\Webmozart\Assert\Assert::allIsInstanceOf($methodCallToServices, \Rector\Transform\ValueObject\ServiceGetterToConstructorInjection::class);
|
|
$this->methodCallToServices = $methodCallToServices;
|
|
}
|
|
}
|