Skip to content
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

Add support for float and null for strlen, improve support for booleans #1199

Merged
merged 1 commit into from Apr 12, 2022
Merged

Add support for float and null for strlen, improve support for booleans #1199

merged 1 commit into from Apr 12, 2022

Conversation

fluffycondor
Copy link
Contributor

@fluffycondor fluffycondor commented Apr 11, 2022

Improvement for #1162

@@ -38,19 +42,25 @@ public function getTypeFromFunctionCall(

$argType = $scope->getType($args[0]->value);

$constantScalars = TypeUtils::getConstantScalars($argType);
if ($argType->isSuperTypeOf(new BooleanType())->yes()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this condition right? This is true for bool but also for bool|object, and also mixed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it's false for true and false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this condition is right. There a test that covers it:
assertType('int<0, 1>', strlen($emptyStringBoolNull)); // ""|bool|null

The thing is TypeUtils::getConstantScalars() returns constant scalars only if the passed argument contains only constant types. So if the arg is a super type of bool, this code tries to remove bool out of it, and if TypeUtils::getConstantScalars() returns some constant scalars after it, then it's a case when it's a union of bool and some scalars. So in this case this code replaces bool to true|false.
If it's a bool|object or mixed, this code will do nothing, because TypeUtils::getConstantScalars() will return an empty array.

@ondrejmirtes ondrejmirtes merged commit 083d5d1 into phpstan:1.5.x Apr 12, 2022
@ondrejmirtes
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants