implement loose comparison
the tests added with his PR represent the comparison table from https://www.php.net/manual/en/types.comparisons.php inspired by https://github.com/phpstan/phpstan-src/pull/1602#discussion_r940628091
the more time I spent while working with loose comparisons, the more I realize how crazy those compare rules actually are.
I think loose comparisons should be implemented via a new method like Type::looseCompare(Type $type): BooleanType. That will allow us to answer all possible situations.
I see.
This implies we also need to implement Type::strictCompare($type): BooleanType in the same PR, because atm InitializerExprTypeResolver->resolveEqualType() is calling into InitializerExprTypeResolver->resolveIdenticalType().
moving the code arround will touch stuff already changed in https://github.com/phpstan/phpstan-src/pull/1634, so I will wait until #1634 is merged before starting the refactoring.
Strict compare method isn't needed, that's already basically isSuperTypeOf, if you see how Identical is implemented in MutatingScope. I don't know of any bug that would suggest lack of something in this area.
ok, just to get it right: I need to implement/test all types compared against all other types (+itself), right?
Yes. It's PITA for sure 😊
If you're going to pursue this, submit an implementation in one or two types first before fully diving in, to make sure we're on the same page. For example StringType::looseCompare() has to account for:
- Any UnionType
- Any IntersectionType
- Against any object it's false
- Looks like against any array it's also false
- To correctly answer when it's
non-empty-string,non-falsy-string,numeric-string...
The UnionType/IntersectionType is probably going to be solved similarly as accepts()/isAcceptedBy() / isSuperTypeOf()/isSubTypeOf(), meaning we're gonna need another method counterpart in CompoundType.
ok thanks.
before bringing up the first looseCompare type, I will give other open PRs of mine some love, which are not blocked by external changes - so we can clean up the pull request queue first
Hi, I'm cleaning up old and stale PRs. Please send a new PR if you're still interested, thanks.