Skip to content

False positive with in_array when being used in loops #7684

@DaDeather

Description

@DaDeather

Bug report

There is an issue which came up with Version 1.8.1 which was not present in 1.8.0.

The code below is shown as if it contains issues but from my point of view it is more like a false positive since the iteration can and will change the values in $arr.

Instead of a const array we are retrieving objects from the database which can also have a collection of objects which is stored within the $arr variable and therefore can have duplicates which are filtered away in this structure.

Code snippet that reproduces the problem

On phpstan.org

<?php declare(strict_types = 1);

class SomeClass
{
	public function someFunction(int ...$someIntArray): void
	{
		$arr = [];
		foreach ($someIntArray as $val) {
			if (in_array($val, $arr, true) === true) {
				continue;
			}

			$arr[] = $val;
		}
	}
}

Expected output

Should not report an error, since the $arr variable is only empty in the first iteration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions