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.

44 changes: 8 additions & 36 deletions rules/Transform/Rector/MethodCall/MethodCallToFuncCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,20 @@
namespace Rector\Transform\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
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\MethodCallToFuncCall;
use Webmozart\Assert\Assert;

/**
* @note used extensively https://github.com/search?q=MethodCallToFuncCallRector%3A%3Aclass&type=code
* @see \Rector\Tests\Transform\Rector\MethodCall\MethodCallToFuncCallRector\MethodCallToFuncCallRectorTest
* @deprecated This rule is deprecated, as changing a method call to a function call is niche and depends on project context. Write a custom rule for the exact project case instead.
*/
final class MethodCallToFuncCallRector extends AbstractRector implements ConfigurableRectorInterface
final class MethodCallToFuncCallRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var array<string, MethodCallToFuncCall[]>
*/
private array $methodCallsToFuncCallsByMethod = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change method call to function call', [
Expand Down Expand Up @@ -70,36 +62,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->isFirstClassCallable()) {
return null;
}

$methodName = $this->getName($node->name);
if ($methodName === null) {
return null;
}

foreach ($this->methodCallsToFuncCallsByMethod[strtolower($methodName)] ?? [] as $methodCallToFuncCall) {
if (! $this->isObjectType($node->var, new ObjectType($methodCallToFuncCall->getObjectType()))) {
continue;
}

return new FuncCall(new FullyQualified($methodCallToFuncCall->getFunctionName()), $node->getArgs());
}

return null;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as changing a method call to a function call is niche and 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::allIsInstanceOf($configuration, MethodCallToFuncCall::class);

$this->methodCallsToFuncCallsByMethod = [];
foreach ($configuration as $methodCallToFuncCall) {
$this->methodCallsToFuncCallsByMethod[strtolower($methodCallToFuncCall->getMethodName())][] = $methodCallToFuncCall;
}
}
}
3 changes: 3 additions & 0 deletions rules/Transform/ValueObject/MethodCallToFuncCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Rector\Transform\ValueObject;

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