-
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
More precise mixed-type subtraction #3420
Conversation
a26a7a0
to
1f9a3bf
Compare
after a break and thinking about your feedback, I came to a new implementation idea :). |
just reduced this PR to |
$accessories[] = new AccessoryNonFalsyStringType(); | ||
} | ||
return new IntersectionType( | ||
$accessories, |
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 think I'm missing floats here. See https://3v4l.org/7kgpL
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.
maybe I missed your point, but float 0.0
is substracted in line 544
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 was caught off guard a bit because this method (https://github.com/phpstan/phpstan-src/pull/3434/files#diff-23a89368c86f55bda583e138270384edec06835b889677bb17fc84e581e36dfcR494) mentions whole FloatType
and this one mentions just constant 0.0
, is there a difference on purpose or does it even matter?
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.
the difference is, that there is a bunch of float numbers which cast to int 0
, but only a single one which casts to string '0'
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.
Oh right :)
Thank you! |
@@ -526,6 +528,32 @@ public function toFloat(): Type | |||
|
|||
public function toString(): Type | |||
{ | |||
if ($this->subtractedType !== null) { | |||
$castsToEmptyString = new UnionType([ |
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.
In the same way there AllowedArrayKeysTypes::getType
I dunno if it would be interesting to introduce a class/static methods for
- castsToZero
- castsToEmptyString
- castsToZeroString
- ...
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.
do you see a use-case where we need the same implementation somewhere else?
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.
No indeed, there is all the following union which is part similar but always different so far
NullType::getSmallerOrEqualType
return new UnionType([
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType(''),
new ConstantArrayType([], []),
]);
NullType::getGreaterType
return new MixedType(false, new UnionType([
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType(''),
new ConstantArrayType([], []),
]));
$castsToZero = new UnionType([
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantArrayType([], []),
new StringType(),
new FloatType(), // every 0.x float casts to int(0)
]);
$castsToEmptyString = new UnionType([
new NullType(),
new ConstantBooleanType(false),
new ConstantStringType(''),
]);
$castsToZeroString = new UnionType([
new ConstantFloatType(0.0),
new ConstantStringType('0'),
new ConstantIntegerType(0),
]);
StaticTypeFactory::falsey
$falsey = new UnionType([
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType(''),
new ConstantStringType('0'),
new ConstantArrayType([], []),
]);
realized this nit while looking more into division related types