Skip to content

Suggest unsetting a variable by reference after the loop #11120

@b1rdex

Description

@b1rdex

Feature request

https://phpstan.org/r/8b50b60a-9c0c-4627-a6c6-103bbe6802a9

	/** @param list<array<string, mixed>> $input */
	public function sayHello(array $input): void
	{
		// improvement request: phpstan should suggest adding `unset($item)` after the foreach statement
		// only for loops with variable passed by reference
		foreach ($input as &$item) {}

		// this is the problem code: it changes last array element
		// this code is almost certanly reuses variable by mistake
		// and overwrites the last array element by mistake
		$item = 'foo';

		// bug: phpstan doesn't detect the $input type is changed now
		\PHPStan\dumpType($input);
	}

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

💯 the best utility!

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