-
Notifications
You must be signed in to change notification settings - Fork 461
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,9 @@ | |
use PHPStan\Type\Accessory\OversizedArrayType; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\Constant\ConstantFloatType; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\Generic\TemplateMixedType; | ||
use PHPStan\Type\Generic\TemplateType; | ||
use PHPStan\Type\Traits\NonGeneralizableTypeTrait; | ||
|
@@ -504,7 +506,7 @@ public function toInteger(): Type | |
new ConstantIntegerType(0), | ||
new ConstantArrayType([], []), | ||
new StringType(), | ||
new FloatType(), | ||
new FloatType(), // every 0.x float casts to int(0) | ||
]); | ||
if ( | ||
$this->subtractedType !== null | ||
|
@@ -526,6 +528,32 @@ public function toFloat(): Type | |
|
||
public function toString(): Type | ||
{ | ||
if ($this->subtractedType !== null) { | ||
$castsToEmptyString = new UnionType([ | ||
new NullType(), | ||
new ConstantBooleanType(false), | ||
new ConstantStringType(''), | ||
]); | ||
if ($this->subtractedType->isSuperTypeOf($castsToEmptyString)->yes()) { | ||
$accessories = [ | ||
new StringType(), | ||
new AccessoryNonEmptyStringType(), | ||
]; | ||
|
||
$castsToZeroString = new UnionType([ | ||
new ConstantFloatType(0.0), | ||
new ConstantStringType('0'), | ||
new ConstantIntegerType(0), | ||
]); | ||
if ($this->subtractedType->isSuperTypeOf($castsToZeroString)->yes()) { | ||
$accessories[] = new AccessoryNonFalsyStringType(); | ||
} | ||
return new IntersectionType( | ||
$accessories, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. maybe I missed your point, but float There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right :) |
||
); | ||
} | ||
} | ||
|
||
return new StringType(); | ||
} | ||
|
||
|
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 forThere 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
NullType::getGreaterType
StaticTypeFactory::falsey