Skip to content

PHPDoc Array shape with nullable key not recognized correctly #10317

@ThomasLandauer

Description

@ThomasLandauer

Bug report

With a setup like this:

/**
 * @param array{'foo': string, 'bar'?: string} $array
 */
private function myfunction(array $array): int
{
    $string = $array['bar'] ?? $array['foo'];
    return strlen($string);
}

I'm getting:

Parameter #1 $string of function strlen expects string, mixed given.

But from my understanding, $string will always be a string, cause both array keys (foo and bar) are string (if present at all).

This only happens when checkImplicitMixed is enabled; therefore I can't reproduce it in the playground.

Code snippet that reproduces the problem

No response

Expected output

No error message.

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

Sure! :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions