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

Merged

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
…tClassCallableRector/FunctionLikeToFirstClassCallableRectorTest.php
…keToFirstClassCallableRector/FunctionLikeToFirstClassCallableRectorTest.php"

This reverts commit 52a9a6a.
@peterfox peterfox requested a review from samsonasik February 12, 2025 17:56
@TomasVotruba
Copy link
Member

My appologies, I've missed the approval here.

Let's ship it 🥳 thank you Peter

@TomasVotruba TomasVotruba merged commit cca89d9 into rectorphp:main Mar 3, 2025
44 checks passed
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.

3 participants