|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\Utils\PHPStan\Rule; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr; |
| 9 | +use PhpParser\Node\Expr\Assign; |
| 10 | +use PhpParser\Node\Expr\StaticCall; |
| 11 | +use PhpParser\Node\Expr\Variable; |
| 12 | +use PhpParser\Node\Stmt; |
| 13 | +use PhpParser\Node\Stmt\ClassMethod; |
| 14 | +use PhpParser\Node\Stmt\Expression; |
| 15 | +use PhpParser\Node\Stmt\If_; |
| 16 | +use PhpParser\Node\Stmt\Return_; |
| 17 | +use PhpParser\NodeFinder; |
| 18 | +use PHPStan\Analyser\Scope; |
| 19 | +use PHPStan\Rules\IdentifierRuleError; |
| 20 | +use PHPStan\Rules\Rule; |
| 21 | +use PHPStan\Rules\RuleErrorBuilder; |
| 22 | +use Rector\PHPStan\ScopeFetcher; |
| 23 | + |
| 24 | +/** |
| 25 | + * Flags ScopeFetcher::fetch() called right before a cheap guard that does not use the scope. |
| 26 | + * The guard should run first, so most nodes bail out before scope resolution. |
| 27 | + * |
| 28 | + * @implements Rule<ClassMethod> |
| 29 | + * @see \Rector\Utils\PHPStan\Tests\Rule\NoScopeFetchBeforeCheapGuardRule\NoScopeFetchBeforeCheapGuardRuleTest |
| 30 | + */ |
| 31 | +final class NoScopeFetchBeforeCheapGuardRule implements Rule |
| 32 | +{ |
| 33 | + private const string ERROR_MESSAGE = 'Move the "return null" guard above ScopeFetcher::fetch(), as it does not use the scope - most nodes then bail out before scope resolution.'; |
| 34 | + |
| 35 | + public function getNodeType(): string |
| 36 | + { |
| 37 | + return ClassMethod::class; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param ClassMethod $node |
| 42 | + * @return list<IdentifierRuleError> |
| 43 | + */ |
| 44 | + public function processNode(Node $node, Scope $scope): array |
| 45 | + { |
| 46 | + if ($node->name->toString() !== 'refactor') { |
| 47 | + return []; |
| 48 | + } |
| 49 | + |
| 50 | + $stmts = $node->stmts; |
| 51 | + if ($stmts === null) { |
| 52 | + return []; |
| 53 | + } |
| 54 | + |
| 55 | + foreach ($stmts as $key => $stmt) { |
| 56 | + $scopeVariableName = $this->matchScopeFetchVariableName($stmt); |
| 57 | + if ($scopeVariableName === null) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + $nextStmt = $stmts[$key + 1] ?? null; |
| 62 | + if (! $nextStmt instanceof If_) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if (! $this->isReturnNullGuard($nextStmt)) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + if ($this->usesVariable($nextStmt->cond, $scopeVariableName)) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + return [ |
| 75 | + RuleErrorBuilder::message(self::ERROR_MESSAGE) |
| 76 | + ->identifier('rector.scopeFetchBeforeCheapGuard') |
| 77 | + ->line($stmt->getStartLine()) |
| 78 | + ->build(), |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + return []; |
| 83 | + } |
| 84 | + |
| 85 | + private function matchScopeFetchVariableName(Stmt $stmt): ?string |
| 86 | + { |
| 87 | + if (! $stmt instanceof Expression) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + if (! $stmt->expr instanceof Assign) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + $assign = $stmt->expr; |
| 96 | + if (! $assign->var instanceof Variable || ! is_string($assign->var->name)) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + if (! $assign->expr instanceof StaticCall) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + $staticCall = $assign->expr; |
| 105 | + if (! $staticCall->class instanceof Node\Name) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + if ($staticCall->class->toString() !== ScopeFetcher::class) { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + if (! $staticCall->name instanceof Node\Identifier || $staticCall->name->toString() !== 'fetch') { |
| 114 | + return null; |
| 115 | + } |
| 116 | + |
| 117 | + return $assign->var->name; |
| 118 | + } |
| 119 | + |
| 120 | + private function isReturnNullGuard(If_ $if): bool |
| 121 | + { |
| 122 | + if (count($if->stmts) !== 1) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + $onlyStmt = $if->stmts[0]; |
| 127 | + if (! $onlyStmt instanceof Return_) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + return $onlyStmt->expr === null || $this->isNullConstant($onlyStmt->expr); |
| 132 | + } |
| 133 | + |
| 134 | + private function isNullConstant(Expr $expr): bool |
| 135 | + { |
| 136 | + return $expr instanceof Node\Expr\ConstFetch && $expr->name->toLowerString() === 'null'; |
| 137 | + } |
| 138 | + |
| 139 | + private function usesVariable(Expr $expr, string $variableName): bool |
| 140 | + { |
| 141 | + $foundVariable = (new NodeFinder())->findFirst( |
| 142 | + [$expr], |
| 143 | + static fn (Node $subNode): bool => $subNode instanceof Variable && $subNode->name === $variableName |
| 144 | + ); |
| 145 | + |
| 146 | + return $foundVariable instanceof Variable; |
| 147 | + } |
| 148 | +} |
0 commit comments