2021-03-03 09:49:57 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-03-03 09:49:57 +01:00
|
|
|
namespace Rector\Php72\NodeFactory;
|
|
|
|
|
2021-11-28 00:36:13 +00:00
|
|
|
use RectorPrefix20211128\Nette\Utils\Strings;
|
2021-03-03 09:49:57 +01:00
|
|
|
use PhpParser\Node;
|
2021-09-27 15:43:15 +00:00
|
|
|
use PhpParser\Node\ComplexType;
|
2021-06-10 10:46:24 +00:00
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
2021-10-07 08:01:49 +00:00
|
|
|
use PhpParser\Node\Expr\ArrowFunction;
|
2021-03-03 09:49:57 +01:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2021-07-05 09:07:12 +00:00
|
|
|
use PhpParser\Node\Expr\ClassConstFetch;
|
2021-03-03 09:49:57 +01:00
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\ClosureUse;
|
2021-06-10 10:46:24 +00:00
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
2021-07-31 11:43:58 +00:00
|
|
|
use PhpParser\Node\Expr\New_;
|
2021-07-05 09:07:12 +00:00
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
2021-03-03 09:49:57 +01:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Name;
|
2021-07-31 11:43:58 +00:00
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
2021-03-03 09:49:57 +01:00
|
|
|
use PhpParser\Node\NullableType;
|
|
|
|
use PhpParser\Node\Param;
|
2021-06-10 10:46:24 +00:00
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
2021-03-03 09:49:57 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
2021-06-10 10:46:24 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2021-10-22 03:28:32 +00:00
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
2021-06-10 10:46:24 +00:00
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2021-03-03 09:49:57 +01:00
|
|
|
use PhpParser\Node\UnionType;
|
2021-06-10 10:46:24 +00:00
|
|
|
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
|
|
|
|
use PHPStan\Reflection\ParameterReflection;
|
2021-06-28 17:10:14 +00:00
|
|
|
use PHPStan\Reflection\ParametersAcceptorSelector;
|
2021-06-10 10:46:24 +00:00
|
|
|
use PHPStan\Reflection\Php\PhpMethodReflection;
|
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use PHPStan\Type\VoidType;
|
|
|
|
use Rector\Core\Exception\ShouldNotHappenException;
|
2021-10-23 01:58:01 +00:00
|
|
|
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
2021-03-03 09:49:57 +01:00
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
2021-06-10 10:46:24 +00:00
|
|
|
use Rector\Core\PhpParser\Node\NodeFactory;
|
2021-10-26 12:39:52 +00:00
|
|
|
use Rector\Core\PhpParser\Parser\SimplePhpParser;
|
2021-03-03 09:49:57 +01:00
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-10-27 19:25:02 +00:00
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
2021-06-10 10:46:24 +00:00
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2021-11-28 00:36:13 +00:00
|
|
|
use RectorPrefix20211128\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
|
2021-03-03 09:49:57 +01:00
|
|
|
final class AnonymousFunctionFactory
|
|
|
|
{
|
2021-06-10 10:46:24 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
* @see https://regex101.com/r/jkLLlM/2
|
|
|
|
*/
|
|
|
|
private const DIM_FETCH_REGEX = '#(\\$|\\\\|\\x0)(?<number>\\d+)#';
|
2021-03-03 09:49:57 +01:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2021-03-03 09:49:57 +01:00
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
2021-03-03 09:49:57 +01:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2021-06-10 10:46:24 +00:00
|
|
|
/**
|
|
|
|
* @var \Rector\Core\PhpParser\Node\NodeFactory
|
|
|
|
*/
|
|
|
|
private $nodeFactory;
|
|
|
|
/**
|
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser
|
2021-06-10 10:46:24 +00:00
|
|
|
*/
|
|
|
|
private $simpleCallableNodeTraverser;
|
|
|
|
/**
|
2021-10-26 12:39:52 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Parser\SimplePhpParser
|
2021-06-10 10:46:24 +00:00
|
|
|
*/
|
2021-10-26 12:39:52 +00:00
|
|
|
private $simplePhpParser;
|
2021-10-23 01:58:01 +00:00
|
|
|
/**
|
|
|
|
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
|
|
|
*/
|
|
|
|
private $nodeComparator;
|
2021-11-28 00:36:13 +00:00
|
|
|
public function __construct(\Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\Core\PhpParser\Node\BetterNodeFinder $betterNodeFinder, \Rector\Core\PhpParser\Node\NodeFactory $nodeFactory, \Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper, \RectorPrefix20211128\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser $simpleCallableNodeTraverser, \Rector\Core\PhpParser\Parser\SimplePhpParser $simplePhpParser, \Rector\Core\PhpParser\Comparing\NodeComparator $nodeComparator)
|
2021-03-03 09:49:57 +01:00
|
|
|
{
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2021-06-10 10:46:24 +00:00
|
|
|
$this->nodeFactory = $nodeFactory;
|
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
|
|
|
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
2021-10-26 12:39:52 +00:00
|
|
|
$this->simplePhpParser = $simplePhpParser;
|
2021-10-23 01:58:01 +00:00
|
|
|
$this->nodeComparator = $nodeComparator;
|
2021-03-03 09:49:57 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Param[] $params
|
|
|
|
* @param Stmt[] $stmts
|
2021-10-30 14:18:31 +00:00
|
|
|
* @param \PhpParser\Node\ComplexType|\PhpParser\Node\Identifier|\PhpParser\Node\Name|\PhpParser\Node\NullableType|\PhpParser\Node\UnionType|null $returnTypeNode
|
2021-03-03 09:49:57 +01:00
|
|
|
*/
|
2021-10-21 17:54:08 +00:00
|
|
|
public function create(array $params, array $stmts, $returnTypeNode, bool $static = \false) : \PhpParser\Node\Expr\Closure
|
2021-03-03 09:49:57 +01:00
|
|
|
{
|
|
|
|
$useVariables = $this->createUseVariablesFromParams($stmts, $params);
|
2021-05-10 22:23:08 +00:00
|
|
|
$anonymousFunctionNode = new \PhpParser\Node\Expr\Closure();
|
2021-03-03 09:49:57 +01:00
|
|
|
$anonymousFunctionNode->params = $params;
|
2021-10-21 17:54:08 +00:00
|
|
|
if ($static) {
|
|
|
|
$anonymousFunctionNode->static = $static;
|
|
|
|
}
|
2021-03-03 09:49:57 +01:00
|
|
|
foreach ($useVariables as $useVariable) {
|
2021-10-23 01:58:01 +00:00
|
|
|
$anonymousFunctionNode = $this->applyNestedUses($anonymousFunctionNode, $useVariable);
|
2021-05-10 22:23:08 +00:00
|
|
|
$anonymousFunctionNode->uses[] = new \PhpParser\Node\Expr\ClosureUse($useVariable);
|
2021-03-03 09:49:57 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($returnTypeNode instanceof \PhpParser\Node) {
|
2021-03-03 10:40:03 +01:00
|
|
|
$anonymousFunctionNode->returnType = $returnTypeNode;
|
2021-03-03 09:49:57 +01:00
|
|
|
}
|
|
|
|
$anonymousFunctionNode->stmts = $stmts;
|
|
|
|
return $anonymousFunctionNode;
|
|
|
|
}
|
2021-07-31 11:43:58 +00:00
|
|
|
public function createFromPhpMethodReflection(\PHPStan\Reflection\Php\PhpMethodReflection $phpMethodReflection, \PhpParser\Node\Expr $expr) : ?\PhpParser\Node\Expr\Closure
|
2021-06-10 10:46:24 +00:00
|
|
|
{
|
|
|
|
/** @var FunctionVariantWithPhpDocs $functionVariantWithPhpDoc */
|
2021-06-28 17:10:14 +00:00
|
|
|
$functionVariantWithPhpDoc = \PHPStan\Reflection\ParametersAcceptorSelector::selectSingle($phpMethodReflection->getVariants());
|
2021-06-10 10:46:24 +00:00
|
|
|
$anonymousFunction = new \PhpParser\Node\Expr\Closure();
|
|
|
|
$newParams = $this->createParams($functionVariantWithPhpDoc->getParameters());
|
|
|
|
$anonymousFunction->params = $newParams;
|
2021-07-31 11:43:58 +00:00
|
|
|
$innerMethodCall = $this->createInnerMethodCall($phpMethodReflection, $expr, $newParams);
|
|
|
|
if ($innerMethodCall === null) {
|
|
|
|
return null;
|
2021-07-05 08:51:49 +00:00
|
|
|
}
|
2021-06-10 10:46:24 +00:00
|
|
|
if (!$functionVariantWithPhpDoc->getReturnType() instanceof \PHPStan\Type\MixedType) {
|
2021-10-27 19:25:02 +00:00
|
|
|
$returnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($functionVariantWithPhpDoc->getReturnType(), \Rector\PHPStanStaticTypeMapper\Enum\TypeKind::RETURN());
|
2021-06-10 10:46:24 +00:00
|
|
|
$anonymousFunction->returnType = $returnType;
|
|
|
|
}
|
|
|
|
// does method return something?
|
|
|
|
if (!$functionVariantWithPhpDoc->getReturnType() instanceof \PHPStan\Type\VoidType) {
|
|
|
|
$anonymousFunction->stmts[] = new \PhpParser\Node\Stmt\Return_($innerMethodCall);
|
|
|
|
} else {
|
|
|
|
$anonymousFunction->stmts[] = new \PhpParser\Node\Stmt\Expression($innerMethodCall);
|
|
|
|
}
|
|
|
|
if ($expr instanceof \PhpParser\Node\Expr\Variable && !$this->nodeNameResolver->isName($expr, 'this')) {
|
|
|
|
$anonymousFunction->uses[] = new \PhpParser\Node\Expr\ClosureUse($expr);
|
|
|
|
}
|
|
|
|
return $anonymousFunction;
|
|
|
|
}
|
|
|
|
public function createAnonymousFunctionFromString(\PhpParser\Node\Expr $expr) : ?\PhpParser\Node\Expr\Closure
|
|
|
|
{
|
|
|
|
if (!$expr instanceof \PhpParser\Node\Scalar\String_) {
|
|
|
|
// not supported yet
|
|
|
|
throw new \Rector\Core\Exception\ShouldNotHappenException();
|
|
|
|
}
|
|
|
|
$phpCode = '<?php ' . $expr->value . ';';
|
2021-10-26 12:39:52 +00:00
|
|
|
$contentStmts = $this->simplePhpParser->parseString($phpCode);
|
2021-06-10 10:46:24 +00:00
|
|
|
$anonymousFunction = new \PhpParser\Node\Expr\Closure();
|
2021-10-26 12:39:52 +00:00
|
|
|
$firstNode = $contentStmts[0] ?? null;
|
2021-06-10 10:46:24 +00:00
|
|
|
if (!$firstNode instanceof \PhpParser\Node\Stmt\Expression) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$stmt = $firstNode->expr;
|
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmt, function (\PhpParser\Node $node) : Node {
|
|
|
|
if (!$node instanceof \PhpParser\Node\Scalar\String_) {
|
|
|
|
return $node;
|
|
|
|
}
|
2021-11-28 00:36:13 +00:00
|
|
|
$match = \RectorPrefix20211128\Nette\Utils\Strings::match($node->value, self::DIM_FETCH_REGEX);
|
2021-06-10 10:46:24 +00:00
|
|
|
if (!$match) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
$matchesVariable = new \PhpParser\Node\Expr\Variable('matches');
|
|
|
|
return new \PhpParser\Node\Expr\ArrayDimFetch($matchesVariable, new \PhpParser\Node\Scalar\LNumber((int) $match['number']));
|
|
|
|
});
|
|
|
|
$anonymousFunction->stmts[] = new \PhpParser\Node\Stmt\Return_($stmt);
|
|
|
|
$anonymousFunction->params[] = new \PhpParser\Node\Param(new \PhpParser\Node\Expr\Variable('matches'));
|
|
|
|
return $anonymousFunction;
|
|
|
|
}
|
2021-10-23 01:58:01 +00:00
|
|
|
/**
|
|
|
|
* @param ClosureUse[] $uses
|
|
|
|
* @return ClosureUse[]
|
|
|
|
*/
|
|
|
|
private function cleanClosureUses(array $uses) : array
|
|
|
|
{
|
|
|
|
$variableNames = \array_map(function ($use) : string {
|
|
|
|
return (string) $this->nodeNameResolver->getName($use->var);
|
|
|
|
}, $uses, []);
|
|
|
|
$variableNames = \array_unique($variableNames);
|
2021-10-28 12:05:09 +00:00
|
|
|
return \array_map(static function ($variableName) : ClosureUse {
|
2021-10-23 01:58:01 +00:00
|
|
|
return new \PhpParser\Node\Expr\ClosureUse(new \PhpParser\Node\Expr\Variable($variableName));
|
|
|
|
}, $variableNames, []);
|
|
|
|
}
|
|
|
|
private function applyNestedUses(\PhpParser\Node\Expr\Closure $anonymousFunctionNode, \PhpParser\Node\Expr\Variable $useVariable) : \PhpParser\Node\Expr\Closure
|
|
|
|
{
|
2021-10-28 12:03:27 +00:00
|
|
|
$parent = $this->betterNodeFinder->findParentType($useVariable, \PhpParser\Node\Expr\Closure::class);
|
2021-10-28 12:05:09 +00:00
|
|
|
if ($parent instanceof \PhpParser\Node\Expr\Closure) {
|
|
|
|
$paramNames = $this->nodeNameResolver->getNames($parent->params);
|
|
|
|
if ($this->nodeNameResolver->isNames($useVariable, $paramNames)) {
|
|
|
|
return $anonymousFunctionNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$anonymousFunctionNode = clone $anonymousFunctionNode;
|
2021-10-23 01:58:01 +00:00
|
|
|
while ($parent instanceof \PhpParser\Node\Expr\Closure) {
|
|
|
|
$parentOfParent = $this->betterNodeFinder->findParentType($parent, \PhpParser\Node\Expr\Closure::class);
|
|
|
|
$uses = [];
|
|
|
|
while ($parentOfParent instanceof \PhpParser\Node\Expr\Closure) {
|
|
|
|
$uses = $this->collectUsesEqual($parentOfParent, $uses, $useVariable);
|
|
|
|
$parentOfParent = $this->betterNodeFinder->findParentType($parentOfParent, \PhpParser\Node\Expr\Closure::class);
|
|
|
|
}
|
|
|
|
$uses = \array_merge($parent->uses, $uses);
|
|
|
|
$uses = $this->cleanClosureUses($uses);
|
|
|
|
$parent->uses = $uses;
|
|
|
|
$parent = $this->betterNodeFinder->findParentType($parent, \PhpParser\Node\Expr\Closure::class);
|
|
|
|
}
|
|
|
|
return $anonymousFunctionNode;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param ClosureUse[] $uses
|
|
|
|
* @return ClosureUse[]
|
|
|
|
*/
|
|
|
|
private function collectUsesEqual(\PhpParser\Node\Expr\Closure $closure, array $uses, \PhpParser\Node\Expr\Variable $useVariable) : array
|
|
|
|
{
|
|
|
|
foreach ($closure->params as $param) {
|
|
|
|
if ($this->nodeComparator->areNodesEqual($param->var, $useVariable)) {
|
|
|
|
$uses[] = new \PhpParser\Node\Expr\ClosureUse($param->var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $uses;
|
|
|
|
}
|
2021-03-03 09:49:57 +01:00
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
* @param Param[] $paramNodes
|
|
|
|
* @return Variable[]
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private function createUseVariablesFromParams(array $nodes, array $paramNodes) : array
|
2021-03-03 09:49:57 +01:00
|
|
|
{
|
|
|
|
$paramNames = [];
|
|
|
|
foreach ($paramNodes as $paramNode) {
|
|
|
|
$paramNames[] = $this->nodeNameResolver->getName($paramNode);
|
|
|
|
}
|
2021-10-07 08:01:49 +00:00
|
|
|
$variableNodes = $this->resolveVariableNodes($nodes);
|
2021-03-03 09:49:57 +01:00
|
|
|
/** @var Variable[] $filteredVariables */
|
|
|
|
$filteredVariables = [];
|
|
|
|
$alreadyAssignedVariables = [];
|
|
|
|
foreach ($variableNodes as $variableNode) {
|
|
|
|
// "$this" is allowed
|
2021-05-09 20:15:43 +00:00
|
|
|
if ($this->nodeNameResolver->isName($variableNode, 'this')) {
|
2021-03-03 09:49:57 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$variableName = $this->nodeNameResolver->getName($variableNode);
|
|
|
|
if ($variableName === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\in_array($variableName, $paramNames, \true)) {
|
2021-03-03 09:49:57 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
$parentNode = $variableNode->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
|
2021-10-21 19:12:05 +00:00
|
|
|
if ($parentNode instanceof \PhpParser\Node\Expr\Assign || $parentNode instanceof \PhpParser\Node\Stmt\Foreach_ || $parentNode instanceof \PhpParser\Node\Param) {
|
2021-03-03 09:49:57 +01:00
|
|
|
$alreadyAssignedVariables[] = $variableName;
|
|
|
|
}
|
|
|
|
if ($this->nodeNameResolver->isNames($variableNode, $alreadyAssignedVariables)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$filteredVariables[$variableName] = $variableNode;
|
|
|
|
}
|
|
|
|
return $filteredVariables;
|
|
|
|
}
|
2021-10-07 08:01:49 +00:00
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
* @return Variable[]
|
|
|
|
*/
|
|
|
|
private function resolveVariableNodes(array $nodes) : array
|
|
|
|
{
|
|
|
|
return $this->betterNodeFinder->find($nodes, function (\PhpParser\Node $subNode) : bool {
|
|
|
|
if (!$subNode instanceof \PhpParser\Node\Expr\Variable) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
$parentArrowFunction = $this->betterNodeFinder->findParentType($subNode, \PhpParser\Node\Expr\ArrowFunction::class);
|
|
|
|
if ($parentArrowFunction instanceof \PhpParser\Node\Expr\ArrowFunction) {
|
|
|
|
return !(bool) $this->betterNodeFinder->findParentType($parentArrowFunction, \PhpParser\Node\Expr\ArrowFunction::class);
|
|
|
|
}
|
|
|
|
return \true;
|
|
|
|
});
|
|
|
|
}
|
2021-06-10 10:46:24 +00:00
|
|
|
/**
|
|
|
|
* @param ParameterReflection[] $parameterReflections
|
|
|
|
* @return Param[]
|
|
|
|
*/
|
|
|
|
private function createParams(array $parameterReflections) : array
|
|
|
|
{
|
|
|
|
$params = [];
|
|
|
|
foreach ($parameterReflections as $parameterReflection) {
|
|
|
|
$param = new \PhpParser\Node\Param(new \PhpParser\Node\Expr\Variable($parameterReflection->getName()));
|
|
|
|
if (!$parameterReflection->getType() instanceof \PHPStan\Type\MixedType) {
|
2021-10-27 19:25:02 +00:00
|
|
|
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($parameterReflection->getType(), \Rector\PHPStanStaticTypeMapper\Enum\TypeKind::PARAM());
|
2021-06-10 10:46:24 +00:00
|
|
|
}
|
|
|
|
$params[] = $param;
|
|
|
|
}
|
|
|
|
return $params;
|
|
|
|
}
|
2021-07-31 11:43:58 +00:00
|
|
|
/**
|
|
|
|
* @param Param[] $params
|
|
|
|
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|null
|
|
|
|
*/
|
|
|
|
private function createInnerMethodCall(\PHPStan\Reflection\Php\PhpMethodReflection $phpMethodReflection, \PhpParser\Node\Expr $expr, array $params)
|
|
|
|
{
|
|
|
|
if ($phpMethodReflection->isStatic()) {
|
|
|
|
$expr = $this->normalizeClassConstFetchForStatic($expr);
|
|
|
|
if ($expr === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$innerMethodCall = new \PhpParser\Node\Expr\StaticCall($expr, $phpMethodReflection->getName());
|
|
|
|
} else {
|
|
|
|
$expr = $this->resolveExpr($expr);
|
|
|
|
if (!$expr instanceof \PhpParser\Node\Expr) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$innerMethodCall = new \PhpParser\Node\Expr\MethodCall($expr, $phpMethodReflection->getName());
|
|
|
|
}
|
|
|
|
$innerMethodCall->args = $this->nodeFactory->createArgsFromParams($params);
|
|
|
|
return $innerMethodCall;
|
|
|
|
}
|
|
|
|
/**
|
2021-10-30 14:18:31 +00:00
|
|
|
* @return \PhpParser\Node\Expr|\PhpParser\Node\Name\FullyQualified|null
|
2021-07-31 11:43:58 +00:00
|
|
|
*/
|
|
|
|
private function normalizeClassConstFetchForStatic(\PhpParser\Node\Expr $expr)
|
|
|
|
{
|
|
|
|
if (!$expr instanceof \PhpParser\Node\Expr\ClassConstFetch) {
|
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
if (!$this->nodeNameResolver->isName($expr->name, 'class')) {
|
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
// dynamic name, nothing we can do
|
|
|
|
$className = $this->nodeNameResolver->getName($expr->class);
|
|
|
|
if ($className === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new \PhpParser\Node\Name\FullyQualified($className);
|
|
|
|
}
|
|
|
|
/**
|
2021-10-30 14:18:31 +00:00
|
|
|
* @return \PhpParser\Node\Expr|\PhpParser\Node\Expr\New_|null
|
2021-07-31 11:43:58 +00:00
|
|
|
*/
|
|
|
|
private function resolveExpr(\PhpParser\Node\Expr $expr)
|
|
|
|
{
|
|
|
|
if (!$expr instanceof \PhpParser\Node\Expr\ClassConstFetch) {
|
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
if (!$this->nodeNameResolver->isName($expr->name, 'class')) {
|
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
// dynamic name, nothing we can do
|
|
|
|
$className = $this->nodeNameResolver->getName($expr->class);
|
|
|
|
if ($className === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return new \PhpParser\Node\Expr\New_(new \PhpParser\Node\Name\FullyQualified($className));
|
|
|
|
}
|
2021-03-03 09:49:57 +01:00
|
|
|
}
|