1

The following code returns different results in two environments (which however were supposed to be as close to one another as possible: the same PHP version, etc.)

The /var/www/html/project/vendor/package/path/to/some/folder directory contains exactly the same set of files in both environments. The files themselves were put there by composer.

use Symfony\Component\Finder\Finder;

// Don't mind this. The real app uses an autoloading mechanism.
require __DIR__ . '/../vendor/symfony/finder/Finder.php';
require __DIR__ . '/../vendor/symfony/finder/SplFileInfo.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/FilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/RecursiveDirectoryIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/ExcludeDirectoryFilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/MultiplePcreFilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/PathFilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/FilenameFilterIterator.php';
require __DIR__ . '/../vendor/symfony/finder/Iterator/FileTypeFilterIterator.php';

$path = '/var/www/html/project/vendor/package/path/to/some/folder';
$path = Finder::create()->files()->followLinks()->name('/\.(php|inc|hh)$/')->in($path);

$iterator = $path->getIterator();
$iterator->rewind();
$current = $iterator->current();
echo $current->getRelativePathname();

It isn't random. The results are consistent in a single environment. But a different path name is diplayed in either environment. I was able to trace it down to PHP's native: FilterIterator::rewind, but I'm not sure if rewinding is the problem here.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • Does any of the documentation imply that you will retrieve the file names in any particular order? – Nigel Ren May 17 '21 at 12:30
  • @NigelRen No, I could not find such information in either. – Krzysztof Wołowski May 17 '21 at 12:51
  • 2
    Personally I would assume there is no specific ordering applied to them. It may be just the order of the entries in the file system or some other internal thing. It may be safe to apply your own sorting if needed. – Nigel Ren May 17 '21 at 12:52

1 Answers1

2

There is no defined "default" order for the Finder component.

Finder's iterator (Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator) extends on the core \RecursiveDirectoryIterator, which does not define a default order (nor a way to "set" the order). The ordering is thus not guaranteed defined or guaranteed. It is highly system-dependant and file-system dependant, and should not be be relied upon.

If you need a dependable, consistent sort order, just use the appropriate sort*() methods on the Finder instance. This will cause the result to be wrapped on a SortableIterator when you call getIterator(), and get you results ordered in a predictable manner.

yivi
  • 42,438
  • 18
  • 116
  • 138
  • 1
    I reached the same conclusion. The order of the files is "directory order" (basically what you get when typing `ls -U`). It's filesystem-dependant and you can't rely on it. – Krzysztof Wołowski May 17 '21 at 14:48