Skip to content

Optional keys in object shapes do not work with null coalesce #10327

@leongersen

Description

@leongersen

Bug report

Optional keys on objects can be coalesced, but in PHPStan they result in the error type.

This works:

class Test {}

$a = new \stdClass();
$b = new Test();

var_dump($a->optional ?? null, $b->optional ?? null);

Accessing $b->optional without coalesce results in Warning: Undefined property.

Code snippet that reproduces the problem

https://phpstan.org/r/1f24e6ab-289c-45ad-861c-1f7455f55723

Expected output

Both types should be int|null

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

It does! I get excited every time a new PhpStan version is released!

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