Skip to content

disallowedLooseComparison - allow only DateTime/DateTimeImmutable #232

@realjjaveweb

Description

@realjjaveweb

disallowedLooseComparison is a nice rule and can be applicable to most of the cases, except DateTime/DateTimeImmutable

Comparing these 2 using comparing operators started in PHP 5.2.2, and is documented even in the current documentation:

Note:
DateTimeImmutable and DateTime objects can be compared using comparison operators.

If we dig deeper in the php-src - https://github.com/php/php-src/blob/master/ext/date/php_date.c
we can see that date_object_handlers_date.compare = date_object_compare_date; and date_object_handlers_immutable.compare = date_object_compare_date; have both the same comparator which leads us to timelib_time_compare in https://github.com/php/php-src/blob/master/ext/date/lib/timelib.c#L75 where we can see it DOES compare microseconds which is something some people may worry about.

In any case - I understand why PHP docs for object comparison say this:

When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values (values are compared with ==), and are instances of the same class.

It's because the == allows comparing objects with nested objects. But in many opinion worlds that is still bad and one should rather specify specifically what is to be compared => however that is not the case of DateTime/DateTimeImmutable.

So the core point is - one should be able to make an exception for disallowedLooseComparison - have it option something like "allowDateTime" or maybe just allow it by default.

Final question is whether this exception should only apply to the core classes, or, should also apply to any classes that extend them (like e.g. Carbon).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions