Skip to content

parse_url false positive #3046

@iluuu1994

Description

@iluuu1994

https://phpstan.org/r/51d146e4-ee31-4e29-958d-a8c17c049da7

$urlParts = parse_url($url);
if ($urlParts === false) {
    throw new \InvalidArgumentException('Url is not valid');
}

if (isset($urlParts['host'])) {
    // This is not redundant
    if (isset($urlParts['port'])) {
        echo 'Host and port are defined';
    }
}

Offset 'port' on array('scheme' => string, 'host' => string, 'port' => int, 'user' => string, 'pass' => string, 'path' => string, 'query' => string, 'fragment' => string) in isset() always exists and is not nullable.

I think this is because the return type of parse_url is defined as array()|array(scheme: string, ...)|false which is not correct. The correct type would be array(scheme?: string, ...)|false but I'm not sure if PHPStan is capable of defining optional keys in array literals right now.

This only occurs with bleeding edge. Not sure why.

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