Repository: symfony/symfony
File: src/Symfony/Component/Finder/Finder.php (PHP)

Code before:
            $iterator->append(new \IteratorIterator(new LazyIterator(fn () => $this->searchInDirectory($dir))));
        }

        foreach ($this->iterators as $it) {
            $iterator->append($it);
        }

        if ($this->sort || $this->reverseSorting) {
            $iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
        }

        return $iterator;
    }

    /**
     * Appends an existing set of files/directories to the finder.
     *
     * The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
     *
     * @param iterable<SplFileInfo|\SplFileInfo|string> $iterator
     *
     * @return $this
     */
    public function append(iterable $iterator): static
    {
        if ($iterator instanceof static) {
            $this->iterators[] = $iterator->getIterator();
        } else {
            /** @var \ArrayIterator<string, SplFileInfo> $it */
            $it = new \ArrayIterator();
            foreach ($iterator as $file) {
                if ($file instanceof SplFileInfo) {
                    $it[$file->getPathname()] = $file;
                } elseif ($file instanceof \SplFileInfo) {
                    $it[$file->getPathname()] = new SplFileInfo(
                        $file->getPathname(),
                        $file->getPath(),
                        $file->getPathname(),
                    );
                } else {
                    $file = new \SplFileInfo($file);
                    $it[$file->getPathname()] = new SplFileInfo(
                        $file->getPathname(),
                        $file->getPath(),
                        $file->getPathname(),
                    );
                }
            }
            $this->iterators[] = $it;
        }


Review comment (performance):
should be `instanceof self` instead. When using a child class of the finder, we can still optimize this for any Finder, as we know it will already iterate over the appropriate SplFileInfo.

Code after:
                        if (!$file instanceof \SplFileInfo) {
                            $file = new \SplFileInfo($file);
                        }
                        $key = $file->getPathname();
                        if (!$file instanceof SplFileInfo) {
                            $file = new SplFileInfo($key, $file->getPath(), $key);
                        }

                        yield $key => $file;
                    }
                })());
            }
        }

        if ($this->sort || $this->reverseSorting) {
            $iterator = (new SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
        }

        return $iterator;
    }

    /**
     * Appends an existing set of files/directories to the finder.
     *
     * The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
     *
     * @param iterable<SplFileInfo|\SplFileInfo|string> $iterator
     *
     * @return $this
     */
    public function append(iterable $iterator): static
    {
        $this->iterators[] = $iterator;

        return $this;
    }

    /**
     * Check if any results were found.
     */
    public function hasResults(): bool
    {
        foreach ($this->getIterator() as $_) {
            return true;
        }

        return false;
    }

    /**
     * Counts all the results collected by the iterators.