Skip to content
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

Prohibit Rector from making Closures static #102

Open
HenkPoley opened this issue Apr 28, 2023 · 4 comments
Open

Prohibit Rector from making Closures static #102

HenkPoley opened this issue Apr 28, 2023 · 4 comments

Comments

@HenkPoley
Copy link

HenkPoley commented Apr 28, 2023

Since Laravel tends to depend on lots of behind the scene magic, using static on calls to functions and closures tends not give what you want. Rector should not introduce these things.

I propose adding these somewhere sensible (maybe to all the LARAVEL_number and LARAVEL_CODE_QUALITY):

    $rectorConfig->skip([
        \Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector::class,
        \Rector\CodingStyle\Rector\Closure\StaticClosureRector::class,
        \Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector::class,
        \Rector\Php70\Rector\MethodCall\ThisCallOnStaticMethodToStaticCallRector::class,
    ]]);
@HenkPoley
Copy link
Author

HenkPoley commented May 11, 2023

Oof, I don't think I have seen it not give problems.

So.. I suppose it is always when you do something with Laravel Eloquent Models inside a closure?

I think I've read Rector (?similar tool?) removing most of the automatic to static conversions in a recent or upcoming update.

@GeniJaho
Copy link
Collaborator

@driftingly @peterfox The rules mentioned above are part of sets in the core Rector repo, the Php 70, and the Coding Style sets. I found this old issue which probably has the same problem rectorphp/rector#8087.

Their solution is to skip the rule for a file or the entire project.

Digging deeper, there are these two rules (StaticArrowFunctionRector and StaticClosureRector) that prefix the arrow functions and closures with static whenever possible. That is, if a closure does not need the $this reference, it can be made static. They occur quite frequently, at least in Laravel codebases. However, the RFC notes that we probably don't need them and get little benefit.

Static closures are rarely used: They're mainly used to prevent $this cycles, which make GC behavior less predictable. Most code need not concern itself with this.

For that reason, I suggest we skip these two rules in the Readme of the project and/or add a section with suggestions on rules to avoid in general. What do you think?

@peterfox
Copy link
Collaborator

Funnily enough, this is something I've already looked into a little recently and PHPStan will soon be able to detect when this is happening so actually, we'd just need to check for the param.

phpstan/phpstan#9302 (reply in thread)

I agree though that there shouldn't be any rules we write in this repo that make the closures static. That's for other rules to do the transformation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants