Skip to content

implement loose comparison#1640

Closed
staabm wants to merge 2 commits intophpstan:1.8.xfrom
staabm:loose-compare
Closed

implement loose comparison#1640
staabm wants to merge 2 commits intophpstan:1.8.xfrom
staabm:loose-compare

Conversation

@staabm
Copy link
Copy Markdown
Contributor

@staabm staabm commented Aug 22, 2022

the tests added with his PR represent the comparison table from https://www.php.net/manual/en/types.comparisons.php inspired by #1602 (comment)

the more time I spent while working with loose comparisons, the more I realize how crazy those compare rules actually are.

@staabm staabm marked this pull request as ready for review August 22, 2022 19:43
@ondrejmirtes
Copy link
Copy Markdown
Member

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.

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Aug 23, 2022

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 #1634, so I will wait until #1634 is merged before starting the refactoring.

@ondrejmirtes
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

@staabm staabm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the new type method.

looked into the test errors and as far as I can say, these look like unrelated to this PR.

return new BooleanType();
}

public function resolveEqualType(Type $leftType, Type $rightType): BooleanType
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if desired, resolveEqualType could be inlined as it is now a single line method

Copy link
Copy Markdown
Member

@ondrejmirtes ondrejmirtes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementations of looseCompare are going to be much more complicated, also i don't think there's such a thing as NonLooseComparableTrait. Every type can be put in ==...

@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Aug 24, 2022

ok, just to get it right: I need to implement/test all types compared against all other types (+itself), right?

@ondrejmirtes
Copy link
Copy Markdown
Member

Yes. It's PITA for sure 😊

@ondrejmirtes
Copy link
Copy Markdown
Member

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.

@staabm staabm marked this pull request as draft August 25, 2022 06:14
@staabm
Copy link
Copy Markdown
Contributor Author

staabm commented Aug 25, 2022

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

@ondrejmirtes
Copy link
Copy Markdown
Member

Hi, I'm cleaning up old and stale PRs. Please send a new PR if you're still interested, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants