phabel/src/Target/Php70/ClosureCallReplacer.php

48 lines
1.0 KiB
PHP
Raw Normal View History

2020-08-03 21:31:32 +02:00
<?php
namespace Phabel\Target\Php70;
use Phabel\Plugin;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
2020-09-05 22:35:30 +02:00
/**
* @author Daniil Gentili <daniil@daniil.it>
* @license MIT
*/
2020-08-03 21:31:32 +02:00
class ClosureCallReplacer extends Plugin
{
/**
2020-08-09 13:49:19 +02:00
* Replace composite function calls.
2020-08-03 21:31:32 +02:00
*
* @param FuncCall $node Function call
2020-08-09 13:49:19 +02:00
*
2020-08-03 21:31:32 +02:00
* @return StaticCall|null
*/
public function enter(FuncCall $node): ?StaticCall
{
$name = $node->name;
if ($name instanceof Name || $name instanceof Variable) {
return null;
}
\array_unshift($node->args, new Arg($name));
return self::callPoly('callMe', ...$node->args);
}
/**
* Call provided argument.
*
* @param callable $callable Callable
* @param mixed ...$arguments Arguments
*
* @return mixed
*/
public static function callMe(callable $callable, ...$arguments)
{
return $callable($arguments);
}
}