You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't cast mixed to string, but can't catch error when you try #6232
Phpstan does not allow cast from mixed to string. Reasonable enough. It is presumably trying to prevent you from using code that will throw an unexpected Exception/Error/Throwable. However, that being the case, you should be allowed to put the cast in a try/catch block. But Phpstan says the catch block can never be executed, which isn't consistent with the previous message, and is certainly not true. You can always tell Phpstan to ignore the line, but shouldn't there be a way to handle this in code without an annotation?
One of these situations (probably the code inside the try and inside the catch) should not be flagged. In my code snippet, either line 10 should not issue an error message, or lines 19 and 20 should not.
Did PHPStan help you today? Did it make you happy in any way?
Knowing nothing about the internals of the program, I would have to leave the answers to your questions to the author. However, in my ideal world, if there is a catch for the error that Phpstan is reporting, it shouldn't report an error in the try section. Catching 'Throwable' will, of course, catch anything. In your example, I don't think it should report, and the same would be true if you're catching 'TypeError' rather than (or in addition to Throwable). But, if you're only, for example, catching 'DivideByZeroError' (and not Throwable nor TypeError), it should report. So, this would not be a special behavior for cast.
Activity
VincentLanglet commentedon Feb 14, 2023
I'm not sure about this.
Does it mean the following try catch https://phpstan.org/r/dfd98987-6f13-46af-8563-5ec931f3e87e shouldn't report a Dead catch because we could get a TypeError ?
oleibman commentedon Feb 14, 2023
Knowing nothing about the internals of the program, I would have to leave the answers to your questions to the author. However, in my ideal world, if there is a catch for the error that Phpstan is reporting, it shouldn't report an error in the try section. Catching 'Throwable' will, of course, catch anything. In your example, I don't think it should report, and the same would be true if you're catching 'TypeError' rather than (or in addition to Throwable). But, if you're only, for example, catching 'DivideByZeroError' (and not Throwable nor TypeError), it should report. So, this would not be a special behavior for cast.
ondrejmirtes commentedon Feb 25, 2023
Not all string conversions throw something catchable, like: https://3v4l.org/Frq47
So instead of catching
\Throwable, I'd try to prevent the error from happening by checking the types first: https://phpstan.org/r/de9e39e1-9f2f-41c2-9083-4093aede675agithub-actions commentedon Mar 29, 2023
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.