-
Notifications
You must be signed in to change notification settings - Fork 504
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
Introduce isSuperTypeOfWithReason #3538
Conversation
src/Type/AcceptsResult.php
Outdated
public function negate(): self | ||
{ | ||
return new self($this->result->negate(), $this->reasons); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dunno if it makes sens to have a negate method (Should the reasons be lost ?)
I created one since I had multiple Trinary::negate calls to replace.
src/Type/IntegerRangeType.php
Outdated
return TrinaryLogic::createNo()->lazyOr($otherType->getTypes(), fn (Type $innerType) => $this->isSubTypeOf($innerType)); | ||
return AcceptsResult::createNo()->or(...array_map(fn (Type $innerType) => $this->isSubTypeOfWithReason($innerType), $otherType->getTypes())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we're loosing a lazyOr
into a or
.
I dunno if the lazy could be kept on AcceptsResults but I saw similar lazy lost when isAcceptedWithReasonBy
was introduced
src/Type/IntersectionType.php
Outdated
return TrinaryLogic::createYes()->lazyAnd($this->getTypes(), static fn (Type $innerType) => $innerType->isSuperTypeOf($otherType)); | ||
return AcceptsResult::createYes()->and(...array_map(static fn (Type $innerType) => $innerType->isSuperTypeOfWithReason($otherType), $this->types)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we're loosing a lazyAnd
into a and
.
I dunno if the lazy could be kept on AcceptsResults but I saw similar lazy lost when isAcceptedWithReasonBy
was introduced
src/Type/IntersectionType.php
Outdated
$result = TrinaryLogic::lazyMaxMin($this->getTypes(), static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType)); | ||
$result = AcceptsResult::maxMin(...array_map(static fn (Type $innerType) => $otherType->isSuperTypeOfWithReason($innerType), $this->types)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we're loosing a lazyMaxMin
into a maxMin
.
I dunno if the lazy could be kept on AcceptsResults but I saw similar lazy lost when isAcceptedWithReasonBy
was introduced (In this same class for instance).
} | ||
|
||
$result = TrinaryLogic::createNo()->lazyOr($this->getTypes(), static fn (Type $innerType) => $innerType->isSuperTypeOf($otherType)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same, lazyOr into or.
} | ||
|
||
return $result; | ||
} | ||
|
||
public function isSubTypeOf(Type $otherType): TrinaryLogic | ||
{ | ||
return TrinaryLogic::lazyExtremeIdentity($this->getTypes(), static fn (Type $innerType) => $otherType->isSuperTypeOf($innerType)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same lazyExtremeIdentity
into extremeIdentity
, like it's done for isAcceptedWithReasonBy
a98e908
to
c030170
Compare
This looks promising. Could you do a quick proof of concept where this might be useful? In StrictComparisonOfDifferentTypesRule we could read the reasons and output them as a tip (similar to what places that call |
I understand it might be a bit challenging because we might have to propagate that all the way from |
Maybe better to leave it for later, so this can be merged. So nevermind for now, I'll review this PR. |
Yes I had in mind to work on it step by step ^^' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, I was thinking about this a bit more and it'd help me if:
- We didn't reuse AcceptsResult for the
isSuperTypeOfWithReason
return type. - This PR did not do any changes to AcceptsResult.
- Instead, it introduced new class called
IsSuperTypeOfResult
by copying and modifying AcceptsResult to suit the method's needs.
After that I'm very likely to merge this.
Since I created isSubtypeOfWithReason too, does it mean we need a IsSubtypeOfResult too @ondrejmirtes ? This may complicate things since i need to trasnform often subtypeOfResult into superTypeOfResult. |
@VincentLanglet It's fine to use IsSuperTypeOfResult for isSubtypeOf too. |
I'll end with something very similar for AcceptsResult and IsSuperTypeOfResult. I was wondering what's the idea between duplicating such code ; wouldn't it better to introduce something like a Result class which works for everything ? It would make sens because there is code like
So both method rely on isSuperTypeOfInternal and could use the same value object as return type. Another "weird" situation is the fact that isParametersAcceptorSuperTypeOf currently returns an AcceptsResult when the method talk about SuperTypeOf... phpstan-src/src/Type/CallableTypeHelper.php Lines 15 to 19 in c77064a
So I'm not sure how to evolve this PR with the use of a IsSuperTypeOf without using a lot of method like |
This is accidental duplication. The fact these are the same right now doesn't mean they're going to be the same in the future. I tend to develop a hunch for these things. |
This method should return IsSuperTypeOfResult in your PR, because it's used in |
Hey, I need this really soon so I can announce an RC period for 2.0. I'm changing this locally right now. |
I started the changes, do you want me to stop and letting u doing all the changes then ? (Or I push the current changes ?) |
c030170
to
7234623
Compare
Thank you. |
Thanks you too ! |
As suggested by #3536 (comment)