|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ItkDev\OpenIdConnectBundle\PHPStan\Rule; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Arg; |
| 7 | +use PhpParser\Node\Expr\New_; |
| 8 | +use PhpParser\Node\Expr\Throw_; |
| 9 | +use PhpParser\Node\Expr\Variable; |
| 10 | +use PhpParser\Node\Stmt\Catch_; |
| 11 | +use PHPStan\Analyser\Scope; |
| 12 | +use PHPStan\Rules\Rule; |
| 13 | +use PHPStan\Rules\RuleErrorBuilder; |
| 14 | + |
| 15 | +/** |
| 16 | + * When a catch block rethrows by constructing a new exception, the caught |
| 17 | + * exception must be chained as `$previous` (positional 3rd argument or named |
| 18 | + * `previous:`). This enforces the "wrap at the boundary" rule in ADR 001, |
| 19 | + * preserving `getPrevious()` traversal for logs and debugging. |
| 20 | + * |
| 21 | + * Escape hatch: add "phpstan-ignore throw.unchainedPrevious" on the throw line |
| 22 | + * with a justification comment when the rethrow is intentionally unrelated to |
| 23 | + * the caught cause. |
| 24 | + * |
| 25 | + * @implements Rule<Catch_> |
| 26 | + */ |
| 27 | +final class WrappedExceptionChainsPrevious implements Rule |
| 28 | +{ |
| 29 | + public function getNodeType(): string |
| 30 | + { |
| 31 | + return Catch_::class; |
| 32 | + } |
| 33 | + |
| 34 | + public function processNode(Node $node, Scope $scope): array |
| 35 | + { |
| 36 | + if (null === $node->var) { |
| 37 | + return []; |
| 38 | + } |
| 39 | + |
| 40 | + $catchVarName = $node->var->name; |
| 41 | + if (!is_string($catchVarName)) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + |
| 45 | + $errors = []; |
| 46 | + foreach ($this->findThrows($node->stmts) as $throw) { |
| 47 | + $newExpr = $throw->expr; |
| 48 | + if (!$newExpr instanceof New_) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + if ($this->chainsCaughtAsPrevious($newExpr, $catchVarName)) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + $thrownLabel = $this->describeNewExpr($newExpr); |
| 57 | + $errors[] = RuleErrorBuilder::message(sprintf( |
| 58 | + 'Exception %s thrown inside catch($%s) does not chain the caught exception as `previous`. Pass `previous: $%s` (or as the 3rd positional argument), or annotate with `@phpstan-ignore throw.unchainedPrevious` when the unrelated throw is intentional.', |
| 59 | + $thrownLabel, |
| 60 | + $catchVarName, |
| 61 | + $catchVarName, |
| 62 | + )) |
| 63 | + ->identifier('throw.unchainedPrevious') |
| 64 | + ->line($throw->getStartLine()) |
| 65 | + ->build(); |
| 66 | + } |
| 67 | + |
| 68 | + return $errors; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Collect every `throw` node reachable from these statements, but stop at |
| 73 | + * nested catch blocks — those bind a different catch variable and are |
| 74 | + * inspected by their own rule invocation. |
| 75 | + * |
| 76 | + * @param Node[] $stmts |
| 77 | + * |
| 78 | + * @return list<Throw_> |
| 79 | + */ |
| 80 | + private function findThrows(array $stmts): array |
| 81 | + { |
| 82 | + $found = []; |
| 83 | + foreach ($stmts as $stmt) { |
| 84 | + $this->collect($stmt, $found); |
| 85 | + } |
| 86 | + |
| 87 | + return $found; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param list<Throw_> $found |
| 92 | + */ |
| 93 | + private function collect(Node $node, array &$found): void |
| 94 | + { |
| 95 | + if ($node instanceof Catch_) { |
| 96 | + // A nested catch owns its own catch variable scope; its rethrows are |
| 97 | + // checked when that catch is processed. |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + if ($node instanceof Throw_) { |
| 102 | + $found[] = $node; |
| 103 | + } |
| 104 | + |
| 105 | + foreach ($node->getSubNodeNames() as $subName) { |
| 106 | + $sub = $node->{$subName}; // @phpstan-ignore property.dynamicName (walking an AST node's children requires a dynamic subnode name lookup; the names come from getSubNodeNames() and are intrinsically dynamic) |
| 107 | + if ($sub instanceof Node) { |
| 108 | + $this->collect($sub, $found); |
| 109 | + } elseif (is_array($sub)) { |
| 110 | + foreach ($sub as $item) { |
| 111 | + if ($item instanceof Node) { |
| 112 | + $this->collect($item, $found); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Accept either a named `previous: $catchVar` argument, or any argument whose |
| 121 | + * value is directly the catch variable. The latter covers conventional PHP |
| 122 | + * exceptions (positional $previous at index 2) and Symfony's HTTP exception |
| 123 | + * subclasses (which bake the status code into the type and place $previous |
| 124 | + * at index 1). Passing the catch variable as any other slot is unusual in |
| 125 | + * practice; the looser check trades a rare false-negative for not depending |
| 126 | + * on constructor reflection. |
| 127 | + */ |
| 128 | + private function chainsCaughtAsPrevious(New_ $new, string $catchVarName): bool |
| 129 | + { |
| 130 | + foreach ($new->args as $arg) { |
| 131 | + if (!$arg instanceof Arg) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + |
| 135 | + if (null !== $arg->name && 'previous' === $arg->name->name) { |
| 136 | + return $this->valueIsVariable($arg->value, $catchVarName); |
| 137 | + } |
| 138 | + |
| 139 | + if (null === $arg->name && $this->valueIsVariable($arg->value, $catchVarName)) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + private function valueIsVariable(Node\Expr $expr, string $name): bool |
| 148 | + { |
| 149 | + return $expr instanceof Variable && is_string($expr->name) && $expr->name === $name; |
| 150 | + } |
| 151 | + |
| 152 | + private function describeNewExpr(New_ $new): string |
| 153 | + { |
| 154 | + $cls = $new->class; |
| 155 | + if ($cls instanceof Node\Name) { |
| 156 | + return $cls->toString(); |
| 157 | + } |
| 158 | + |
| 159 | + return 'anonymous/dynamic exception'; |
| 160 | + } |
| 161 | +} |
0 commit comments