Skip to content

PHPStan assumes string conversions cannot throw exceptions #13806

@marijnvanwezel

Description

@marijnvanwezel

Bug report

PHPStan seems to incorrectly assume that string conversions cannot throw exceptions. While this used to be the case (although there were obscure workarounds), this is now officially supported: https://wiki.php.net/rfc/tostring_exceptions.

Consider the program below:

try {
  (string) $myVariable;
} catch (Exception) {
  // Reported as dead catch, even though the `__toString()` method
  // in `$myVariable` might throw an exception.
}

See also: https://3v4l.org/Ze7r6#v8.4.14

Code snippet that reproduces the problem

https://phpstan.org/r/f0ac2617-d1d7-4537-bcfe-f5127dfd002c

Expected output

No issues are reported.

Did PHPStan help you today? Did it make you happy in any way?

PHPStan has been incredibly useful for us in detecting errors and issues early! Thank you for the great work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions