-
Notifications
You must be signed in to change notification settings - Fork 515
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
base: 2.1.x
Are you sure you want to change the base?
Conversation
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 |
I hadn't noticed that they added |
Yeah, just detect if === null and assignment to the same thing in the only statement. |
c3a52ed
to
d51316c
Compare
d51316c
to
2154e33
Compare
This pull request has been marked as ready for review. |
This pull request has been marked as ready for review. |
29b8dc6
to
23939e1
Compare
23939e1
to
3c4bcf2
Compare
@ondrejmirtes The refactoring feature integrated into PHPStan is a game changer. 🎉 |
|
||
[$ifNode, $returnNode] = $stmts; | ||
if (!$returnNode instanceof Return_ | ||
|| !$returnNode->expr instanceof PropertyFetch |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
468fab5
to
9d72e31
Compare
There was a problem hiding this 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
7e86217
to
a6e89c9
Compare
Now that PHP 7.2 has been dropped, this pattern can be replaced.