Skip to content

Commit 1dc8b6b

Browse files
authored
Merge pull request #7687 from samsonasik/remove-find-parent
[Rector] Remove findParentType() on RemoveErrorSuppressInTryCatchStmtsRector
2 parents 97f9034 + 14171bf commit 1dc8b6b

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

utils/Rector/RemoveErrorSuppressInTryCatchStmtsRector.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
use PhpParser\Node;
1717
use PhpParser\Node\Expr\ErrorSuppress;
18+
use PhpParser\Node\Stmt\Class_;
19+
use PhpParser\Node\Stmt\Function_;
1820
use PhpParser\Node\Stmt\TryCatch;
21+
use PhpParser\NodeTraverser;
1922
use Rector\Core\Rector\AbstractRector;
2023
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2124
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -45,29 +48,37 @@ public function getRuleDefinition(): RuleDefinition
4548
*/
4649
public function getNodeTypes(): array
4750
{
48-
return [ErrorSuppress::class];
51+
return [TryCatch::class];
4952
}
5053

5154
/**
52-
* @param ErrorSuppress $node
55+
* @param TryCatch $node
5356
*/
5457
public function refactor(Node $node): ?Node
5558
{
56-
$tryCatch = $this->betterNodeFinder->findParentType($node, TryCatch::class);
59+
$hasChanged = false;
5760

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+
}
6273

63-
$inStmts = (bool) $this->betterNodeFinder->findFirst((array) $tryCatch->stmts, static fn (Node $n): bool => $n === $node);
74+
return null;
75+
}
76+
);
6477

65-
// not in stmts, means it in catch or finally
66-
if (! $inStmts) {
67-
return null;
78+
if ($hasChanged) {
79+
return $node;
6880
}
6981

70-
// in try { ... } stmts
71-
return $node->expr;
82+
return null;
7283
}
7384
}

0 commit comments

Comments
 (0)