Skip to content

false possibly-null-argument #1575

@UweOhse

Description

@UweOhse

Description

Mago reports a false positive here:

/** @return array<string,int> */
function fassoc(): array
{
    return [
        'object_id' => mt_rand(1, max: 100_000),
    ];
}

function object_url(int $oid): string
{
    return (string) $oid;
}

/** @return array<string> */
function test(int $mon, int $dom, bool $cheat): array
{
    $list = [];
    while ($row = fassoc()) {
        $list[] = $row['object_id'];
    }
    $out = [];
    for ($i = 1; $i < 25; $i++) {
        if (!$cheat && 12 === $mon && $i > $dom) {
            continue;
        }
        if (!array_key_exists($i, $list)) {
            continue;
        }
        // $list: `array{}|non-empty-list<int>`
        // $list[$i-1]: `int|null`

        $oid = $list[$i - 1];
        // possibly-null-argument: Argument #1 of function `object_url` is possibly `null`, but parameter type `int` does not accept it.
        $out[] = object_url($oid);
    }
    return $out;
}

This is loop related, too.

Playground Link

https://mago.carthage.software/playground#019d5c8d-8e27-5497-3dcf-c26975f4b7f8

Reproduction

No response

Environment

mago current as of this morning.

Metadata

Metadata

Assignees

Labels

c-analyzerPHP static analyzer and type error detectiont-bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions