Skip to content

Conversation

@fluffycondor
Copy link
Contributor

No description provided.

Comment on lines +45 to +50
Copy link
Contributor

@staabm staabm Apr 1, 2022

Choose a reason for hiding this comment

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

would toString also work here instead of these 3 separated cases?
(let the type system decide whether the scalar is castable to string)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

toString returns PHPStan\Type\Type.
I don't see any way to get value from it, maybe I don't understand how to do it

Copy link
Contributor

@staabm staabm Apr 1, 2022

Choose a reason for hiding this comment

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

I am not sure about it, but I guess this could work:

$toStringType = $constantScalar->toString();
if ($toStringType instanceof ConstantStringType) {
  $len = strlen($toStringType->getValue());
}

your added tests will tell :)

Copy link
Contributor

Choose a reason for hiding this comment

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

what about float?
https://3v4l.org/XVRAE

maybe even null?
https://3v4l.org/J1pbq

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!
I will add support for these

@ondrejmirtes
Copy link
Member

I wonder how useful these are if this error is reported on them? https://phpstan.org/r/71827703-d925-432a-87a9-2e8b6fbbb291

@fluffycondor
Copy link
Contributor Author

fluffycondor commented Apr 4, 2022

I wonder how useful these are if this error is reported on them? https://phpstan.org/r/71827703-d925-432a-87a9-2e8b6fbbb291

This kind of errors can be manually suppressed, and return value can be used in the further calculations. I think it would be better (or at least it costs nothing) that PHPStan would be able to figure out the length of vars that are actually acceptable by strlen.
https://phpstan.org/r/b433ed2a-985c-49e0-a434-f273e4f9d36c

@ondrejmirtes ondrejmirtes changed the base branch from 1.6.x to 1.5.x April 4, 2022 14:32
@ondrejmirtes ondrejmirtes merged commit a29dc57 into phpstan:1.5.x Apr 4, 2022
@ondrejmirtes
Copy link
Member

Thank you. Please send any modifications as a future PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants