|
15 | 15 |
|
16 | 16 | use PhpParser\Node; |
17 | 17 | use PhpParser\Node\Expr\ErrorSuppress; |
| 18 | +use PhpParser\Node\Stmt\Class_; |
| 19 | +use PhpParser\Node\Stmt\Function_; |
18 | 20 | use PhpParser\Node\Stmt\TryCatch; |
| 21 | +use PhpParser\NodeTraverser; |
19 | 22 | use Rector\Core\Rector\AbstractRector; |
20 | 23 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
21 | 24 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
@@ -45,29 +48,37 @@ public function getRuleDefinition(): RuleDefinition |
45 | 48 | */ |
46 | 49 | public function getNodeTypes(): array |
47 | 50 | { |
48 | | - return [ErrorSuppress::class]; |
| 51 | + return [TryCatch::class]; |
49 | 52 | } |
50 | 53 |
|
51 | 54 | /** |
52 | | - * @param ErrorSuppress $node |
| 55 | + * @param TryCatch $node |
53 | 56 | */ |
54 | 57 | public function refactor(Node $node): ?Node |
55 | 58 | { |
56 | | - $tryCatch = $this->betterNodeFinder->findParentType($node, TryCatch::class); |
| 59 | + $hasChanged = false; |
57 | 60 |
|
58 | | - // not in try catch |
59 | | - if (! $tryCatch instanceof TryCatch) { |
60 | | - return null; |
61 | | - } |
| 61 | + $this->traverseNodesWithCallable( |
| 62 | + $node->stmts, |
| 63 | + static function (Node $subNode) use (&$hasChanged) { |
| 64 | + if ($subNode instanceof Class_ || $subNode instanceof Function_) { |
| 65 | + return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; |
| 66 | + } |
| 67 | + |
| 68 | + if ($subNode instanceof ErrorSuppress) { |
| 69 | + $hasChanged = true; |
| 70 | + |
| 71 | + return $subNode->expr; |
| 72 | + } |
62 | 73 |
|
63 | | - $inStmts = (bool) $this->betterNodeFinder->findFirst((array) $tryCatch->stmts, static fn (Node $n): bool => $n === $node); |
| 74 | + return null; |
| 75 | + } |
| 76 | + ); |
64 | 77 |
|
65 | | - // not in stmts, means it in catch or finally |
66 | | - if (! $inStmts) { |
67 | | - return null; |
| 78 | + if ($hasChanged) { |
| 79 | + return $node; |
68 | 80 | } |
69 | 81 |
|
70 | | - // in try { ... } stmts |
71 | | - return $node->expr; |
| 82 | + return null; |
72 | 83 | } |
73 | 84 | } |
0 commit comments