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 8 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. 🎉


[$ifNode, $returnNode] = $stmts;
if (!$returnNode instanceof Return_
|| !$returnNode->expr instanceof PropertyFetch
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can do the same for StaticPropertyFetch

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, I implemented it.

@zonuexe zonuexe force-pushed the replace-memoization branch from 468fab5 to 9d72e31 Compare July 7, 2025 16:11
Copy link
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

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

I described the logic as following:

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

So even if there's if in the middle of a function body, this rule could detect and change it.

It doesn't matter if there's $this->foo = $foo; or return $this->foo = $foo;. The rule should be able to handle both.


public function getNodeType(): string
{
return InClassMethodNode::class;
Copy link
Member

Choose a reason for hiding this comment

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

I think the rule should apply to If_, not to a method body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, it has been reflected in the rule.

@zonuexe zonuexe force-pushed the replace-memoization branch from 7e86217 to a6e89c9 Compare July 8, 2025 01:46
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.

4 participants