Skip to content

Overwriting foreach iterable variable causes wrong results #8924

@jlherren

Description

@jlherren

Bug report

When foreach's iterable variable is overwritten in the loop body, it no longer understands the element type.

Possibly related to #4907. Sample code:

/**
 * @param list<int> $array
 */
function foo(array $array): void {
	foreach ($array as $element) {
		PHPStan\dumpType($element);
		$array = null;
	}
}

https://phpstan.org/r/28c10452-0fd4-49bf-9f5e-e292cd31b586

Actual output

7: Dumped type: *ERROR*

Expected output

$element is int.

The loop correctly loops over all elements of $array. (However, note that it doesn't do so when using &$element.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions