mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-02 17:30:50 +00:00
4f456a1654
5c1be93b80
PHP 8.2 is supported (#3717)
50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\Symfony\NodeAnalyzer;
|
|
|
|
use PhpParser\Node\Expr\CallLike;
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
use PhpParser\Node\Expr\New_;
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
use PhpParser\Node\Scalar\LNumber;
|
|
use PhpParser\Node\Scalar\String_;
|
|
use Rector\Core\PhpParser\Node\NodeFactory;
|
|
final class LiteralCallLikeConstFetchReplacer
|
|
{
|
|
/**
|
|
* @readonly
|
|
* @var \Rector\Core\PhpParser\Node\NodeFactory
|
|
*/
|
|
private $nodeFactory;
|
|
public function __construct(NodeFactory $nodeFactory)
|
|
{
|
|
$this->nodeFactory = $nodeFactory;
|
|
}
|
|
/**
|
|
* @template TCallLike as MethodCall|New_|StaticCall
|
|
*
|
|
* @param TCallLike $callLike
|
|
* @param array<string|int, string> $constantMap
|
|
* @return TCallLike
|
|
*/
|
|
public function replaceArgOnPosition(CallLike $callLike, int $argPosition, string $className, array $constantMap)
|
|
{
|
|
$args = $callLike->getArgs();
|
|
if (!isset($args[$argPosition])) {
|
|
return null;
|
|
}
|
|
$arg = $args[$argPosition];
|
|
if (!$arg->value instanceof String_ && !$arg->value instanceof LNumber) {
|
|
return null;
|
|
}
|
|
$scalar = $arg->value;
|
|
$constantName = $constantMap[$scalar->value] ?? null;
|
|
if ($constantName === null) {
|
|
return null;
|
|
}
|
|
$arg->value = $this->nodeFactory->createClassConstFetch($className, $constantName);
|
|
return $callLike;
|
|
}
|
|
}
|