Skip to content
Closed
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 @@ -4,6 +4,7 @@

namespace Rector\PhpParser\NodeVisitor;

use PhpParser\Node\Arg;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\CallLike;
Expand Down Expand Up @@ -37,32 +38,38 @@ public function enterNode(Node $node): ?Node
return null;
}

if ($node->getArgs() === []) {
$args = $node->getArgs();
if ($args === []) {
return null;
}

$methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
$filteredArgs = array_filter(
$args,
fn (Arg $arg): bool => $arg->value instanceof Closure || $arg->value instanceof ArrowFunction
);

foreach ($node->getArgs() as $arg) {
if (! $arg->value instanceof Closure && ! $arg->value instanceof ArrowFunction) {
continue;
}
if ($filteredArgs === []) {
return null;
}

$methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optimization should happen inside this method.

Otherwise the performance is reduced when it's called from different place.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this PR is to avoid reflection lookup when possible on every call like, since the attribute flag filling is only when argument is closure/arrow function.

The method is general method that used in various places and imo already optimized.


foreach ($filteredArgs as $filteredArg) {
if ($methodReflection instanceof NativeFunctionReflection) {
$parametersAcceptors = ParametersAcceptorSelector::combineAcceptors(
$methodReflection->getVariants()
);

foreach ($parametersAcceptors->getParameters() as $extendedParameterReflection) {
if ($extendedParameterReflection->getType() instanceof CallableType && $extendedParameterReflection->getType() ->isVariadic()) {
$arg->value->setAttribute(AttributeKey::HAS_CLOSURE_WITH_VARIADIC_ARGS, true);
$filteredArg->value->setAttribute(AttributeKey::HAS_CLOSURE_WITH_VARIADIC_ARGS, true);
}
}

return null;
}

$arg->value->setAttribute(AttributeKey::HAS_CLOSURE_WITH_VARIADIC_ARGS, true);
$filteredArg->value->setAttribute(AttributeKey::HAS_CLOSURE_WITH_VARIADIC_ARGS, true);
}

return null;
Expand Down