Skip to content

Multiple optional array-key doesn't work in the array-shape #3248

@koriym

Description

@koriym

Bug report

Code snippet that reproduces the problem

class Foo
{
    /**
     * @return array{foo?: string, bar?: string}
     */
    private function docBlock(int $a) : array
    {
        $docs = [];
        if ($a < 10) {
            $docs['foo'] = 's';
        }
        if ($a < 100) {
            $docs['bar'] = 'm';
        }
        return $docs;
    }
}

https://phpstan.org/r/f6bd1c14-ad32-4263-8dd2-6f20e6344a86

This error was produced as follows.

Method Foo::docBlock() should return array()|array('foo' => string, 'bar' => string) but returns array(?'foo' => 's', ?'bar' => 'm').

It works fine with a single array-key.
https://phpstan.org/r/5255b43c-283b-4085-bea7-862f9484ae57

Expected output

No error.

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