rector-src icon indicating copy to clipboard operation
rector-src copied to clipboard

Static closure rule ignores arguments with $this binding

Open peterfox opened this issue 2 months ago • 1 comments

Changes

  • Adds a new CallLikeExpectsThisBindedClosureArgsAnalyzer which allows the detection of the @param-closure-this.
  • Adds a mechanism in the Static Closure rule to apply an attribute to closures that might conflict with @param-closure-this and then skip these closures
  • Sets up the tests to cover these scenarios

Why

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 https://github.com/driftingly/rector-laravel/issues/102

Notes

I had to use method.notFound to 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.

peterfox avatar Dec 05 '25 20:12 peterfox

@peterfox unit test somehow error

samsonasik avatar Dec 24 '25 02:12 samsonasik

Lets give this a go. Thank you Peter :+1:

TomasVotruba avatar Dec 25 '25 22:12 TomasVotruba