vimeo/psalm

Psalm does not infer type in array_reduce()

Open

#6,370 opened on 2021年8月27日

GitHub で見る
 (6 comments) (1 reaction) (0 assignees)PHP (668 forks)batch import
Help wantedenhancement

Repository metrics

Stars
 (5,369 stars)
PR merge metrics
 (平均マージ 3d 12h) (30d で 5 merged PRs)

説明

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?

コントリビューターガイド