Skip to content

Commit 0d86656

Browse files
authored
[Performance] Avoid multiple traverse on BetterNodeFinder::hasInstanceof() (#6742)
1 parent 5b054ea commit 0d86656

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

src/NodeManipulator/IfManipulator.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,7 @@ public function collectNestedIfsWithNonBreaking(Foreach_ $foreach): array
147147
return [];
148148
}
149149

150-
$return = $this->betterNodeFinder->findFirstInstanceOf($currentIf->stmts, Return_::class);
151-
152-
if ($return instanceof Return_) {
153-
return [];
154-
}
155-
156-
$exit = $this->betterNodeFinder->findFirstInstanceOf($currentIf->stmts, Exit_::class);
157-
if ($exit instanceof Exit_) {
150+
if ($this->betterNodeFinder->hasInstancesOf($currentIf->stmts, [Return_::class, Exit_::class])) {
158151
return [];
159152
}
160153

src/PhpParser/Node/BetterNodeFinder.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,15 @@ public function hasInstancesOf(Node | array $nodes, array $types): bool
112112
{
113113
Assert::allIsAOf($types, Node::class);
114114

115-
foreach ($types as $type) {
116-
$foundNode = $this->nodeFinder->findFirstInstanceOf($nodes, $type);
117-
if (! $foundNode instanceof Node) {
118-
continue;
115+
return (bool) $this->nodeFinder->findFirst($nodes, static function (Node $node) use ($types): bool {
116+
foreach ($types as $type) {
117+
if ($node instanceof $type) {
118+
return true;
119+
}
119120
}
120121

121-
return true;
122-
}
123-
124-
return false;
122+
return false;
123+
});
125124
}
126125

127126
/**

0 commit comments

Comments
 (0)