nodeNameResolver = $nodeNameResolver; $this->betterNodeFinder = $betterNodeFinder; } /** * @param Param[] $params * @param Stmt[] $stmts * @param Identifier|Name|NullableType|UnionType|null $returnTypeNode */ public function create(array $params, array $stmts, ?\PhpParser\Node $returnTypeNode) : \PhpParser\Node\Expr\Closure { $useVariables = $this->createUseVariablesFromParams($stmts, $params); $anonymousFunctionNode = new \PhpParser\Node\Expr\Closure(); $anonymousFunctionNode->params = $params; foreach ($useVariables as $useVariable) { $anonymousFunctionNode->uses[] = new \PhpParser\Node\Expr\ClosureUse($useVariable); } if ($returnTypeNode instanceof \PhpParser\Node) { $anonymousFunctionNode->returnType = $returnTypeNode; } $anonymousFunctionNode->stmts = $stmts; return $anonymousFunctionNode; } /** * @param Node[] $nodes * @param Param[] $paramNodes * @return Variable[] */ private function createUseVariablesFromParams(array $nodes, array $paramNodes) : array { $paramNames = []; foreach ($paramNodes as $paramNode) { $paramNames[] = $this->nodeNameResolver->getName($paramNode); } $variableNodes = $this->betterNodeFinder->findInstanceOf($nodes, \PhpParser\Node\Expr\Variable::class); /** @var Variable[] $filteredVariables */ $filteredVariables = []; $alreadyAssignedVariables = []; foreach ($variableNodes as $variableNode) { // "$this" is allowed if ($this->nodeNameResolver->isName($variableNode, 'this')) { continue; } $variableName = $this->nodeNameResolver->getName($variableNode); if ($variableName === null) { continue; } if (\in_array($variableName, $paramNames, \true)) { continue; } $parentNode = $variableNode->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE); if ($parentNode instanceof \PhpParser\Node\Expr\Assign) { $alreadyAssignedVariables[] = $variableName; } if ($this->nodeNameResolver->isNames($variableNode, $alreadyAssignedVariables)) { continue; } $filteredVariables[$variableName] = $variableNode; } return $filteredVariables; } }