Skip to content

DateComparator : operator does not fall back to its default value #59314

Closed
@GlucNAc

Description

@GlucNAc

Symfony version(s) affected

6.4.0

Description

This "bug" is present since version 2.1, so not sure if it's really a bug, but I think something must be done regarding at least the DX of the exception message thrown by the use case below.

How to reproduce

use Symfony\Component\Finder\Comparator\DateComparator;

new DateComparator('yesterday');

// leads to:
// Uncaught InvalidArgumentException: Invalid operator "".
// (thrown by base class Symfony\Component\Finder\Comparator\Comparator)

Possible Solution

In Symfony\Component\Finder\Comparator\DateComparator constructor, we could replace

$operator = $matches[1] ?? '==';

by

$operator = $matches[1] ?: '==';

as in this case

$matches[1] === ""

And maybe we could improve the Symfony\Component\Finder\Comparator\Comparator constructor signature with this anotation (to help phpstan raising error):

/**
 * @param non-empty-string $target
 */
public function __construct(string $target, string $operator = '==')
{
    if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
        throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
    }

    $this->target = $target;
    $this->operator = $operator;
}

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDXDX = Developer eXperience (anything that improves the experience of using Symfony)FinderStatus: Needs Review

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions