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

[CodingStyle] FunctionLikeToFirstClassCallableRector #6667

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

peterfox
Copy link
Contributor

@peterfox peterfox commented Jan 12, 2025

Changes

  • Adds the FunctionLikeToFirstClassCallableRector rule to CodeStyle
  • Adds tests for the new rule

Why

This reduces the user of Closures and Arrow functions where they are basically redundant and code be replaced by a First-Class Callable instead of being wrapped in a closure.

-function ($parameter) { return Call::to($parameter); }
+Call::to(...);

Notes

I debated what group this should be in. It could be considered Code Quality but equally it could be Deadcode or even Code Style. I'm happy to change it based on feedback.

Added to Code Style.

Risk

There might be some risk for this if used with closure binding e.g.

$function = function ($foo) { return $this->foo($foo); };
$function->bindTo($obj);

Might end up as

$function = $this->foo(...);
$function->bindTo($obj);

If that's a problem then I can make the rule avoid method calls of $this.

@peterfox peterfox requested a review from samsonasik January 12, 2025 13:26
@samsonasik samsonasik changed the title [CodeQuality] FuncLikeToFirstClassCallableRector [CodingStyle] FuncLikeToFirstClassCallableRector Jan 19, 2025
@peterfox peterfox changed the title [CodingStyle] FuncLikeToFirstClassCallableRector [CodingStyle] FunctionLikeToFirstClassCallableRector Jan 19, 2025
@peterfox peterfox requested a review from samsonasik January 19, 2025 13:08
@samsonasik
Copy link
Member

rebase latest main is needed to fix unit test

@peterfox peterfox force-pushed the feature/funclike-to-firstclasscallable branch from a98fe29 to 970a27a Compare January 19, 2025 22:02
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

Successfully merging this pull request may close these issues.

2 participants