Skip to content

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

@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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy