Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive with in_array when being used in loops #7684

Closed
DaDeather opened this issue Jul 25, 2022 · 3 comments · Fixed by phpstan/phpstan-src#1545
Closed

False positive with in_array when being used in loops #7684

DaDeather opened this issue Jul 25, 2022 · 3 comments · Fixed by phpstan/phpstan-src#1545
Labels
Milestone

Comments

@DaDeather
Copy link

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.

@ondrejmirtes
Copy link
Member

First bad commit: phpstan/phpstan-src@2cd83b2 /cc @herndlm :)

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jul 25, 2022
@herndlm
Copy link
Contributor

herndlm commented Jul 25, 2022

Oh no, I knew it 😅 at least that should be an easy fix. I'll open it a bit later

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants