Skip to content

False positives for unreachable detection of nested optional elements in an array #4903

@zonuexe

Description

@zonuexe

Bug report

The following code is detected in the latest PHPStan as “Unreachable statement - code above always terminates.”

Maybe MutatingScope::resolveType() needs to recursively inspect the parent element.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

/**
 * @param array{a?:array{b:bool}} $options
 */
function f(array $options): bool
{
    if (isset($options['a']['b'])) {
        return true;
    }
	
    return false;
}

var_dump(f([])); // returns false

https://phpstan.org/r/c877f393-c5d5-4b81-9655-709ba6b6a85b

Expected output

The last statement is reachable at run time, so no message should be printed.

https://3v4l.org/fjfml

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions