shipmonk-rnd/phpstan-rules

Custom allowComparingOnlyComparableTypes

Closed this issue · 4 comments

Hello

Thank you for this great set of phpstan rules.

We developed http://github.com/assoconnect/php-date which supports comparison.
allowComparingOnlyComparableTypes is powerful but isn't customizable so I can't enable it.

Could you please make this rule accept a whitelist of function which supports comparison?

Thank you!

I believe comparing objects in PHP (>,<,<=,>=,<=>) is too magical and should be avoided. You cannot easily overload operators and default PHP implementation compares properties based on their definition order which can easily break by some refactoring of the class. See example. Therefore I do not want to support such feature. Typical implementation for such objects in PHP is to add methods for comparison (isAfter, isBefore, ...), see e.g. brick/datetime implementation.

You can always disable that rule and copy-paste it to your codebase with the changes you need.

Hello @janedbal

Thank you for the example, I wasn't aware of it!

I'm curious: why is the comparison of DateTimeInterface allowed then?

Because it is PHP internal class that has special implementation for it in PHP core.

Makes sense, thank you for the explanation 👍