Skip to content

Dependant types - parsing logic (can't think of a better subject - please change) #4695

@pauljherring

Description

@pauljherring

Bug report

As requested from #4694

Complicated logic not being recognised, leading to erroneous errors.

Code snippet that reproduces the problem

https://phpstan.org/r/3d974ed6-7d4b-4f91-8dcf-7187222d3004

<?php declare(strict_types = 1);
/**
* @return no-return
*/
function no_return(){
    echo "Dying\n";
    die(1);
}

/**
* @param int|null $u
* @param int|null $a
* @return array<int, int>
*/
function foo($u=null, $a=null){
    if (is_null($u) && is_null($a)){
        no_return();
	return [0,0]; // line 18
    }
    if ($u){
        $a = $u;
    }else if ($a){
        $u = $a;
    }
    return [$u, $a]; // line 25
}

print_r(foo(1)); // first only
print_r(foo(null,2)); // second only
print_r(foo(1,2)); // both
print_r(foo()); // none - will die() and not return

Actual output

18 | Unreachable statement - code above always terminates.
-- | --
25 | Function foo() should return array<int, int> but returns array<int, int|null>.

Expected output

18 | Unreachable statement - code above always terminates.

Line 18 added to check that no-return being honoured.

However $a (the int|null) will never be null if it gets that far through the function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions