-
-
Notifications
You must be signed in to change notification settings - Fork 425
Static closure rule ignores arguments with $this binding #7721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Static closure rule ignores arguments with $this binding #7721
Conversation
src/NodeAnalyzer/CallLikeExpectsThisBindedClosureArgsAnalyzer.php
Outdated
Show resolved
Hide resolved
samsonasik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me 👍
rules-tests/CodingStyle/Rector/Closure/StaticClosureRector/Source/functions.php
Show resolved
Hide resolved
1310b07 to
91e3be0
Compare
src/NodeAnalyzer/CallLikeExpectsThisBindedClosureArgsAnalyzer.php
Outdated
Show resolved
Hide resolved
…e-improved # Conflicts: # src/DependencyInjection/LazyContainerFactory.php # src/NodeTypeResolver/Node/AttributeKey.php # src/PhpParser/NodeVisitor/CallLikeThisBoundClosureArgsNodeVisitor.php
…e-improved # Conflicts: # rules/CodingStyle/Guard/ArrowFunctionAndClosureFirstClassCallableGuard.php
| public function getArgsUsingThisBoundClosure(CallLike $callLike): array | ||
| { | ||
| $args = []; | ||
| $reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this reflection get before it usage
| $parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $callLike, $scope); | ||
| $parameters = $parametersAcceptor->getParameters(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think verify if there is Closure as Arg first is better to avoid possible reflection check:
$args = $callLike->getArgs();
$hasClosureArg = (bool) array_filter($args, fn (Arg $arg): bool => $arg->value instanceof Closure);
if (! $hasClosureArg) {
return null;
}
Changes
CallLikeExpectsThisBindedClosureArgsAnalyzerwhich allows the detection of the@param-closure-this.@param-closure-thisand then skip these closuresWhy
Often there's a few scenarios where the static closure rule causes problems and starts to add the keyword where it will break code because it's being used as a argument for a parameter which will be later bound to an object. This can't be done if the closure is static and causes a runtime error.
This happens quite a lot in Laravel projects. Example issues driftingly/rector-laravel#102
Notes
I had to use
method.notFoundto silence PHPStan. I don't know why this is the case as the code works but something is up with PHPStan.I also created a new analyser to make this work but I'm happy to take this method and put it into a pre-existing one if adding a new one seems pointless.