|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Build; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr; |
| 7 | +use PhpParser\Node\Expr\Assign; |
| 8 | +use PhpParser\Node\Expr\AssignOp\Coalesce; |
| 9 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 10 | +use PhpParser\Node\Expr\ConstFetch; |
| 11 | +use PhpParser\Node\Expr\PropertyFetch; |
| 12 | +use PhpParser\Node\Expr\StaticPropertyFetch; |
| 13 | +use PhpParser\Node\Expr\Variable; |
| 14 | +use PhpParser\Node\Identifier; |
| 15 | +use PhpParser\Node\Name; |
| 16 | +use PhpParser\Node\Stmt\Expression; |
| 17 | +use PhpParser\Node\Stmt\If_; |
| 18 | +use PhpParser\Node\VarLikeIdentifier; |
| 19 | +use PHPStan\Analyser\Scope; |
| 20 | +use PHPStan\File\FileHelper; |
| 21 | +use PHPStan\Node\InClassMethodNode; |
| 22 | +use PHPStan\Rules\Rule; |
| 23 | +use PHPStan\Rules\RuleErrorBuilder; |
| 24 | +use function count; |
| 25 | +use function dirname; |
| 26 | +use function is_string; |
| 27 | +use function str_starts_with; |
| 28 | +use function strcasecmp; |
| 29 | + |
| 30 | +/** |
| 31 | + * @implements Rule<If_> |
| 32 | + */ |
| 33 | +final class MemoizationPropertyRule implements Rule |
| 34 | +{ |
| 35 | + |
| 36 | + public function __construct(private FileHelper $fileHelper, private bool $skipTests = true) |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + public function getNodeType(): string |
| 41 | + { |
| 42 | + return If_::class; |
| 43 | + } |
| 44 | + |
| 45 | + public function processNode(Node $node, Scope $scope): array |
| 46 | + { |
| 47 | + $ifNode = $node; |
| 48 | + |
| 49 | + if (count($ifNode->stmts) !== 1 |
| 50 | + || !$ifNode->stmts[0] instanceof Expression |
| 51 | + || count($ifNode->elseifs) !== 0 |
| 52 | + || $ifNode->else !== null |
| 53 | + || !$ifNode->cond instanceof Identical |
| 54 | + || !$this->isSupportedFetchNode($ifNode->cond->left) |
| 55 | + || !$ifNode->cond->right instanceof ConstFetch |
| 56 | + || strcasecmp($ifNode->cond->right->name->name, 'null') !== 0 |
| 57 | + ) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + $ifThenNode = $ifNode->stmts[0]->expr; |
| 62 | + if (!$ifThenNode instanceof Assign || !$this->isSupportedFetchNode($ifThenNode->var)) { |
| 63 | + return []; |
| 64 | + } |
| 65 | + |
| 66 | + if ($this->areNodesNotEqual($ifNode->cond->left, [$ifThenNode->var])) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + if ($this->skipTests && str_starts_with($this->fileHelper->normalizePath($scope->getFile()), $this->fileHelper->normalizePath(dirname(__DIR__, 3) . '/tests'))) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + $errorBuilder = RuleErrorBuilder::message('This initializing if statement can be replaced with null coalescing assignment operator (??=).') |
| 75 | + ->fixNode($node, static fn (If_ $node) => new Expression(new Coalesce($ifThenNode->var, $ifThenNode->expr))) |
| 76 | + ->identifier('phpstan.memoizationProperty'); |
| 77 | + |
| 78 | + return [ |
| 79 | + $errorBuilder->build(), |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @phpstan-assert-if-true PropertyFetch|StaticPropertyFetch $node |
| 85 | + */ |
| 86 | + private function isSupportedFetchNode(?Expr $node): bool |
| 87 | + { |
| 88 | + return $node instanceof PropertyFetch || $node instanceof StaticPropertyFetch; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param list<PropertyFetch|StaticPropertyFetch> $otherNodes |
| 93 | + */ |
| 94 | + private function areNodesNotEqual(PropertyFetch|StaticPropertyFetch $node, array $otherNodes): bool |
| 95 | + { |
| 96 | + if ($node instanceof PropertyFetch) { |
| 97 | + if (!$node->var instanceof Variable |
| 98 | + || !is_string($node->var->name) |
| 99 | + || !$node->name instanceof Identifier |
| 100 | + ) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + foreach ($otherNodes as $otherNode) { |
| 105 | + if (!$otherNode instanceof PropertyFetch) { |
| 106 | + return true; |
| 107 | + } |
| 108 | + if (!$otherNode->var instanceof Variable |
| 109 | + || !is_string($otherNode->var->name) |
| 110 | + || !$otherNode->name instanceof Identifier |
| 111 | + ) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + if ($node->var->name !== $otherNode->var->name |
| 116 | + || $node->name->name !== $otherNode->name->name |
| 117 | + ) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + if (!$node->class instanceof Name || !$node->name instanceof VarLikeIdentifier) { |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + foreach ($otherNodes as $otherNode) { |
| 130 | + if (!$otherNode instanceof StaticPropertyFetch) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + if (!$otherNode->class instanceof Name |
| 135 | + || !$otherNode->name instanceof VarLikeIdentifier |
| 136 | + ) { |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + if ($node->class->toLowerString() !== $otherNode->class->toLowerString() |
| 141 | + || $node->name->toString() !== $otherNode->name->toString() |
| 142 | + ) { |
| 143 | + return true; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments