Skip to content

Declare closures static whenever possible#1651

Closed
talkinnl wants to merge 11 commits intopredis:mainfrom
talkinnl:static-closures
Closed

Declare closures static whenever possible#1651
talkinnl wants to merge 11 commits intopredis:mainfrom
talkinnl:static-closures

Conversation

@talkinnl
Copy link
Copy Markdown

@talkinnl talkinnl commented Mar 5, 2026

As per the request of @tillkruss in #1642 (comment) :

@talkinnl Wanna open a PR to add them back in?

Documentation reference of static closures was already nicely provided by @predakanga in that same PR:

This PR seems to be based on a misunderstanding - the code changes are all examples of static anonymous functions, not the static callables which have been deprecated.

For reference, here is the deprecation notice, and here is the documentation of static anonymous functions - note that they're not deprecated as of the current version.


Also: in the next PHP major release, static closures seem to get an extra optimization: https://wiki.php.net/rfc/closure-optimizations (section 2 'Stateless closure caching').


So this PR restores the static closures.
In fact, I've just ran Rector with only the StaticClosureRector rule, which not only restores the previous static closures but finds way more.

Review hint:
You might want to look at individual commits.
I have touched 0 .php files by hand, 100% of the changes are just the StaticClosureRector and the composer style:fix afterwards.

For convenience, I've added a composer test target which does the safe set of checks: phpstan + phpunit without connected + style dry-run.

PR was carefully made with Rector, and without any AI.


Want to reproduce the Rector change which isn't pushed as part of this PR?

  1. composer require rector/rector --dev
  2. Run once to autogenerate config
  3. Change the rector.php:
->withRules([StaticClosureRector::class]) 
->withSkip([AddClosureVoidReturnTypeWhereNoReturnRector::class]) // Disable default Rule unrelated to goal of PR, so would introduce noise

Maarten Scholder added 5 commits March 5, 2026 12:08
… possible.

- ONLY ran Rector with ONLY StaticClosureRector. NO other changes made, not even the style fixes afterwards.
- StaticArrowFunctionRector did not change anything, no static arrow functions present in all the code.
- Did not add Rector dependency nor config to this PR.
@talkinnl talkinnl requested a review from a team as a code owner March 5, 2026 12:00
Comment thread composer.json
"@phpstan",
"@phpunit:exclude-connected",
"@style"
]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanna add the static_lambda to our php-cs-fixer config as well?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Running it introduces no extra changes.

TIL the php-cs-fixer can do it too, immediately going with Rector is a habit I guess. So now it's enforced thanks to the CI fixer checks. :)

Maarten Scholder and others added 4 commits March 6, 2026 09:13
- static_lambda needs to allow risky. The rule might be theoretically risky when you're passing the closures everywhere and use bind/bindTo(), but predis code has 0 bind/bindTo() calls even.
@talkinnl
Copy link
Copy Markdown
Author

talkinnl commented Mar 6, 2026

Feedback processed + synced my fork.

As suggested, this style is now enforced by the existing php-cs-fixer setup.

Comment thread .php-cs-fixer.dist.php
'increment_style' => false,
'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['array_destructuring', 'arrays']]
'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['array_destructuring', 'arrays']],
'static_lambda' => true,
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal preference would be to a-z order all these array keys, but added as last line to follow the existing convention. ;)

@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 92.9% (+0.002%) from 92.898%
when pulling a149c44 on talkinnl:static-closures
into 7388d91 on predis:main.

@talkinnl
Copy link
Copy Markdown
Author

talkinnl commented Mar 9, 2026

@tillkruss feedback has been processed. :)

Comment on lines -43 to -47

if (is_nan($value)) {
return 'nan';
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is all this whitespace being removed?

@tillkruss tillkruss mentioned this pull request Mar 9, 2026
@tillkruss
Copy link
Copy Markdown
Member

See #1652.

@tillkruss tillkruss closed this Mar 9, 2026
@talkinnl
Copy link
Copy Markdown
Author

Based on the whitespace comment , I already wanted to make a new PR using cs fixer for you. But nice to see you already did it. 👍

(Rector has a disclaimer to run a style tool afterwards, but in this case it'd be nicer if it didn't touch unrelated lines.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants