2021-03-03 09:49:57 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Php72\NodeFactory;
|
2021-03-03 09:49:57 +01:00
|
|
|
|
2022-06-11 12:27:46 +00:00
|
|
|
use RectorPrefix202206\Nette\Utils\Strings;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\ComplexType;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\ClassConstFetch;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\ClosureUse;
|
|
|
|
use PhpParser\Node\Expr\ConstFetch;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\New_;
|
|
|
|
use PhpParser\Node\Expr\StaticCall;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\Node\Name\FullyQualified;
|
|
|
|
use PhpParser\Node\NullableType;
|
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Scalar\LNumber;
|
|
|
|
use PhpParser\Node\Scalar\String_;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use PhpParser\Node\UnionType;
|
|
|
|
use PHPStan\Reflection\FunctionVariantWithPhpDocs;
|
|
|
|
use PHPStan\Reflection\ParameterReflection;
|
|
|
|
use PHPStan\Reflection\ParametersAcceptorSelector;
|
|
|
|
use PHPStan\Reflection\Php\PhpMethodReflection;
|
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use PHPStan\Type\Type;
|
|
|
|
use PHPStan\Type\VoidType;
|
|
|
|
use Rector\Core\Contract\PhpParser\NodePrinterInterface;
|
|
|
|
use Rector\Core\PhpParser\AstResolver;
|
|
|
|
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\Core\PhpParser\Node\NodeFactory;
|
2022-06-13 06:59:41 +00:00
|
|
|
use Rector\Core\PhpParser\Parser\InlineCodeParser;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\Core\PhpParser\Parser\SimplePhpParser;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2022-03-28 22:48:45 +00:00
|
|
|
use ReflectionParameter;
|
2022-06-11 12:27:46 +00:00
|
|
|
use RectorPrefix202206\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
|
|
|
|
use RectorPrefix202206\Symplify\PackageBuilder\Reflection\PrivatesAccessor;
|
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-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2021-03-03 09:49:57 +01:00
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
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
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-06-10 10:46:24 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\NodeFactory
|
|
|
|
*/
|
|
|
|
private $nodeFactory;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-06-10 10:46:24 +00:00
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-08-23 00:20:32 +00:00
|
|
|
* @var \Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser
|
2021-06-10 10:46:24 +00:00
|
|
|
*/
|
|
|
|
private $simpleCallableNodeTraverser;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
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
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-10-23 01:58:01 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
|
|
|
*/
|
|
|
|
private $nodeComparator;
|
2022-03-03 21:57:13 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Core\PhpParser\AstResolver
|
|
|
|
*/
|
|
|
|
private $astResolver;
|
|
|
|
/**
|
|
|
|
* @readonly
|
2022-04-09 22:42:24 +00:00
|
|
|
* @var \Rector\Core\Contract\PhpParser\NodePrinterInterface
|
2022-03-03 21:57:13 +00:00
|
|
|
*/
|
2022-04-09 22:42:24 +00:00
|
|
|
private $nodePrinter;
|
2022-03-28 22:48:45 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Symplify\PackageBuilder\Reflection\PrivatesAccessor
|
|
|
|
*/
|
|
|
|
private $privatesAccessor;
|
2022-06-13 06:59:41 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Core\PhpParser\Parser\InlineCodeParser
|
|
|
|
*/
|
|
|
|
private $inlineCodeParser;
|
|
|
|
public function __construct(NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder, NodeFactory $nodeFactory, StaticTypeMapper $staticTypeMapper, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, SimplePhpParser $simplePhpParser, NodeComparator $nodeComparator, AstResolver $astResolver, NodePrinterInterface $nodePrinter, PrivatesAccessor $privatesAccessor, InlineCodeParser $inlineCodeParser)
|
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;
|
2022-03-03 21:57:13 +00:00
|
|
|
$this->astResolver = $astResolver;
|
2022-04-09 22:42:24 +00:00
|
|
|
$this->nodePrinter = $nodePrinter;
|
2022-03-28 22:48:45 +00:00
|
|
|
$this->privatesAccessor = $privatesAccessor;
|
2022-06-13 06:59:41 +00:00
|
|
|
$this->inlineCodeParser = $inlineCodeParser;
|
2021-03-03 09:49:57 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Param[] $params
|
|
|
|
* @param Stmt[] $stmts
|
2022-04-26 08:13:18 +00:00
|
|
|
* @param \PhpParser\Node\Identifier|\PhpParser\Node\Name|\PhpParser\Node\NullableType|\PhpParser\Node\UnionType|\PhpParser\Node\ComplexType|null $returnTypeNode
|
2021-03-03 09:49:57 +01:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function create(array $params, array $stmts, $returnTypeNode, bool $static = \false) : Closure
|
2021-03-03 09:49:57 +01:00
|
|
|
{
|
|
|
|
$useVariables = $this->createUseVariablesFromParams($stmts, $params);
|
2022-06-07 08:22:29 +00:00
|
|
|
$anonymousFunctionNode = new 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);
|
2022-06-07 08:22:29 +00:00
|
|
|
$anonymousFunctionNode->uses[] = new ClosureUse($useVariable);
|
2021-03-03 09:49:57 +01:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($returnTypeNode instanceof 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;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function createFromPhpMethodReflection(PhpMethodReflection $phpMethodReflection, Expr $expr) : ?Closure
|
2021-06-10 10:46:24 +00:00
|
|
|
{
|
|
|
|
/** @var FunctionVariantWithPhpDocs $functionVariantWithPhpDoc */
|
2022-06-07 08:22:29 +00:00
|
|
|
$functionVariantWithPhpDoc = ParametersAcceptorSelector::selectSingle($phpMethodReflection->getVariants());
|
|
|
|
$anonymousFunction = new Closure();
|
2022-03-03 21:57:13 +00:00
|
|
|
$newParams = $this->createParams($phpMethodReflection, $functionVariantWithPhpDoc->getParameters());
|
2021-06-10 10:46:24 +00:00
|
|
|
$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
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$functionVariantWithPhpDoc->getReturnType() instanceof MixedType) {
|
|
|
|
$returnType = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($functionVariantWithPhpDoc->getReturnType(), TypeKind::RETURN);
|
2021-06-10 10:46:24 +00:00
|
|
|
$anonymousFunction->returnType = $returnType;
|
|
|
|
}
|
|
|
|
// does method return something?
|
2022-06-07 08:22:29 +00:00
|
|
|
$anonymousFunction->stmts[] = $functionVariantWithPhpDoc->getReturnType() instanceof VoidType ? new Expression($innerMethodCall) : new Return_($innerMethodCall);
|
|
|
|
if ($expr instanceof Variable && !$this->nodeNameResolver->isName($expr, 'this')) {
|
|
|
|
$anonymousFunction->uses[] = new ClosureUse($expr);
|
2021-06-10 10:46:24 +00:00
|
|
|
}
|
|
|
|
return $anonymousFunction;
|
|
|
|
}
|
2022-06-13 06:59:41 +00:00
|
|
|
public function createAnonymousFunctionFromExpr(Expr $expr) : ?Closure
|
2021-06-10 10:46:24 +00:00
|
|
|
{
|
2022-06-13 06:59:41 +00:00
|
|
|
$stringValue = $this->inlineCodeParser->stringify($expr);
|
|
|
|
$phpCode = '<?php ' . $stringValue . ';';
|
2021-10-26 12:39:52 +00:00
|
|
|
$contentStmts = $this->simplePhpParser->parseString($phpCode);
|
2022-06-07 08:22:29 +00:00
|
|
|
$anonymousFunction = new Closure();
|
2021-10-26 12:39:52 +00:00
|
|
|
$firstNode = $contentStmts[0] ?? null;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$firstNode instanceof Expression) {
|
2021-06-10 10:46:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$stmt = $firstNode->expr;
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmt, function (Node $node) : Node {
|
|
|
|
if (!$node instanceof String_) {
|
2021-06-10 10:46:24 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$match = Strings::match($node->value, self::DIM_FETCH_REGEX);
|
2021-11-28 17:35:55 +00:00
|
|
|
if ($match === null) {
|
2021-06-10 10:46:24 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$matchesVariable = new Variable('matches');
|
|
|
|
return new ArrayDimFetch($matchesVariable, new LNumber((int) $match['number']));
|
2021-06-10 10:46:24 +00:00
|
|
|
});
|
2022-06-07 08:22:29 +00:00
|
|
|
$anonymousFunction->stmts[] = new Return_($stmt);
|
|
|
|
$anonymousFunction->params[] = new Param(new Variable('matches'));
|
2022-06-13 06:59:41 +00:00
|
|
|
$variables = $expr instanceof Variable ? [] : $this->betterNodeFinder->findInstanceOf($expr, Variable::class);
|
|
|
|
$anonymousFunction->uses = \array_map(function (Variable $variable) : ClosureUse {
|
|
|
|
return new ClosureUse($variable);
|
|
|
|
}, $variables);
|
2021-06-10 10:46:24 +00:00
|
|
|
return $anonymousFunction;
|
|
|
|
}
|
2021-10-23 01:58:01 +00:00
|
|
|
/**
|
|
|
|
* @param ClosureUse[] $uses
|
|
|
|
* @return ClosureUse[]
|
|
|
|
*/
|
|
|
|
private function cleanClosureUses(array $uses) : array
|
|
|
|
{
|
2021-12-17 17:47:54 +00:00
|
|
|
$uniqueUses = [];
|
|
|
|
foreach ($uses as $use) {
|
|
|
|
if (!\is_string($use->var->name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$variableName = $use->var->name;
|
|
|
|
if (\array_key_exists($variableName, $uniqueUses)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$uniqueUses[$variableName] = $use;
|
|
|
|
}
|
|
|
|
return \array_values($uniqueUses);
|
2021-10-23 01:58:01 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function applyNestedUses(Closure $anonymousFunctionNode, Variable $useVariable) : Closure
|
2021-10-23 01:58:01 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$parent = $this->betterNodeFinder->findParentType($useVariable, Closure::class);
|
|
|
|
if (!$parent instanceof Closure) {
|
2022-01-14 09:27:49 +00:00
|
|
|
return $anonymousFunctionNode;
|
|
|
|
}
|
|
|
|
$paramNames = $this->nodeNameResolver->getNames($parent->params);
|
|
|
|
if ($this->nodeNameResolver->isNames($useVariable, $paramNames)) {
|
|
|
|
return $anonymousFunctionNode;
|
2021-10-28 12:05:09 +00:00
|
|
|
}
|
|
|
|
$anonymousFunctionNode = clone $anonymousFunctionNode;
|
2022-06-07 08:22:29 +00:00
|
|
|
while ($parent instanceof Closure) {
|
|
|
|
$parentOfParent = $this->betterNodeFinder->findParentType($parent, Closure::class);
|
2021-10-23 01:58:01 +00:00
|
|
|
$uses = [];
|
2022-06-07 08:22:29 +00:00
|
|
|
while ($parentOfParent instanceof Closure) {
|
2021-10-23 01:58:01 +00:00
|
|
|
$uses = $this->collectUsesEqual($parentOfParent, $uses, $useVariable);
|
2022-06-07 08:22:29 +00:00
|
|
|
$parentOfParent = $this->betterNodeFinder->findParentType($parentOfParent, Closure::class);
|
2021-10-23 01:58:01 +00:00
|
|
|
}
|
|
|
|
$uses = \array_merge($parent->uses, $uses);
|
|
|
|
$uses = $this->cleanClosureUses($uses);
|
|
|
|
$parent->uses = $uses;
|
2022-06-07 08:22:29 +00:00
|
|
|
$parent = $this->betterNodeFinder->findParentType($parent, Closure::class);
|
2021-10-23 01:58:01 +00:00
|
|
|
}
|
|
|
|
return $anonymousFunctionNode;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param ClosureUse[] $uses
|
|
|
|
* @return ClosureUse[]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function collectUsesEqual(Closure $closure, array $uses, Variable $useVariable) : array
|
2021-10-23 01:58:01 +00:00
|
|
|
{
|
|
|
|
foreach ($closure->params as $param) {
|
|
|
|
if ($this->nodeComparator->areNodesEqual($param->var, $useVariable)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$uses[] = new ClosureUse($param->var);
|
2021-10-23 01:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $uses;
|
|
|
|
}
|
2021-03-03 09:49:57 +01:00
|
|
|
/**
|
|
|
|
* @param Param[] $paramNodes
|
2022-03-03 21:57:13 +00:00
|
|
|
* @return string[]
|
2021-03-03 09:49:57 +01:00
|
|
|
*/
|
2022-03-03 21:57:13 +00:00
|
|
|
private function collectParamNames(array $paramNodes) : array
|
2021-03-03 09:49:57 +01:00
|
|
|
{
|
|
|
|
$paramNames = [];
|
|
|
|
foreach ($paramNodes as $paramNode) {
|
|
|
|
$paramNames[] = $this->nodeNameResolver->getName($paramNode);
|
|
|
|
}
|
2022-03-03 21:57:13 +00:00
|
|
|
return $paramNames;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Node[] $nodes
|
|
|
|
* @param Param[] $paramNodes
|
2022-03-07 11:51:39 +00:00
|
|
|
* @return array<string, Variable>
|
2022-03-03 21:57:13 +00:00
|
|
|
*/
|
|
|
|
private function createUseVariablesFromParams(array $nodes, array $paramNodes) : array
|
|
|
|
{
|
|
|
|
$paramNames = $this->collectParamNames($paramNodes);
|
2022-03-07 11:51:39 +00:00
|
|
|
/** @var Variable[] $variables */
|
2022-06-07 08:22:29 +00:00
|
|
|
$variables = $this->betterNodeFinder->findInstanceOf($nodes, Variable::class);
|
2022-03-07 11:51:39 +00:00
|
|
|
/** @var array<string, Variable> $filteredVariables */
|
2021-03-03 09:49:57 +01:00
|
|
|
$filteredVariables = [];
|
|
|
|
$alreadyAssignedVariables = [];
|
2022-03-07 11:51:39 +00:00
|
|
|
foreach ($variables as $variable) {
|
2021-03-03 09:49:57 +01:00
|
|
|
// "$this" is allowed
|
2022-03-07 11:51:39 +00:00
|
|
|
if ($this->nodeNameResolver->isName($variable, 'this')) {
|
2021-03-03 09:49:57 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-07 11:51:39 +00:00
|
|
|
$variableName = $this->nodeNameResolver->getName($variable);
|
2021-03-03 09:49:57 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE);
|
|
|
|
if ($parentNode instanceof Node && \in_array(\get_class($parentNode), [Assign::class, Foreach_::class, Param::class], \true)) {
|
2021-03-03 09:49:57 +01:00
|
|
|
$alreadyAssignedVariables[] = $variableName;
|
|
|
|
}
|
2022-03-07 11:51:39 +00:00
|
|
|
if (!$this->nodeNameResolver->isNames($variable, $alreadyAssignedVariables)) {
|
|
|
|
$filteredVariables[$variableName] = $variable;
|
2021-03-03 09:49:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $filteredVariables;
|
|
|
|
}
|
2021-06-10 10:46:24 +00:00
|
|
|
/**
|
|
|
|
* @param ParameterReflection[] $parameterReflections
|
|
|
|
* @return Param[]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createParams(PhpMethodReflection $phpMethodReflection, array $parameterReflections) : array
|
2021-06-10 10:46:24 +00:00
|
|
|
{
|
2022-03-03 21:57:13 +00:00
|
|
|
$classReflection = $phpMethodReflection->getDeclaringClass();
|
|
|
|
$className = $classReflection->getName();
|
|
|
|
$methodName = $phpMethodReflection->getName();
|
|
|
|
/** @var ClassMethod $classMethod */
|
|
|
|
$classMethod = $this->astResolver->resolveClassMethod($className, $methodName);
|
2021-06-10 10:46:24 +00:00
|
|
|
$params = [];
|
2022-03-03 21:57:13 +00:00
|
|
|
foreach ($parameterReflections as $key => $parameterReflection) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$param = new Param(new Variable($parameterReflection->getName()));
|
2022-03-03 21:57:13 +00:00
|
|
|
$this->applyParamType($param, $parameterReflection);
|
|
|
|
$this->applyParamDefaultValue($param, $parameterReflection, $key, $classMethod);
|
2022-03-28 22:48:45 +00:00
|
|
|
$this->applyParamByReference($param, $parameterReflection);
|
2021-06-10 10:46:24 +00:00
|
|
|
$params[] = $param;
|
|
|
|
}
|
|
|
|
return $params;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function applyParamType(Param $param, ParameterReflection $parameterReflection) : void
|
2022-03-03 21:57:13 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($parameterReflection->getType() instanceof MixedType) {
|
2022-03-03 21:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($parameterReflection->getType(), TypeKind::PARAM);
|
2022-03-03 21:57:13 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function applyParamByReference(Param $param, ParameterReflection $parameterReflection) : void
|
2022-03-28 22:48:45 +00:00
|
|
|
{
|
|
|
|
/** @var ReflectionParameter $reflection */
|
|
|
|
$reflection = $this->privatesAccessor->getPrivateProperty($parameterReflection, 'reflection');
|
|
|
|
$param->byRef = $reflection->isPassedByReference();
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function applyParamDefaultValue(Param $param, ParameterReflection $parameterReflection, int $key, ClassMethod $classMethod) : void
|
2022-03-03 21:57:13 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$parameterReflection->getDefaultValue() instanceof Type) {
|
2022-03-03 21:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-09 22:42:24 +00:00
|
|
|
$printDefaultValue = $this->nodePrinter->print($classMethod->params[$key]->default);
|
2022-06-07 08:22:29 +00:00
|
|
|
$param->default = new ConstFetch(new Name($printDefaultValue));
|
2022-03-03 21:57:13 +00:00
|
|
|
}
|
2021-07-31 11:43:58 +00:00
|
|
|
/**
|
|
|
|
* @param Param[] $params
|
|
|
|
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|null
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createInnerMethodCall(PhpMethodReflection $phpMethodReflection, Expr $expr, array $params)
|
2021-07-31 11:43:58 +00:00
|
|
|
{
|
|
|
|
if ($phpMethodReflection->isStatic()) {
|
|
|
|
$expr = $this->normalizeClassConstFetchForStatic($expr);
|
|
|
|
if ($expr === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$innerMethodCall = new StaticCall($expr, $phpMethodReflection->getName());
|
2021-07-31 11:43:58 +00:00
|
|
|
} else {
|
|
|
|
$expr = $this->resolveExpr($expr);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof Expr) {
|
2021-07-31 11:43:58 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$innerMethodCall = new MethodCall($expr, $phpMethodReflection->getName());
|
2021-07-31 11:43:58 +00:00
|
|
|
}
|
|
|
|
$innerMethodCall->args = $this->nodeFactory->createArgsFromParams($params);
|
|
|
|
return $innerMethodCall;
|
|
|
|
}
|
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @return null|\PhpParser\Node\Name|\PhpParser\Node\Name\FullyQualified|\PhpParser\Node\Expr
|
2021-07-31 11:43:58 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function normalizeClassConstFetchForStatic(Expr $expr)
|
2021-07-31 11:43:58 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof ClassConstFetch) {
|
2021-07-31 11:43:58 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$name = new Name($className);
|
|
|
|
return $name->isSpecialClassName() ? $name : new FullyQualified($className);
|
2021-07-31 11:43:58 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-04-26 08:13:18 +00:00
|
|
|
* @return \PhpParser\Node\Expr\New_|\PhpParser\Node\Expr|null
|
2021-07-31 11:43:58 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function resolveExpr(Expr $expr)
|
2021-07-31 11:43:58 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expr instanceof ClassConstFetch) {
|
2021-07-31 11:43:58 +00:00
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
if (!$this->nodeNameResolver->isName($expr->name, 'class')) {
|
|
|
|
return $expr;
|
|
|
|
}
|
|
|
|
// dynamic name, nothing we can do
|
|
|
|
$className = $this->nodeNameResolver->getName($expr->class);
|
2022-06-07 08:22:29 +00:00
|
|
|
return $className === null ? null : new New_(new FullyQualified($className));
|
2021-07-31 11:43:58 +00:00
|
|
|
}
|
2021-03-03 09:49:57 +01:00
|
|
|
}
|