How to disallow casting mixed to string #9199
Unanswered
still-dreaming-1
asked this question in
Q&A
Replies: 2 comments 1 reply
-
Surely looks like a missing feature to me. |
Beta Was this translation helpful? Give feedback.
0 replies
-
We have a RiskyCast for some known weirdness (like casting an array to int) but it is not triggered on |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This might be an issue, but I wanted to start it as a discussion first because it seems so fundamental it must have been thought about already. Also, I think I remember reading about this somewhere but now I can't find anything about it.
Is there a setting or some way to not allow casting mixed to string?
Currently in the project I'm working on, if a type is known to be mixed by Psalm, it will allow me to cast it to string. I can see how this is very convenient and many programmers may want to do this, but technically it is invalid and so it seems like Psalm should consider it a PossiblyInvalidCast or something like that. The reason I say this is, php does not consider all casts to a string as being valid. For example, if you cast an array to a string you will get an "Array to string conversion" warning and they are always converted to the string "Array". Also you will get a php error if you try to cast an object to string if its class does not have a
__toString()
method. There may be other types this applies to as well, I'm not sure. Casting something to a string is not the same as asserting that it is a string or that it is castable to a string. There could still be a programming mistake where the type is actually an array and should not be casted to a string, so Psalm should help us catch this.Beta Was this translation helpful? Give feedback.
All reactions