|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Exceptions; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Stmt; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\DependencyInjection\AutowiredParameter; |
| 9 | +use PHPStan\DependencyInjection\RegisteredRule; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | +use function is_array; |
| 13 | + |
| 14 | +/** |
| 15 | + * @implements Rule<Stmt\TryCatch> |
| 16 | + */ |
| 17 | +#[RegisteredRule(level: 0)] |
| 18 | +final class ReturnInFinallyRule implements Rule |
| 19 | +{ |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + #[AutowiredParameter(ref: '%featureToggles.checkWholePhpVersionRange%')] |
| 23 | + private bool $checkWholePhpVersionRange, |
| 24 | + ) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + public function getNodeType(): string |
| 29 | + { |
| 30 | + return Stmt\TryCatch::class; |
| 31 | + } |
| 32 | + |
| 33 | + public function processNode(Node $node, Scope $scope): array |
| 34 | + { |
| 35 | + if ($node->finally === null) { |
| 36 | + return []; |
| 37 | + } |
| 38 | + |
| 39 | + $deprecatesReturnInFinally = $scope->getPhpVersion()->deprecatesReturnInFinally(); |
| 40 | + if ($this->checkWholePhpVersionRange ? $deprecatesReturnInFinally->no() : !$deprecatesReturnInFinally->yes()) { |
| 41 | + return []; |
| 42 | + } |
| 43 | + |
| 44 | + $errors = []; |
| 45 | + foreach ($this->findReturns($node->finally->stmts) as $return) { |
| 46 | + $errors[] = RuleErrorBuilder::message('Returning from a finally block is deprecated in PHP 8.6.') |
| 47 | + ->line($return->getStartLine()) |
| 48 | + ->identifier('finally.deprecatedReturn') |
| 49 | + ->build(); |
| 50 | + } |
| 51 | + |
| 52 | + return $errors; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Like PHP, looks for return statements lexically, without descending into |
| 57 | + * functions, classes, and expressions (which is where closures live). |
| 58 | + * A nested try's own finally block is left out because this rule reports it separately. |
| 59 | + * |
| 60 | + * @param Node[] $nodes |
| 61 | + * @return list<Stmt\Return_> |
| 62 | + */ |
| 63 | + private function findReturns(array $nodes): array |
| 64 | + { |
| 65 | + $returns = []; |
| 66 | + foreach ($nodes as $node) { |
| 67 | + if (!$node instanceof Stmt || $node instanceof Stmt\Function_ || $node instanceof Stmt\ClassLike) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + if ($node instanceof Stmt\Return_) { |
| 72 | + $returns[] = $node; |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + $subNodeNames = $node instanceof Stmt\TryCatch ? ['stmts', 'catches'] : $node->getSubNodeNames(); |
| 77 | + foreach ($subNodeNames as $subNodeName) { |
| 78 | + $subNode = $node->$subNodeName; |
| 79 | + foreach ($this->findReturns(is_array($subNode) ? $subNode : [$subNode]) as $return) { |
| 80 | + $returns[] = $return; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return $returns; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments