rector/packages/Php72/src/Rector/FuncCall/CreateFunctionToAnonymousFunctionRector.php

194 lines
5.3 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Rector\Php72\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Parser\InlineCodeParser;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://stackoverflow.com/q/48161526/1348344
* @see http://php.net/manual/en/migration72.deprecated.php#migration72.deprecated.create_function-function
* @see \Rector\Php72\Tests\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\CreateFunctionToAnonymousFunctionRectorTest
*/
final class CreateFunctionToAnonymousFunctionRector extends AbstractRector
{
/**
* @var InlineCodeParser
*/
private $inlineCodeParser;
2019-03-26 22:56:10 +00:00
public function __construct(InlineCodeParser $inlineCodeParser)
{
$this->inlineCodeParser = $inlineCodeParser;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Use anonymous functions instead of deprecated create_function()', [
new CodeSample(
2019-09-18 06:14:35 +00:00
<<<'PHP'
class ClassWithCreateFunction
{
public function run()
{
$callable = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
}
}
2019-09-18 06:14:35 +00:00
PHP
,
2019-09-18 06:14:35 +00:00
<<<'PHP'
class ClassWithCreateFunction
{
public function run()
{
$callable = function($matches) use ($delimiter) {
return $delimiter . strtolower($matches[1]);
};
}
}
2019-09-18 06:14:35 +00:00
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'create_function')) {
return null;
}
/** @var Variable[] $parameters */
$parameters = $this->parseStringToParameters($node->args[0]->value);
$body = $this->parseStringToBody($node->args[1]->value);
$useVariables = $this->resolveUseVariables($body, $parameters);
$anonymousFunctionNode = new Closure();
foreach ($parameters as $parameter) {
/** @var Variable $parameter */
$anonymousFunctionNode->params[] = new Param($parameter);
}
2019-02-17 14:12:47 +00:00
if ($body !== []) {
$anonymousFunctionNode->stmts = $body;
}
foreach ($useVariables as $useVariable) {
$anonymousFunctionNode->uses[] = new ClosureUse($useVariable);
}
return $anonymousFunctionNode;
}
2018-12-16 13:24:31 +00:00
/**
* @return Param[]
*/
private function parseStringToParameters(Expr $expr): array
{
$content = $this->inlineCodeParser->stringify($expr);
2018-12-16 13:24:31 +00:00
$content = '<?php $value = function(' . $content . ') {};';
$nodes = $this->inlineCodeParser->parse($content);
2018-12-16 13:24:31 +00:00
return $nodes[0]->expr->expr->params;
}
/**
2019-02-22 17:25:31 +00:00
* @param String_|Expr $node
* @return Stmt[]
*/
2019-02-22 17:25:31 +00:00
private function parseStringToBody(Node $node): array
{
2019-02-22 17:25:31 +00:00
if (! $node instanceof String_ && ! $node instanceof Encapsed && ! $node instanceof Concat) {
// special case of code elsewhere
2019-02-22 17:25:31 +00:00
return [$this->createEval($node)];
}
$node = $this->inlineCodeParser->stringify($node);
return $this->inlineCodeParser->parse($node);
}
/**
* @param Node[] $nodes
* @param Variable[] $paramNodes
* @return Variable[]
*/
private function resolveUseVariables(array $nodes, array $paramNodes): array
{
$paramNames = [];
foreach ($paramNodes as $paramNode) {
$paramNames[] = $this->getName($paramNode);
}
$variableNodes = $this->betterNodeFinder->findInstanceOf($nodes, Variable::class);
$filteredVariables = [];
$alreadyAssignedVariables = [];
foreach ($variableNodes as $variableNode) {
// "$this" is allowed
if ($this->isName($variableNode, 'this')) {
continue;
}
$variableName = $this->getName($variableNode);
if ($variableName === null) {
continue;
}
if (in_array($variableName, $paramNames, true)) {
continue;
}
$parentNode = $variableNode->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Assign) {
$alreadyAssignedVariables[] = $variableName;
}
if ($this->isNames($variableNode, $alreadyAssignedVariables)) {
continue;
}
$filteredVariables[$variableName] = $variableNode;
}
return $filteredVariables;
}
2019-02-22 17:25:31 +00:00
private function createEval(Expr $expr): Expression
{
2019-02-22 17:25:31 +00:00
$evalFuncCall = new FuncCall(new Name('eval'), [new Arg($expr)]);
return new Expression($evalFuncCall);
}
}