2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2019-09-22 18:57:03 +00:00
|
|
|
namespace Rector\Php72\Rector\FuncCall;
|
2018-12-15 17:53:38 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
2018-12-15 21:05:44 +00:00
|
|
|
use PhpParser\Node\Arg;
|
|
|
|
use PhpParser\Node\Expr;
|
2019-02-23 09:32:27 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2018-12-15 21:05:44 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Concat;
|
2018-12-15 17:53:38 +00:00
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
2018-12-15 21:05:44 +00:00
|
|
|
use PhpParser\Node\Name;
|
2018-12-15 17:53:38 +00:00
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Scalar\Encapsed;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2020-09-01 17:56:30 +00:00
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
2021-03-05 12:30:00 +00:00
|
|
|
use Rector\Core\Php\ReservedKeywordAnalyzer;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\PhpParser\Parser\InlineCodeParser;
|
2021-03-03 08:49:57 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Php72\NodeFactory\AnonymousFunctionFactory;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-12-15 17:53:38 +00:00
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
* @changelog https://stackoverflow.com/q/48161526/1348344 http://php.net/manual/en/migration72.deprecated.php#migration72.deprecated.create_function-function
|
2020-09-01 17:56:30 +00:00
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Php72\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\CreateFunctionToAnonymousFunctionRectorTest
|
2018-12-15 17:53:38 +00:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
final class CreateFunctionToAnonymousFunctionRector extends \Rector\Core\Rector\AbstractRector
|
2018-12-15 17:53:38 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Parser\InlineCodeParser
|
2018-12-15 17:53:38 +00:00
|
|
|
*/
|
2019-02-23 09:32:27 +00:00
|
|
|
private $inlineCodeParser;
|
2021-03-03 08:49:57 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Php72\NodeFactory\AnonymousFunctionFactory
|
2021-03-03 08:49:57 +00:00
|
|
|
*/
|
|
|
|
private $anonymousFunctionFactory;
|
2021-03-05 12:30:00 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\Php\ReservedKeywordAnalyzer
|
2021-03-05 12:30:00 +00:00
|
|
|
*/
|
|
|
|
private $reservedKeywordAnalyzer;
|
2021-05-10 22:23:08 +00:00
|
|
|
public function __construct(\Rector\Core\PhpParser\Parser\InlineCodeParser $inlineCodeParser, \Rector\Php72\NodeFactory\AnonymousFunctionFactory $anonymousFunctionFactory, \Rector\Core\Php\ReservedKeywordAnalyzer $reservedKeywordAnalyzer)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2019-02-23 09:32:27 +00:00
|
|
|
$this->inlineCodeParser = $inlineCodeParser;
|
2021-03-03 08:49:57 +00:00
|
|
|
$this->anonymousFunctionFactory = $anonymousFunctionFactory;
|
2021-03-05 12:30:00 +00:00
|
|
|
$this->reservedKeywordAnalyzer = $reservedKeywordAnalyzer;
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
2018-12-15 17:53:38 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Use anonymous functions instead of deprecated create_function()', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
2018-12-15 17:53:38 +00:00
|
|
|
class ClassWithCreateFunction
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$callable = create_function('$matches', "return '$delimiter' . strtolower(\$matches[1]);");
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-15 17:53:38 +00:00
|
|
|
class ClassWithCreateFunction
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$callable = function($matches) use ($delimiter) {
|
|
|
|
return $delimiter . strtolower($matches[1]);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-15 17:53:38 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-15 17:53:38 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
return [\PhpParser\Node\Expr\FuncCall::class];
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-07-05 22:50:18 +00:00
|
|
|
* @param FuncCall $node
|
2021-03-03 08:49:57 +00:00
|
|
|
* @return Closure|null
|
2018-12-15 17:53:38 +00:00
|
|
|
*/
|
2021-07-05 22:50:18 +00:00
|
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
2020-09-07 13:38:03 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isName($node, 'create_function')) {
|
2021-03-03 08:49:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$params = $this->createParamsFromString($node->args[0]->value);
|
|
|
|
$stmts = $this->parseStringToBody($node->args[1]->value);
|
2021-03-05 12:30:00 +00:00
|
|
|
$refactored = $this->anonymousFunctionFactory->create($params, $stmts, null);
|
|
|
|
foreach ($refactored->uses as $key => $use) {
|
|
|
|
$variableName = $this->getName($use->var);
|
|
|
|
if ($variableName === null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->reservedKeywordAnalyzer->isNativeVariable($variableName)) {
|
|
|
|
unset($refactored->uses[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $refactored;
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
2018-12-16 13:24:31 +00:00
|
|
|
/**
|
|
|
|
* @return Param[]
|
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
private function createParamsFromString(\PhpParser\Node\Expr $expr) : array
|
2018-12-16 13:24:31 +00:00
|
|
|
{
|
2019-02-23 09:32:27 +00:00
|
|
|
$content = $this->inlineCodeParser->stringify($expr);
|
2018-12-16 13:24:31 +00:00
|
|
|
$content = '<?php $value = function(' . $content . ') {};';
|
2019-02-23 09:32:27 +00:00
|
|
|
$nodes = $this->inlineCodeParser->parse($content);
|
2020-09-01 17:56:30 +00:00
|
|
|
/** @var Expression $expression */
|
|
|
|
$expression = $nodes[0];
|
|
|
|
/** @var Assign $assign */
|
|
|
|
$assign = $expression->expr;
|
|
|
|
$function = $assign->expr;
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$function instanceof \PhpParser\Node\Expr\Closure) {
|
|
|
|
throw new \Rector\Core\Exception\ShouldNotHappenException();
|
2020-09-01 17:56:30 +00:00
|
|
|
}
|
|
|
|
return $function->params;
|
2018-12-16 13:24:31 +00:00
|
|
|
}
|
2018-12-15 17:53:38 +00:00
|
|
|
/**
|
2020-09-01 17:56:30 +00:00
|
|
|
* @return Expression[]|Stmt[]
|
2018-12-15 17:53:38 +00:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
private function parseStringToBody(\PhpParser\Node\Expr $expr) : array
|
2018-12-15 17:53:38 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$expr instanceof \PhpParser\Node\Scalar\String_ && !$expr instanceof \PhpParser\Node\Scalar\Encapsed && !$expr instanceof \PhpParser\Node\Expr\BinaryOp\Concat) {
|
2018-12-15 21:05:44 +00:00
|
|
|
// special case of code elsewhere
|
2020-12-20 12:50:55 +00:00
|
|
|
return [$this->createEval($expr)];
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
2020-12-20 12:50:55 +00:00
|
|
|
$expr = $this->inlineCodeParser->stringify($expr);
|
|
|
|
return $this->inlineCodeParser->parse($expr);
|
2018-12-15 21:05:44 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function createEval(\PhpParser\Node\Expr $expr) : \PhpParser\Node\Stmt\Expression
|
2018-12-15 21:05:44 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$evalFuncCall = new \PhpParser\Node\Expr\FuncCall(new \PhpParser\Node\Name('eval'), [new \PhpParser\Node\Arg($expr)]);
|
|
|
|
return new \PhpParser\Node\Stmt\Expression($evalFuncCall);
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
|
|
|
}
|