Skip to content

[CodingStyle] FunctionLikeToFirstClassCallableRector#6667

Merged
TomasVotruba merged 17 commits intorectorphp:mainfrom
peterfox:feature/funclike-to-firstclasscallable
Mar 3, 2025
Merged

[CodingStyle] FunctionLikeToFirstClassCallableRector#6667
TomasVotruba merged 17 commits intorectorphp:mainfrom
peterfox:feature/funclike-to-firstclasscallable

Conversation

@peterfox
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Dec 7, 2025

This pull request has been automatically locked because it has been closed for 150 days. Please open a new PR if you want to continue the work.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants