Improve performance of find() and findFirst() when passed $nodes is empty array

This commit is contained in:
Abdul Malik Ikhsan 2023-10-07 17:08:14 +07:00 committed by Nikita Popov
parent 4e27a17cd8
commit 481fec47f4

View File

@ -15,6 +15,10 @@ class NodeFinder {
* @return Node[] Found nodes satisfying the filter callback
*/
public function find($nodes, callable $filter): array {
if ($nodes === []) {
return [];
}
if (!is_array($nodes)) {
$nodes = [$nodes];
}
@ -52,6 +56,10 @@ class NodeFinder {
* @return null|Node Found node (or null if none found)
*/
public function findFirst($nodes, callable $filter): ?Node {
if ($nodes === []) {
return null;
}
if (!is_array($nodes)) {
$nodes = [$nodes];
}