2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-12-15 17:53:38 +00:00
|
|
|
|
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;
|
2020-09-07 13:38:03 +00:00
|
|
|
use PhpParser\Node\Identifier;
|
2018-12-15 21:05:44 +00:00
|
|
|
use PhpParser\Node\Name;
|
2020-09-07 13:38:03 +00:00
|
|
|
use PhpParser\Node\NullableType;
|
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-07 13:38:03 +00:00
|
|
|
use PhpParser\Node\UnionType;
|
2020-09-01 17:56:30 +00:00
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\PhpParser\Parser\InlineCodeParser;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://stackoverflow.com/q/48161526/1348344
|
|
|
|
* @see http://php.net/manual/en/migration72.deprecated.php#migration72.deprecated.create_function-function
|
2020-09-01 17:56:30 +00:00
|
|
|
*
|
2019-09-23 11:43:13 +00:00
|
|
|
* @see \Rector\Php72\Tests\Rector\FuncCall\CreateFunctionToAnonymousFunctionRector\CreateFunctionToAnonymousFunctionRectorTest
|
2018-12-15 17:53:38 +00:00
|
|
|
*/
|
2020-09-07 13:38:03 +00:00
|
|
|
final class CreateFunctionToAnonymousFunctionRector extends AbstractConvertToAnonymousFunctionRector
|
2018-12-15 17:53:38 +00:00
|
|
|
{
|
|
|
|
/**
|
2019-02-23 09:32:27 +00:00
|
|
|
* @var InlineCodeParser
|
2018-12-15 17:53:38 +00:00
|
|
|
*/
|
2019-02-23 09:32:27 +00:00
|
|
|
private $inlineCodeParser;
|
2018-12-15 17:53:38 +00:00
|
|
|
|
2019-03-26 22:56:10 +00:00
|
|
|
public function __construct(InlineCodeParser $inlineCodeParser)
|
2018-12-15 17:53:38 +00:00
|
|
|
{
|
2019-02-23 09:32:27 +00:00
|
|
|
$this->inlineCodeParser = $inlineCodeParser;
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-12-15 17:53:38 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Use anonymous functions instead of deprecated create_function()',
|
|
|
|
[
|
|
|
|
new 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
|
2020-11-16 17:50:38 +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
|
2020-11-16 17:50:38 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
2020-09-07 13:38:03 +00:00
|
|
|
public function shouldSkip(Node $node): bool
|
2018-12-15 17:53:38 +00:00
|
|
|
{
|
2020-09-07 13:38:03 +00:00
|
|
|
return ! $this->isName($node, 'create_function');
|
|
|
|
}
|
2018-12-15 17:53:38 +00:00
|
|
|
|
2020-09-07 13:38:03 +00:00
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
* @return Param[]
|
|
|
|
*/
|
|
|
|
public function getParameters(Node $node): array
|
|
|
|
{
|
|
|
|
return $this->parseStringToParameters($node->args[0]->value);
|
|
|
|
}
|
2018-12-15 17:53:38 +00:00
|
|
|
|
2020-09-07 13:38:03 +00:00
|
|
|
/**
|
|
|
|
* @return Identifier|Name|NullableType|UnionType|null
|
|
|
|
*/
|
|
|
|
public function getReturnType(Node $node): ?Node
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2018-12-15 17:53:38 +00:00
|
|
|
|
2020-09-07 13:38:03 +00:00
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
* @return Stmt[]
|
|
|
|
*/
|
|
|
|
public function getBody(Node $node): array
|
|
|
|
{
|
|
|
|
return $this->parseStringToBody($node->args[1]->value);
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 13:24:31 +00:00
|
|
|
/**
|
|
|
|
* @return Param[]
|
|
|
|
*/
|
|
|
|
private function parseStringToParameters(Expr $expr): array
|
|
|
|
{
|
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);
|
2018-12-16 13:24:31 +00:00
|
|
|
|
2020-09-01 17:56:30 +00:00
|
|
|
/** @var Expression $expression */
|
|
|
|
$expression = $nodes[0];
|
|
|
|
|
|
|
|
/** @var Assign $assign */
|
|
|
|
$assign = $expression->expr;
|
|
|
|
|
|
|
|
/** @var Closure $function */
|
|
|
|
$function = $assign->expr;
|
|
|
|
if (! $function instanceof Closure) {
|
|
|
|
throw new ShouldNotHappenException();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2020-12-20 12:50:55 +00:00
|
|
|
private function parseStringToBody(Expr $expr): array
|
2018-12-15 17:53:38 +00:00
|
|
|
{
|
2020-12-20 12:50:55 +00:00
|
|
|
if (! $expr instanceof String_ && ! $expr instanceof Encapsed && ! $expr instanceof 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
|
|
|
}
|
2018-12-15 17:53:38 +00:00
|
|
|
|
2019-02-22 17:25:31 +00:00
|
|
|
private function createEval(Expr $expr): Expression
|
2018-12-15 21:05:44 +00:00
|
|
|
{
|
2019-02-22 17:25:31 +00:00
|
|
|
$evalFuncCall = new FuncCall(new Name('eval'), [new Arg($expr)]);
|
2018-12-15 21:05:44 +00:00
|
|
|
|
|
|
|
return new Expression($evalFuncCall);
|
2018-12-15 17:53:38 +00:00
|
|
|
}
|
|
|
|
}
|