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

Incorrect assumption on non-empty array #5316

Closed
kubawerlos opened this issue Jul 16, 2021 · 2 comments
Closed

Incorrect assumption on non-empty array #5316

kubawerlos opened this issue Jul 16, 2021 · 2 comments
Labels
Milestone

Comments

@kubawerlos
Copy link
Contributor

Bug report

When iterating on 2 (or more) dimensional array the error: Comparison operation ">" between int<1, max> and 0 is always true. is incorrectly returned.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

$map = [
	1 => 'foo',
	2 => 'foo',
	3 => 'bar',
];
$names = ['foo', 'bar', 'baz'];
$array = ['foo' => [], 'bar' => [], 'baz' => []];

foreach ($map as $value => $name) {
	$array[$name][] = $value;
}


foreach ($array as $name => $elements) {
	echo sprintf('"%s" -> "%d".' . PHP_EOL, $name, count($elements) > 0);
}

(https://phpstan.org/r/8787a548-d915-4658-832f-dc3e7575884e)

Expected output

No error, as it is false positive: https://3v4l.org/6eAMe

Real life case can be found here: https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/5805/files#diff-a809d26a7ee19fceeb074078036812dc5c022e0a52d7ef0cc27d13cd161b33dfR569 - when you remove the condition that is claimed to be always true some tests start to fail

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jul 16, 2021
@phpstan-bot
Copy link
Contributor

@kubawerlos After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-17: Comparison operation ">" between int<1, max> and 0 is always true.
+No errors

@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 Feb 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants