2023-04-10 08:19:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace Rector\Removing\Rector\FuncCall;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2023-06-05 10:07:36 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2023-04-10 08:19:12 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2024-04-01 16:51:34 +00:00
|
|
|
use RectorPrefix202404\Webmozart\Assert\Assert;
|
2023-04-10 08:19:12 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\Removing\Rector\FuncCall\RemoveFuncCallRector\RemoveFuncCallRectorTest
|
|
|
|
*/
|
|
|
|
final class RemoveFuncCallRector extends AbstractRector implements ConfigurableRectorInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $removedFunctions = [];
|
|
|
|
public function getRuleDefinition() : RuleDefinition
|
|
|
|
{
|
|
|
|
return new RuleDefinition('Remove function', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
|
|
|
$x = 'something';
|
|
|
|
var_dump($x);
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
$x = 'something';
|
|
|
|
CODE_SAMPLE
|
|
|
|
, ['var_dump'])]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
|
|
|
return [Expression::class];
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Expression $node
|
|
|
|
*/
|
2023-06-05 10:07:36 +00:00
|
|
|
public function refactor(Node $node) : ?int
|
2023-04-10 08:19:12 +00:00
|
|
|
{
|
|
|
|
$expr = $node->expr;
|
|
|
|
if (!$expr instanceof FuncCall) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-05 10:07:36 +00:00
|
|
|
foreach ($this->removedFunctions as $removedFunction) {
|
|
|
|
if (!$this->isName($expr->name, $removedFunction)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return NodeTraverser::REMOVE_NODE;
|
|
|
|
}
|
2023-04-10 08:19:12 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param mixed[] $configuration
|
|
|
|
*/
|
|
|
|
public function configure(array $configuration) : void
|
|
|
|
{
|
|
|
|
Assert::allString($configuration);
|
|
|
|
$this->removedFunctions = $configuration;
|
|
|
|
}
|
|
|
|
}
|