Skip to content

PHPStan fails to detect type error when passing possibly null value to parameter with other type #7082

@theodorejb

Description

@theodorejb

Bug report

The following code produces a fatal error at runtime:

$nullable = $_GET['x'] ?? null;
echo takesStr($nullable); // Fatal error:  Uncaught TypeError: Argument 1 passed to takesStr() must be of the type string, null given

function takesStr(string $val): string {
    return $val . 'foo';
}

However, PHPStan fails to detect the error, even at level 9 with strict rules: https://phpstan.org/r/b37b1718-1336-47ea-a42b-dd405f34f56d

Psalm detects the error as expected: https://psalm.dev/r/f6dc4527da

Expected output

Parameter #1 $val of function takesStr expects string, mixed|null given.

Actual output

No errors!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions