Skip to content

Can't cast mixed to string, but can't catch error when you try #6232

Closed

Description

@oleibman

Bug report

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?

Code snippet that reproduces the problem

https://phpstan.org/r/a93b6bbf-c857-420e-a847-b1ffd3db36d5

Expected output

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?

Yes it did. I am generally very happy with it.

Activity

  1. added this to the Easy fixes milestone on Feb 1, 2022
  2. VincentLanglet commented on Feb 14, 2023

    @VincentLanglet
    Contributor

    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 ?

    • If yes: PHPStan will report really less dead catch.
    • If no: why should we have a special behavior for cast ?
  3. oleibman commented on Feb 14, 2023

    @oleibman
    Author

    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.

  4. ondrejmirtes commented on Feb 25, 2023

    @ondrejmirtes
    Member

    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-4093aede675a

  5. github-actions commented on Mar 29, 2023

    @github-actions

    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.

  6. locked as resolved and limited conversation to collaborators on Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions