-
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
Fix const bool subtraction from mixed #3421
Conversation
You've opened the pull request against the latest branch 2.0.x. PHPStan 2.0 is not going to be released for months. If your code is relevant on 1.12.x and you want it to be released sooner, please rebase your pull request and change its target to 1.12.x. |
This pull request has been marked as ready for review. |
src/Type/MixedType.php
Outdated
if ($this->subtractedType !== null && StaticTypeFactory::falsey()->equals($this->subtractedType)) { | ||
return new ConstantBooleanType(true); | ||
if ($this->subtractedType !== null) { | ||
if ($this->subtractedType->isSuperTypeOf(new ConstantBooleanType(false))->yes()) { |
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.
This logic is wrong. For example if subtracted type is bool
, this condition will pass and toBoolean
will return true
.
Also many things cast to true or false, making this also wrong.
Here you need something more like if ($this->subtractedType->toBoolean()->isFalse()->yes())
.
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.
great catch!
suprisingly its more complicated then initially expected. I think its in better shape now. |
src/Type/MixedType.php
Outdated
return new ConstantBooleanType(true); | ||
} | ||
if ((new ConstantBooleanType(true))->isSuperTypeOf($this->subtractedType)->yes()) { | ||
return new ConstantBooleanType(false); |
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 still feel this logic is wrong.
- Let's say we subtract more than what's just falsy. Falsy is
0|0.0|''|'0'|array{}|false|null
. But if we subtract more than that, likeint|0.0|''|'0'|array{}|false|null
or0|0.0|''|'0'|array{}|bool|null
, the result should still be the same. (new ConstantBooleanType(false))->isSuperTypeOf($this->subtractedType)->yes()
is also wrong. This representsmixed~false
. But something frommixed
could still be falsy.
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.
after more iterations, I think it turns out I am chasing ghosts and there is no bug.
the tests I came up with now don't fail any longer before this PR.
you may decide whether the tests are worth merging or we just close here, I guess?
} | ||
|
||
if ($m !== $moreThenFalsy) { | ||
assertType('mixed', $m); |
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.
seems the substraction logic atm cannot deal with "subtract more then falsy", as the type stays mixed
.
(I think its a different unrelated bug)
Thank you! |
No description provided.