mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-06 19:30:50 +00:00
91 lines
2.7 KiB
PHP
91 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Transform\Rector\MethodCall;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
use Rector\Rector\AbstractRector;
|
|
use Rector\Transform\ValueObject\MethodCallToStaticCall;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
use RectorPrefix202404\Webmozart\Assert\Assert;
|
|
/**
|
|
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToStaticCallRector\MethodCallToStaticCallRectorTest
|
|
*/
|
|
final class MethodCallToStaticCallRector extends AbstractRector implements ConfigurableRectorInterface
|
|
{
|
|
/**
|
|
* @var MethodCallToStaticCall[]
|
|
*/
|
|
private $methodCallsToStaticCalls = [];
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Change method call to desired static call', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
private $anotherDependency;
|
|
|
|
public function __construct(AnotherDependency $anotherDependency)
|
|
{
|
|
$this->anotherDependency = $anotherDependency;
|
|
}
|
|
|
|
public function loadConfiguration()
|
|
{
|
|
return $this->anotherDependency->process('value');
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
final class SomeClass
|
|
{
|
|
private $anotherDependency;
|
|
|
|
public function __construct(AnotherDependency $anotherDependency)
|
|
{
|
|
$this->anotherDependency = $anotherDependency;
|
|
}
|
|
|
|
public function loadConfiguration()
|
|
{
|
|
return StaticCaller::anotherMethod('value');
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, [new MethodCallToStaticCall('AnotherDependency', 'process', 'StaticCaller', 'anotherMethod')])]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [MethodCall::class];
|
|
}
|
|
/**
|
|
* @param MethodCall $node
|
|
*/
|
|
public function refactor(Node $node) : ?Node
|
|
{
|
|
foreach ($this->methodCallsToStaticCalls as $methodCallToStaticCall) {
|
|
if (!$this->isName($node->name, $methodCallToStaticCall->getOldMethod())) {
|
|
continue;
|
|
}
|
|
if (!$this->isObjectType($node->var, $methodCallToStaticCall->getOldObjectType())) {
|
|
continue;
|
|
}
|
|
return $this->nodeFactory->createStaticCall($methodCallToStaticCall->getNewClass(), $methodCallToStaticCall->getNewMethod(), $node->args);
|
|
}
|
|
return null;
|
|
}
|
|
/**
|
|
* @param mixed[] $configuration
|
|
*/
|
|
public function configure(array $configuration) : void
|
|
{
|
|
Assert::allIsAOf($configuration, MethodCallToStaticCall::class);
|
|
$this->methodCallsToStaticCalls = $configuration;
|
|
}
|
|
}
|