Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

40 changes: 8 additions & 32 deletions rules/Transform/Rector/MethodCall/MethodCallToStaticCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,19 @@

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\Transform\ValueObject\MethodCallToStaticCall;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToStaticCallRector\MethodCallToStaticCallRectorTest
* @deprecated This rule is deprecated, as turning a method call into a static call depends on project context and this generic rule is too niche. Write a custom rule for the exact project case instead.
*/
final class MethodCallToStaticCallRector extends AbstractRector implements ConfigurableRectorInterface
final class MethodCallToStaticCallRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var array<string, MethodCallToStaticCall[]>
*/
private array $methodCallsToStaticCallsByOldMethod = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change method call to desired static call', [
Expand Down Expand Up @@ -79,36 +75,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}

foreach ($this->methodCallsToStaticCallsByOldMethod[strtolower($methodName)] ?? [] as $methodCallToStaticCall) {
if (! $this->isObjectType($node->var, $methodCallToStaticCall->getOldObjectType())) {
continue;
}

return $this->nodeFactory->createStaticCall(
$methodCallToStaticCall->getNewClass(),
$methodCallToStaticCall->getNewMethod(),
$node->args
);
}

return null;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as turning a method call into a static call depends on project context; write a custom rule for the exact project case instead',
self::class
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, MethodCallToStaticCall::class);

$this->methodCallsToStaticCallsByOldMethod = [];
foreach ($configuration as $methodCallToStaticCall) {
$this->methodCallsToStaticCallsByOldMethod[strtolower($methodCallToStaticCall->getOldMethod())][] = $methodCallToStaticCall;
}
}
}
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/MethodCallToStaticCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PHPStan\Type\ObjectType;
use Rector\Validation\RectorAssert;

/**
* @api used in deprecated MethodCallToStaticCallRector configs
*/
final readonly class MethodCallToStaticCall
{
public function __construct(
Expand Down
Loading