vimeo/psalm

Psalm does not infer type in array_reduce()

Open

#6370 aperta il 27 ago 2021

Vedi su GitHub
 (6 commenti) (1 reazione) (0 assegnatari)PHP (668 fork)batch import
Help wantedenhancement

Metriche repository

Star
 (5369 star)
Metriche merge PR
 (Merge medio 3g 12h) (5 PR mergiate in 30 g)

Descrizione

Given the following code:

/**
 * @param array<array{tags: string}> $a
 */
function test(array $a): string
{
    return array_reduce($a, function (string $carry, array $value) {
        return $carry . $value['tags'];
    }, '');
}

Psalm is unable to infer a type for $value in the callback function given to array_reduce():

https://psalm.dev/r/b9e02bcaaa

In this example, it should infer the type as array{tags: string}.

Can this be fixed?

Guida contributor