Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,7 @@ public function refactor(Node $node): ?Node
return null;
}

$anonymousFunction = new Closure();
$anonymousFunction->params = $classMethod->params;

$innerMethodCall = new MethodCall($objectVariable, $classMethod->name);
$innerMethodCall->args = $this->convertParamsToArgs($classMethod->params);

if ($classMethod->returnType !== null) {
$anonymousFunction->returnType = $classMethod->returnType;
}

$anonymousFunction->stmts[] = new Return_($innerMethodCall);

if ($objectVariable instanceof Variable) {
if (! $this->isName($objectVariable, 'this')) {
$anonymousFunction->uses[] = new ClosureUse($objectVariable);
}
}

return $anonymousFunction;
return $this->createAnonymousFunction($classMethod, $objectVariable);
}

/**
Expand Down Expand Up @@ -191,4 +173,37 @@ private function shouldSkipArray(Array_ $array): bool
// can be totally empty in case of "[, $value]"
return $array->items[0] === null;
}

/**
* @param Variable|PropertyFetch $node
*/
private function createAnonymousFunction(ClassMethod $classMethod, Node $node): Closure
{
$hasClassMethodReturn = $this->betterNodeFinder->findInstanceOf((array) $classMethod->stmts, Return_::class);

$anonymousFunction = new Closure();
$anonymousFunction->params = $classMethod->params;

$innerMethodCall = new MethodCall($node, $classMethod->name);
$innerMethodCall->args = $this->convertParamsToArgs($classMethod->params);

if ($classMethod->returnType !== null) {
$anonymousFunction->returnType = $classMethod->returnType;
}

// does method return something?
if ($hasClassMethodReturn) {
$anonymousFunction->stmts[] = new Return_($innerMethodCall);
} else {
$anonymousFunction->stmts[] = new Node\Stmt\Expression($innerMethodCall);
}

if ($node instanceof Variable) {
if (! $this->isName($node, 'this')) {
$anonymousFunction->uses[] = new ClosureUse($node);
}
}

return $anonymousFunction;
}
}