Skip to content

Generic<mixed> overridden by specific type in catch #15198

Description

@zorac

Bug report

If in a try block I set a variable to an instance of a generic class where the generic type is mixed, and in the corresponding catch block I set that variable to an instance of the same generic class but with a specific generic type (e.g. int,) then afterwards PHPStan incorrectly sees that variable as always having the more specific generic type.

try {
    /** @var mixed */
    $bar = 'bar';
    $foo = new Foo($bar);
} catch (\Exception $e) {
    $foo = new Foo(123);
}

PHPStan\dumpType($foo); // returns Foo<int>

Code snippet that reproduces the problem

https://phpstan.org/r/2d6bc77c-c571-4e65-b4eb-53b69b8f59d9

Expected output

No issue should be detected.

PHPStan should see the type of $foo as Foo<mixed>|Foo<int> (or possibly Foo<mixed>.)

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

PHPStan makes me happy by finding bugs before they wake me up for a 3am production bug.

Activity

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