1515use PhpParser \Node \Name ;
1616use PhpParser \Node \Stmt \Expression ;
1717use PhpParser \Node \Stmt \If_ ;
18- use PhpParser \Node \Stmt \Return_ ;
1918use PhpParser \Node \VarLikeIdentifier ;
2019use PHPStan \Analyser \Scope ;
2120use PHPStan \File \FileHelper ;
2524use function count ;
2625use function dirname ;
2726use function is_string ;
28- use function sprintf ;
2927use function str_starts_with ;
3028use function strcasecmp ;
3129
3230/**
33- * @implements Rule<InClassMethodNode >
31+ * @implements Rule<If_ >
3432 */
3533final class MemoizationPropertyRule implements Rule
3634{
@@ -41,28 +39,14 @@ public function __construct(private FileHelper $fileHelper, private bool $skipTe
4139
4240 public function getNodeType (): string
4341 {
44- return InClassMethodNode ::class;
42+ return If_ ::class;
4543 }
4644
4745 public function processNode (Node $ node , Scope $ scope ): array
4846 {
49- $ methodNode = $ node ->getOriginalNode ();
50- if (count ($ methodNode ->params ) !== 0 ) {
51- return [];
52- }
53-
54- $ stmts = $ methodNode ->getStmts ();
55- if ($ stmts === null || count ($ stmts ) !== 2 ) {
56- return [];
57- }
47+ $ ifNode = $ node ;
5848
59- [$ ifNode , $ returnNode ] = $ stmts ;
60- if (!$ returnNode instanceof Return_ || !$ this ->isSupportedFetchNode ($ returnNode ->expr )) {
61- return [];
62- }
63-
64- if (!$ ifNode instanceof If_
65- || count ($ ifNode ->stmts ) !== 1
49+ if (count ($ ifNode ->stmts ) !== 1
6650 || !$ ifNode ->stmts [0 ] instanceof Expression
6751 || count ($ ifNode ->elseifs ) !== 0
6852 || $ ifNode ->else !== null
@@ -79,33 +63,17 @@ public function processNode(Node $node, Scope $scope): array
7963 return [];
8064 }
8165
82- if ($ this ->areNodesNotEqual ( $ returnNode -> expr , [ $ ifNode -> cond -> left , $ ifThenNode -> var ] )) {
66+ if ($ this ->skipTests && str_starts_with ( $ this -> fileHelper -> normalizePath ( $ scope -> getFile ()) , $ this -> fileHelper -> normalizePath ( dirname ( __DIR__ , 3 ) . ' /tests ' ) )) {
8367 return [];
8468 }
8569
86- if ($ this ->skipTests && str_starts_with ( $ this -> fileHelper -> normalizePath ( $ scope -> getFile ()), $ this -> fileHelper -> normalizePath ( dirname ( __DIR__ , 3 ) . ' /tests ' ) )) {
70+ if ($ this ->areNodesNotEqual ( $ ifNode -> cond -> left , [ $ ifThenNode -> var ] )) {
8771 return [];
8872 }
8973
90- $ classReflection = $ node ->getClassReflection ();
91- $ methodReflection = $ node ->getMethodReflection ();
92- $ methodName = $ methodNode ->name ->name ;
93- $ errorBuilder = RuleErrorBuilder::message (
94- sprintf (
95- '%s %s::%s() for memoization can be simplified. ' ,
96- $ methodReflection ->isStatic () ? 'Static method ' : 'Method ' ,
97- $ classReflection ->getDisplayName (),
98- $ methodName ,
99- ),
100- )->fixNode ($ node ->getOriginalNode (), static function (Node \Stmt \ClassMethod $ method ) use ($ ifThenNode ) {
101- $ method ->stmts = [
102- new Return_ (
103- new Coalesce ($ ifThenNode ->var , $ ifThenNode ->expr ),
104- ),
105- ];
106-
107- return $ method ;
108- })->identifier ('phpstan.memoizationProperty ' );
74+ $ errorBuilder = RuleErrorBuilder::message ('Memoization property can be simplified. ' )
75+ ->fixNode ($ node , static fn (If_ $ node ) => new Expression (new Coalesce ($ ifThenNode ->var , $ ifThenNode ->expr )))
76+ ->identifier ('phpstan.memoizationProperty ' );
10977
11078 return [
11179 $ errorBuilder ->build (),
0 commit comments