Skip to content

PHPStan loses type certainty outside of assertion blocks #5754

@gdsmith

Description

@gdsmith

Bug report

On the last line, PHPStan is no longer able to understand that the queue is of the type returned by the context.

Code snippet that reproduces the problem

        if ($context instanceof SnsQsContext) {
            $this->queue = $context->createQueue('test');
        } elseif ($context instanceof SqsContext) {
            $this->queue = $context->createQueue('test');
        } else {
            throw new RuntimeException('nope');
        }
        $context->declareQueue($this->queue);

Complete example: https://phpstan.org/r/f0317936-b04f-4c77-ab21-5f4997ba4479

Not sure if it's possible to fix this kind of edge case, the offending classes are in an external library so don't have much control over the Context/Queue just how we are implementing it. I can live with the Success variant, I just prefer the Fail variant.

Expected output

Fail class should pass but fails with:

Parameter #1 $name of method SnsQsContext::declareQueue() expects SnsQsQueue, SnsQsQueue|SqsQueue given.

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

I love how much stuff PHPStan does pick up for me, and it's been a great help to ensuring our code quality and consistency remains high.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions