Skip to content

False positive paramOut.unusedType when using unset(...) inside of if condition. #13978

@RuggMatt

Description

@RuggMatt

Bug report

Since PHPstan 2.1.34, it declares that one of @param-out union type is never assigned, so it can be removed, but it is a possible return type. If I remove the type that's apparently never assigned, I get the opposite error that the @param-out type is too narrow.

 /**
 *
 * @param array{
 *   key1: int
 * } $item
 *
 * @param-out array{
 *   key1: int
 * }|array{
 *   key2: float
 * } $item
 *
 */
function example(array &$item): void
{
	if (!empty($item["key1"])) {
		$item['key2'] = 1.00;
		unset($item["key1"]);
	}
}

Error:

Function example() never assigns array{key1: int} to &$item so it can be removed from the @param-out type.

Code snippet that reproduces the problem

https://phpstan.org/r/12b15a19-d9c7-4dae-b7b7-029b2b8cc4cc

Expected output

If the if condition is false, then key1 key will still be set, and so the @param-out type is correct

https://3v4l.org/rhfM6#vnull

Did PHPStan help you today? Did it make you happy in any way?

Great work as always!

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