phpunit/php-file-iterator
phpunit/php-file-iterator provides a simple file iterator for PHP projects, helping tools like PHPUnit discover and filter files in directories. Installable via Composer for runtime or dev use, it supports efficient traversal and selection of files for testing workflows.
Start by installing the package via Composer: composer require phpunit/php-file-iterator. Its core purpose is to traverse directories and filter files—ideal for build tools, test runners, or static analysis tools that need to operate on specific file sets. The first use case is typically collecting PHP files in a project:
use PHPUnit\File\Iterator;
$iterator = (new Iterator)->getIterator('/path/to/src', ['php']);
foreach ($iterator as $file) {
echo $file->getPathname() . PHP_EOL;
}
Check the README and source for quick reference on class names (Iterator, Factory) and basic API patterns.
Factory for complex globs: Prefer Factory::getIterator() when using glob patterns (including ** globstar syntax since v5.1.0):
$iterator = Factory::getIterator(['/path/to/**/{src,tests}/*.php']);
addExclude()/addSuffix()/addPrefix() on an Iterator instance:
$iterator = (new Iterator)->addSuffix(['php'])->addPrefix(['Abstract', 'Test']);
$iterator = $iterator->addExclude(['*/.git/*', 'vendor/*']);
phpunit/php-code-coverage), Psalm, and custom tooling to prepare file lists before parsing or linting. Wrap your own RecursiveDirectoryIterator or RecursiveIteratorIterator if needed for extended context.** requires path separators on both sides to be recognized as recursive (e.g., src/**/*.php works; **.php does not). From v5.1+, test with echo $pattern; in debug mode to confirm parsing.realpath() if consistency matters.FilterIterator directly for custom logic (e.g., content-based filtering) rather than chaining filters—Iterator is lightweight and immutable (methods return new instances), so avoid repeated operations in loops.How can I help you explore Laravel packages today?