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
support subtracted-mixed in toNumber() #1949
Conversation
c6b2f86
to
8c13b7c
Compare
This pull request has been marked as ready for review. |
src/Type/MixedType.php
Outdated
if ($float->isSuperTypeOf($this->subtractedType)->yes()) { | ||
return $int; | ||
} | ||
if ($int->isSuperTypeOf($this->subtractedType)->yes()) { | ||
return $float; | ||
} |
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.
What if both float
and int
are subtracted?
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.
sorry, can someone explain to me how this works in general? if something is e.g. not a float it could be e.g. an object still and objects can't be incremented?
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.
What if both
float
andint
are subtracted?
yeah, we could return a NeverType
then.
sorry, can someone explain to me how this works in general? if something is e.g. not a float it could be e.g. an object still and objects can't be incremented?
the incrementing test-case is kind of synthetic.. I "reverse engineered" it from looking at toNumber()
uses in the codebase:
phpstan-src/src/Analyser/MutatingScope.php
Lines 975 to 977 in 213863e
if ($node instanceof Node\Expr\UnaryPlus) { | |
return $this->getType($node->expr)->toNumber(); | |
} |
decting invalid cases like $object++
is the job of a rule, not the narrowing done here I think
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.
Even if float is subtracted, the result can still be a float... Because MixedType
without FloatType
can still be numeric-string for example... https://3v4l.org/StqSP
8c13b7c
to
44894ad
Compare
if (!is_int($mixed)) { | ||
assertType('mixed~float|int', $mixed); | ||
$mixed++; | ||
assertType('*NEVER*', $mixed); |
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.
Could still be a string though? https://3v4l.org/D4BHP
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.
yeah,.. I think the src/ changes actually makes sense .. but my test is to synthetic and your example shows that we cannot return a Never
type for toNumber
fbd9f6d
to
0450d0f
Compare
This pull request has been marked as ready for review. |
03f0a1b
to
52539e7
Compare
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.
Please return ErrorType
if the subtracted type makes sure it can never be a number. Thanks.
Oh actually, other types can be cast to number as well 🤦 |
49209d6
to
8a452c0
Compare
thank you guys for all the examples and ideas. I think we / I learnt from it, that we can't take subtractable into account in I put all the examples into a unit test and fixed a edge case with the PR no longer does what its title suggested, but at least our time discussing it is not lost, but manifested in more testcoverage. thanks again. |
8a452c0
to
d9d607c
Compare
Yeah, at least it was still productive :) Thank you. |
No description provided.