-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
disallowedLooseComparison - allow only DateTime/DateTimeImmutable #232
Comments
Hi, you can solve that today by crafting the right regex to ignore specific errors like this: https://phpstan.org/user-guide/ignoring-errors#ignoring-in-configuration-file |
To ignore some custom rule that would e.g. force
|
Where is |
@ondrejmirtes The core point is that following
Should not be triggered for |
Not sure that would work well because the error is simply |
I also believe a custom regex is a bit silly because strict comparison of DateTimes should not be default behavior |
You can compare |
Thanks, that might not be a bad idea. |
Timestamps are second based, DateTimes can be more precise |
disallowedLooseComparison
is a nice rule and can be applicable to most of the cases, except DateTime/DateTimeImmutableComparing these 2 using comparing operators started in PHP 5.2.2, and is documented even in the current documentation:
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;
anddate_object_handlers_immutable.compare = date_object_compare_date;
have both the same comparator which leads us totimelib_time_compare
inhttps://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:
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).
The text was updated successfully, but these errors were encountered: