-
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
Open
zonuexe
wants to merge
8
commits into
phpstan:2.1.x
Choose a base branch
from
zonuexe:replace-memoization
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+548
−235
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7d6bbec
Simplify property memoization for Flyweight pattern by replacing it w…
zonuexe ef19d69
Add MemoizationPropertyRule
zonuexe 2154e33
Simplify property memoization for Flyweight pattern by replacing it w…
zonuexe 3c4bcf2
fixup! Add MemoizationPropertyRule
zonuexe 6d8d1e3
Simplify property memoization for Flyweight pattern by replacing it w…
zonuexe 9d72e31
MemoizationPropertyRule supports static properties
zonuexe 50d4c64
fixup! Add MemoizationPropertyRule
zonuexe a6e89c9
Simplify property memoization for Flyweight pattern by replacing it w…
zonuexe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Build; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\AssignOp\Coalesce; | ||
use PhpParser\Node\Expr\BinaryOp\Identical; | ||
use PhpParser\Node\Expr\ConstFetch; | ||
use PhpParser\Node\Expr\PropertyFetch; | ||
use PhpParser\Node\Expr\StaticPropertyFetch; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\If_; | ||
use PhpParser\Node\Stmt\Return_; | ||
use PhpParser\Node\VarLikeIdentifier; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\File\FileHelper; | ||
use PHPStan\Node\InClassMethodNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function count; | ||
use function dirname; | ||
use function is_string; | ||
use function sprintf; | ||
use function str_starts_with; | ||
use function strcasecmp; | ||
|
||
/** | ||
* @implements Rule<InClassMethodNode> | ||
*/ | ||
final class MemoizationPropertyRule implements Rule | ||
{ | ||
|
||
public function __construct(private FileHelper $fileHelper, private bool $skipTests = true) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassMethodNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$methodNode = $node->getOriginalNode(); | ||
if (count($methodNode->params) !== 0) { | ||
return []; | ||
} | ||
|
||
$stmts = $methodNode->getStmts(); | ||
if ($stmts === null || count($stmts) !== 2) { | ||
return []; | ||
} | ||
|
||
[$ifNode, $returnNode] = $stmts; | ||
if (!$returnNode instanceof Return_ || !$this->isSupportedFetchNode($returnNode->expr)) { | ||
return []; | ||
} | ||
|
||
if (!$ifNode instanceof If_ | ||
|| count($ifNode->stmts) !== 1 | ||
|| !$ifNode->stmts[0] instanceof Expression | ||
|| count($ifNode->elseifs) !== 0 | ||
|| $ifNode->else !== null | ||
|| !$ifNode->cond instanceof Identical | ||
|| !$this->isSupportedFetchNode($ifNode->cond->left) | ||
|| !$ifNode->cond->right instanceof ConstFetch | ||
|| strcasecmp($ifNode->cond->right->name->name, 'null') !== 0 | ||
) { | ||
return []; | ||
} | ||
|
||
$ifThenNode = $ifNode->stmts[0]->expr; | ||
if (!$ifThenNode instanceof Assign || !$this->isSupportedFetchNode($ifThenNode->var)) { | ||
return []; | ||
} | ||
|
||
if ($this->areNodesNotEqual($returnNode->expr, [$ifNode->cond->left, $ifThenNode->var])) { | ||
return []; | ||
} | ||
|
||
if ($this->skipTests && str_starts_with($this->fileHelper->normalizePath($scope->getFile()), $this->fileHelper->normalizePath(dirname(__DIR__, 3) . '/tests'))) { | ||
return []; | ||
} | ||
|
||
$classReflection = $node->getClassReflection(); | ||
$methodReflection = $node->getMethodReflection(); | ||
$methodName = $methodNode->name->name; | ||
$errorBuilder = RuleErrorBuilder::message( | ||
sprintf( | ||
'%s %s::%s() for memoization can be simplified.', | ||
$methodReflection->isStatic() ? 'Static method' : 'Method', | ||
$classReflection->getDisplayName(), | ||
$methodName, | ||
), | ||
)->fixNode($node->getOriginalNode(), static function (Node\Stmt\ClassMethod $method) use ($ifThenNode) { | ||
$method->stmts = [ | ||
new Return_( | ||
new Coalesce($ifThenNode->var, $ifThenNode->expr), | ||
), | ||
]; | ||
|
||
return $method; | ||
})->identifier('phpstan.memoizationProperty'); | ||
|
||
return [ | ||
$errorBuilder->build(), | ||
]; | ||
} | ||
|
||
/** | ||
* @phpstan-assert-if-true PropertyFetch|StaticPropertyFetch $node | ||
*/ | ||
private function isSupportedFetchNode(?Expr $node): bool | ||
{ | ||
return $node instanceof PropertyFetch || $node instanceof StaticPropertyFetch; | ||
} | ||
|
||
/** | ||
* @param list<PropertyFetch|StaticPropertyFetch> $otherNodes | ||
*/ | ||
private function areNodesNotEqual(PropertyFetch|StaticPropertyFetch $node, array $otherNodes): bool | ||
{ | ||
if ($node instanceof PropertyFetch) { | ||
if (!$node->var instanceof Variable | ||
|| !is_string($node->var->name) | ||
|| !$node->name instanceof Identifier | ||
) { | ||
return true; | ||
} | ||
|
||
foreach ($otherNodes as $otherNode) { | ||
if (!$otherNode instanceof PropertyFetch) { | ||
return true; | ||
} | ||
if (!$otherNode->var instanceof Variable | ||
|| !is_string($otherNode->var->name) | ||
|| !$otherNode->name instanceof Identifier | ||
) { | ||
return true; | ||
} | ||
|
||
if ($node->var->name !== $otherNode->var->name | ||
|| $node->name->name !== $otherNode->name->name | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
if (!$node->class instanceof Name || !$node->name instanceof VarLikeIdentifier) { | ||
return true; | ||
} | ||
|
||
foreach ($otherNodes as $otherNode) { | ||
if (!$otherNode instanceof StaticPropertyFetch) { | ||
return true; | ||
} | ||
|
||
if (!$otherNode->class instanceof Name | ||
|| !$otherNode->name instanceof VarLikeIdentifier | ||
) { | ||
return true; | ||
} | ||
|
||
if ($node->class->toLowerString() !== $otherNode->class->toLowerString() | ||
|| $node->name->toString() !== $otherNode->name->toString() | ||
) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.