Skip to content

False positive: isset on nested associative arrays #2718

@Alban-io

Description

@Alban-io

Bug report

+--------------------------------------------------------------+
| Line | test.php                                              |
+--------------------------------------------------------------+
| 23   | Unreachable statement - code above always terminates. |
+--------------------------------------------------------------+

Code snippet that reproduces the problem

➡️ https://phpstan.org/r/053b9b77-618e-4af2-8105-c2a572945565

<?php declare(strict_types = 1);

$fun = function (): array {
    return [
        ['group_id' => 'a', 'user_id' => 'id1'],
        ['group_id' => 'a', 'user_id' => 'id2'],
        ['group_id' => 'a', 'user_id' => 'id3'],
        ['group_id' => 'b', 'user_id' => 'id4'],
        ['group_id' => 'b', 'user_id' => 'id5'],
        ['group_id' => 'b', 'user_id' => 'id6'],
    ];
};

$orders = $fun();

$result = [];
foreach ($orders as $order) {
    // no error trigger with this statement
    // if (isset($result[$order['group_id']])) {
    if (isset($result[$order['group_id']]['users'])) {
        $result[$order['group_id']]['users'][] = $order['user_id'];
        continue;
    }

    $result[$order['group_id']] = [
        'users' => [
            $order['user_id'],
        ],
    ];
}

Expected output

 [OK] No errors 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions