Skip to content

Simplify property memoization for Flyweight pattern by replacing it with ??= #4084

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

Open
wants to merge 4 commits into
base: 2.1.x
Choose a base branch
from

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented Jul 6, 2025

Now that PHP 7.2 has been dropped, this pattern can be replaced.

@ondrejmirtes
Copy link
Member

Would be great to have an auto-fixable rule that would enforce this under https://github.com/phpstan/phpstan-src/tree/2.1.x/build/PHPStan/Build. Do you think it'd be easy for you to implement that? And verify it by actually using it to make these changes. And compare it to your manual changes 😊

(Yeah, RuleErrorBuilder has fixNode method on 2.1.x, a few rules already use it.)

@zonuexe
Copy link
Contributor Author

zonuexe commented Jul 6, 2025

I hadn't noticed that they added RuleErrorBuilder::fixNode() yet, but this is very interesting! It probably looks very easy, so I'll give it a try.

@ondrejmirtes
Copy link
Member

Yeah, just detect if === null and assignment to the same thing in the only statement.

@zonuexe zonuexe force-pushed the replace-memoization branch 3 times, most recently from c3a52ed to d51316c Compare July 6, 2025 20:45
@zonuexe zonuexe force-pushed the replace-memoization branch from d51316c to 2154e33 Compare July 6, 2025 20:55
@zonuexe zonuexe marked this pull request as draft July 6, 2025 21:08
@zonuexe zonuexe marked this pull request as ready for review July 6, 2025 21:08
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@zonuexe zonuexe marked this pull request as draft July 6, 2025 21:13
@zonuexe zonuexe marked this pull request as ready for review July 6, 2025 21:22
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@zonuexe zonuexe force-pushed the replace-memoization branch from 29b8dc6 to 23939e1 Compare July 6, 2025 21:28
@zonuexe zonuexe force-pushed the replace-memoization branch from 23939e1 to 3c4bcf2 Compare July 6, 2025 21:35
@zonuexe
Copy link
Contributor Author

zonuexe commented Jul 6, 2025

@ondrejmirtes
I think this PR now works perfectly, applying the rules replaced all the things I had missed.

The refactoring feature integrated into PHPStan is a game changer. 🎉

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