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: If condition is always false. #2420

Closed
iluuu1994 opened this issue Sep 11, 2019 · 8 comments
Closed

False positive: If condition is always false. #2420

iluuu1994 opened this issue Sep 11, 2019 · 8 comments
Labels
Milestone

Comments

@iluuu1994
Copy link
Contributor

https://phpstan.org/r/eae81481-04ee-44d2-8ee8-e573625aa034

class HelloWorld
{
	const CONFIG = [
		0 => ['foo' => false],
		1 => ['foo' => false],
	];
	
	public function sayHello(int $key): void
	{
		$config = self::CONFIG[$key]['foo'] ?? true;
		
		if ($config) {
			print 'Foo';
		}
	}
}

If condition is always false.

This only happens when the CONFIG is multidimensional. This here works:

https://phpstan.org/r/0d649821-d7b9-446b-99a5-acd0e3a66002

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Sep 11, 2019
@julienfalque
Copy link

Maybe related:

<?php

$foo = true;

if (mt_rand(0, 1) === 1) {
    $foo = false;

    if (mt_rand(0, 1) === 1) {
        $foo = true;
    }
}

if ($foo) {
}

PhpStan reports if condition is always true: https://phpstan.org/r/d61749a7-76e5-480a-8057-cd8a1c258523

@phpstan-bot
Copy link
Contributor

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

@@ @@
-13: If condition is always true.
+No errors

@ondrejmirtes
Copy link
Member

@julienfalque Your issue got fixed by phpstan/phpstan-src@d4edc59 but was unrelated to the OP issue.

@julienfalque
Copy link

Thank you @ondrejmirtes.

@staabm
Copy link
Contributor

staabm commented Nov 1, 2021

@ondrejmirtes this bug is listed in the phpstan 1.0 release notes as beeing fixed. it seems thats a typo/mistake?

@ondrejmirtes
Copy link
Member

@staabm Not really, it fixed #2420 (comment). (This is why I insist on separate issues for unrelated problems, but I didn't do that a lot 1,5 year ago).

@phpstan-bot
Copy link
Contributor

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

@@ @@
-14: If condition is always false.
+No errors

@github-actions
Copy link

github-actions bot commented Mar 4, 2022

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 Mar 4, 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

5 participants