symfony/finder · error · InvalidArgumentException
The SortableIterator takes a PHP callable or a valid…
Error message
The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.
What it means
SortableIterator accepts either a built-in sort constant (SORT_BY_NAME, SORT_BY_TYPE, SORT_BY_ACCESSED_TIME, SORT_BY_CHANGED_TIME, SORT_BY_MODIFIED_TIME, SORT_BY_NONE, and their _AS_STRING variants) or a PHP callable comparator; anything else throws InvalidArgumentException with this message.
Solutions
- Use class constants: SortableIterator::SORT_BY_NAME, or a closure fn (\SplFileInfo $a, \SplFileInfo $b) => ...
- Replace string sort names with the corresponding constant or callable
- Verify the sort int is one of the class's SORT_BY_* constants after a library upgrade
Example fix
// before
$finder->sort('name'); // string is neither callable nor a valid algorithm
// after
$finder->sort(\Symfony\Component\Finder\Iterator\SortableIterator::SORT_BY_NAME); Defensive patterns
Strategy: type-guard
Validate before calling
$valid = [SortableIterator::SORT_BY_NONE, SortableIterator::SORT_BY_NAME, SortableIterator::SORT_BY_TYPE, SortableIterator::SORT_BY_ACCESSED_TIME, SortableIterator::SORT_BY_CHANGED_TIME, SortableIterator::SORT_BY_MODIFIED_TIME]; if (!is_callable($sort) && !in_array($sort, $valid, true)) { throw new \InvalidArgumentException('Invalid sort'); } Type guard
function isValidSort(mixed $sort): bool { return is_callable($sort) || in_array($sort, [SortableIterator::SORT_BY_NONE, SortableIterator::SORT_BY_NAME, SortableIterator::SORT_BY_TYPE, SortableIterator::SORT_BY_ACCESSED_TIME, SortableIterator::SORT_BY_CHANGED_TIME, SortableIterator::SORT_BY_MODIFIED_TIME], true); } Try / catch
try { $finder->sort($sort); } catch (\InvalidArgumentException $e) { /* handle invalid sort */ } Prevention
- Use SortableIterator::SORT_BY_* constants instead of raw strings/ints
- Pass closures typed as fn (\SplFileInfo $a, \SplFileInfo $b): int for custom sorts
- Map config sort names ('name', 'size') to constants in one place
- Re-check constant usage after Finder upgrades
When it happens
Trigger: new SortableIterator($iterator, 'name') (string not a callable/constant), Finder::sort('bogus'), passing an int outside the defined constant range, or a non-callable value like an array of sort options.
Common situations: Passing 'name' as a string expecting it to map to SORT_BY_NAME, using removed/renamed constants after upgrading, config-driven sort values read as raw strings.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Invalid operator " ".
- Invalid PHP callback.
- Don't understand " " as a date test.
- Don't understand " " as a number test.
- Invalid number " ".
AI-assisted analysis of symfony/finder@4d6c057bfd (2026-09-13).
Data as JSON: /api/errors/fdf9cf5420e76758.
Report an issue: GitHub.
Appendix: source
Thrown at Iterator/SortableIterator.php:83
return $order * strcmp($a->getRealPath() ?: $a->getPathname(), $b->getRealPath() ?: $b->getPathname());
};
} elseif (self::SORT_BY_ACCESSED_TIME === $sort) {
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * ($a->getATime() - $b->getATime());
} elseif (self::SORT_BY_CHANGED_TIME === $sort) {
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * ($a->getCTime() - $b->getCTime());
} elseif (self::SORT_BY_MODIFIED_TIME === $sort) {
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * ($a->getMTime() - $b->getMTime());
} elseif (self::SORT_BY_EXTENSION === $sort) {
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * strnatcmp($a->getExtension(), $b->getExtension());
} elseif (self::SORT_BY_SIZE === $sort) {
$this->sort = static fn (\SplFileInfo $a, \SplFileInfo $b) => $order * ($a->getSize() - $b->getSize());
} elseif (self::SORT_BY_NONE === $sort) {
$this->sort = $order;
} elseif (\is_callable($sort)) {
$this->sort = $reverseOrder ? static fn (\SplFileInfo $a, \SplFileInfo $b) => -$sort($a, $b) : $sort(...);
} else {
throw new \InvalidArgumentException('The SortableIterator takes a PHP callable or a valid built-in sort algorithm as an argument.');
}
}
public function getIterator(): \Traversable
{
if (1 === $this->sort) {
yield from $this->iterator;
return;
}
$keys = $values = [];
foreach ($this->iterator as $key => $value) {
$keys[] = $key;
$values[] = $value;
}
if (-1 === $this->sort) {View on GitHub (pinned to 4d6c057bfd)