Skip to content

Template type not used for __toString purity #14504

@still-dreaming-1

Description

@still-dreaming-1

Bug report

I'm migrating a very advanced project from Psalm to PHPStan, so please bear with me and provide whatever feedback you feel will be helpful for me to learn to use PHPStan better.

Casting a value to a string that has a template type that must be of string|int is not allowed from a pure context. The exact error message I'm getting is

Possibly impure call to method stdClass::__toString() in pure function stringIt().

The error id is possiblyImpure.methodCall

Code snippet that reproduces the problem

https://phpstan.org/r/e8d13b04-022c-452e-b291-109187ca9156

Expected output

It should be allowed with no issues because casting an int or a string to a string is pure.

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

I'm amazed how well PHPStan is working overall so far with this migration from Psalm. Very nice feature parity for the most part, with differences that I like. It seems very stable and non-buggy overall. I'm glad both Psalm and PhpStan have existed together and evolved for so long with both teams learning from each other.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions