symfony/finder · error · InvalidArgumentException
Don't understand " " as a number test.
Error message
Don't understand "%s" as a number test.
What it means
NumberComparator's constructor validates the test against '#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i'; if the string is null or doesn't match (wrong characters, unsupported operators, text mixed in), it throws InvalidArgumentException with this message, echoing 'null' when null was passed.
Solutions
- Format the test as [operator] number [k|m|g][i], e.g. '> 10M', '>= 1Gi', '== 500k'
- Reject/normalize null before calling, and validate with the same regex
- Remove commas, currency signs, and unsupported unit suffixes like 'b' or 'KB'
Example fix
// before
$finder->size('>= 10 MB'); // no match
// after
$finder->size('>= 10M'); // or '>= 10Mi' for binary megabytes Defensive patterns
Strategy: validation
Validate before calling
if (!is_string($size) || !preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $size)) { throw new \InvalidArgumentException("Bad size test: $size"); } Type guard
function isValidSizeTest(mixed $v): bool { return is_string($v) && (bool) preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $v); } Try / catch
try { $finder->size($size); } catch (\InvalidArgumentException $e) { /* handle bad size test */ } Prevention
- Use the documented format: [op] number [k|m|g][i], e.g. '> 10M'
- Never pass null; default to a valid expression
- Strip commas, spaces inside the number, and unsupported units like 'MB'
- Reuse the library's regex in a shared validator for config values
When it happens
Trigger: Finder::size(null), ->size('about 100'), ->size('==100MB') (unsupported 'b' suffix/multi-char units), ->size('>1,000') (comma), ->size('<>10'), or any string with characters outside digits, dot, optional k/m/g/i suffix.
Common situations: Users writing human sizes like '10 MB' instead of '10M' or '10Mi', passing null from unresolved config, using '===' or '=>' operators, thousands separators.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- Invalid number " ".
- Invalid operator " ".
- Don't understand " " as a date test.
- " " is not a valid date.
- Invalid PHP callback.
AI-assisted analysis of symfony/finder@4d6c057bfd (2026-09-13).
Data as JSON: /api/errors/bd562ed6193d07d5.
Report an issue: GitHub.
Appendix: source
Thrown at Comparator/NumberComparator.php:45
*
* @author Fabien Potencier <fabien@symfony.com> PHP port
* @author Richard Clamp <richardc@unixbeard.net> Perl version
* @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
* @copyright 2002 Richard Clamp <richardc@unixbeard.net>
*
* @see http://physics.nist.gov/cuu/Units/binary.html
*/
class NumberComparator extends Comparator
{
/**
* @param string|null $test A comparison string or null
*
* @throws \InvalidArgumentException If the test is not understood
*/
public function __construct(?string $test)
{
if (null === $test || !preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
throw new \InvalidArgumentException(\sprintf('Don\'t understand "%s" as a number test.', $test ?? 'null'));
}
$target = $matches[2];
if (!is_numeric($target)) {
throw new \InvalidArgumentException(\sprintf('Invalid number "%s".', $target));
}
if (isset($matches[3])) {
// magnitude
switch (strtolower($matches[3])) {
case 'k':
$target *= 1000;
break;
case 'ki':
$target *= 1024;
break;
case 'm':
$target *= 1000000;
break;View on GitHub (pinned to 4d6c057bfd)