|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Build; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\Assign; |
| 7 | +use PhpParser\Node\Expr\AssignOp\Coalesce; |
| 8 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 9 | +use PhpParser\Node\Expr\ConstFetch; |
| 10 | +use PhpParser\Node\Expr\PropertyFetch; |
| 11 | +use PhpParser\Node\Identifier; |
| 12 | +use PhpParser\Node\Stmt\Expression; |
| 13 | +use PhpParser\Node\Stmt\If_; |
| 14 | +use PhpParser\Node\Stmt\Return_; |
| 15 | +use PHPStan\Analyser\Scope; |
| 16 | +use PHPStan\File\FileHelper; |
| 17 | +use PHPStan\Node\InClassMethodNode; |
| 18 | +use PHPStan\Rules\Rule; |
| 19 | +use PHPStan\Rules\RuleErrorBuilder; |
| 20 | +use function count; |
| 21 | +use function dirname; |
| 22 | +use function sprintf; |
| 23 | +use function str_starts_with; |
| 24 | +use function strcasecmp; |
| 25 | + |
| 26 | +/** |
| 27 | + * @implements Rule<InClassMethodNode> |
| 28 | + */ |
| 29 | +final class MemoizationPropertyRule implements Rule |
| 30 | +{ |
| 31 | + |
| 32 | + public function __construct(private FileHelper $fileHelper, private bool $skipTests = true) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + public function getNodeType(): string |
| 37 | + { |
| 38 | + return InClassMethodNode::class; |
| 39 | + } |
| 40 | + |
| 41 | + public function processNode(Node $node, Scope $scope): array |
| 42 | + { |
| 43 | + $methodNode = $node->getOriginalNode(); |
| 44 | + if (count($methodNode->params) !== 0) { |
| 45 | + return []; |
| 46 | + } |
| 47 | + |
| 48 | + $stmts = $methodNode->getStmts(); |
| 49 | + if ($stmts === null || count($stmts) !== 2) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + |
| 53 | + [$ifNode, $returnNode] = $stmts; |
| 54 | + |
| 55 | + if (!$returnNode instanceof Return_ || |
| 56 | + !$returnNode->expr instanceof PropertyFetch |
| 57 | + ) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + if (!$ifNode instanceof If_ |
| 62 | + || count($ifNode->stmts) !== 1 |
| 63 | + || !$ifNode->stmts[0] instanceof Expression |
| 64 | + || count($ifNode->elseifs) !== 0 |
| 65 | + || $ifNode->else !== null |
| 66 | + || !$ifNode->cond instanceof Identical |
| 67 | + || !$ifNode->cond->left instanceof PropertyFetch |
| 68 | + || !$ifNode->cond->right instanceof ConstFetch |
| 69 | + || strcasecmp($ifNode->cond->right->name->name, 'null') !== 0 |
| 70 | + ) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + $ifThenNode = $ifNode->stmts[0]->expr; |
| 75 | + |
| 76 | + if (!$ifThenNode instanceof Assign || !$ifThenNode->var instanceof PropertyFetch) { |
| 77 | + return []; |
| 78 | + } |
| 79 | + |
| 80 | + if ($returnNode->expr::class !== $ifNode->cond->left::class |
| 81 | + || $returnNode->expr->var::class !== $ifNode->cond->left->var::class |
| 82 | + || !$returnNode->expr->name instanceof Identifier |
| 83 | + || !$ifNode->cond->left->name instanceof Identifier |
| 84 | + || $returnNode->expr->name->name !== $ifNode->cond->left->name->name |
| 85 | + ) { |
| 86 | + return []; |
| 87 | + } |
| 88 | + |
| 89 | + if ($this->skipTests && str_starts_with($this->fileHelper->normalizePath($scope->getFile()), $this->fileHelper->normalizePath(dirname(__DIR__, 3) . '/tests'))) { |
| 90 | + return []; |
| 91 | + } |
| 92 | + |
| 93 | + $classReflection = $node->getClassReflection(); |
| 94 | + $methodName = $methodNode->name->name; |
| 95 | + $errorBuilder = RuleErrorBuilder::message( |
| 96 | + sprintf('Method %s::%s() for memoization can be simplified.', $classReflection->getDisplayName(), $methodName), |
| 97 | + )->fixNode($node->getOriginalNode(), static function (Node\Stmt\ClassMethod $method) use ($ifThenNode) { |
| 98 | + $method->stmts = [ |
| 99 | + new Return_( |
| 100 | + new Coalesce($ifThenNode->var, $ifThenNode->expr), |
| 101 | + ), |
| 102 | + ]; |
| 103 | + |
| 104 | + return $method; |
| 105 | + })->identifier('phpstan.memoizationProperty'); |
| 106 | + |
| 107 | + return [ |
| 108 | + $errorBuilder->build(), |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments