symfony/finder · error · InvalidArgumentException

Don't understand " " as a date test.

Error message

Don't understand "%s" as a date test.

What it means

DateComparator's constructor parses the test string with a regex for an optional operator followed by a date expression; if the regex cannot extract an operator+date, it throws InvalidArgumentException saying it cannot understand the string as a date test. Note the regex is permissive, so this rarely fires for non-null strings — but null or structurally impossible input hits it.

Solutions

  1. Pass a non-empty string containing an optional operator and a parsable date, e.g. 'since yesterday' or '> 2024-01-01'
  2. Validate the input is a non-null, non-empty string before calling DateComparator/Finder::date()
  3. If the date itself is bad (regex matches but DateTime fails), you'll get the sibling 'is not a valid date' error — fix the date string instead

Example fix

// before
$finder->date($userInput); // null -> "Don't understand \"\" as a date test."
// after
$finder->date(is_string($userInput) && $userInput !== '' ? $userInput : 'since 1970-01-01');
Defensive patterns

Strategy: validation

Validate before calling

if (!is_string($test) || trim($test) === '') { throw new \InvalidArgumentException('Date test must be a non-empty string'); }

Type guard

function isNonEmptyString(mixed $v): bool { return is_string($v) && trim($v) !== ''; }

Try / catch

try { $finder->date($test); } catch (\InvalidArgumentException $e) { /* handle bad date test */ }

Prevention

When it happens

Trigger: new DateComparator($test) where $test does not match '#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i' — practically only null passed where a string is expected (typed as string) or empty/non-string coercion edge cases in Finder's ->date() calls.

Common situations: Passing null to Finder::date(), calling ->date('') via dynamic config, mistyping so the variable is null, frameworks passing user input unvalidated.

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


AI-assisted analysis of symfony/finder@4d6c057bfd (2026-09-13). Data as JSON: /api/errors/14221dc2e3dbaa7c. Report an issue: GitHub.

Appendix: source

Thrown at Comparator/DateComparator.php:29

namespace Symfony\Component\Finder\Comparator;

/**
 * DateCompare compiles date comparisons.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
class DateComparator extends Comparator
{
    /**
     * @param string $test A comparison string
     *
     * @throws \InvalidArgumentException If the test is not understood
     */
    public function __construct(string $test)
    {
        if (!preg_match('#^\s*(==|!=|[<>]=?|after|since|before|until)?\s*(.+?)\s*$#i', $test, $matches)) {
            throw new \InvalidArgumentException(\sprintf('Don\'t understand "%s" as a date test.', $test));
        }

        try {
            $date = new \DateTimeImmutable($matches[2]);
            $target = $date->format('U');
        } catch (\Exception) {
            throw new \InvalidArgumentException(\sprintf('"%s" is not a valid date.', $matches[2]));
        }

        $operator = $matches[1] ?: '==';
        if ('since' === $operator || 'after' === $operator) {
            $operator = '>';
        }

        if ('until' === $operator || 'before' === $operator) {
            $operator = '<';
        }

View on GitHub (pinned to 4d6c057bfd)