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

Variable undefined when defined in prior if block #1306

Closed
dantleech opened this issue Jul 28, 2018 · 8 comments
Closed

Variable undefined when defined in prior if block #1306

dantleech opened this issue Jul 28, 2018 · 8 comments

Comments

@dantleech
Copy link

Summary of a problem or a feature request

In the below example the two conditionals both evaluate to true based on the same variable. If the variable is true the code within each block should be evaluated and $foo should therefore be defined in the second block, but Phpstan reports that it might be undefined

 ------ ------------------------------------- 
  Line   analyzed.php                         
 ------ ------------------------------------- 
  4      ... 
  10     ...
  11     Variable $foo might not be defined.  
 ------ ------------------------------------- 

Code snippet that reproduces the problem

            <?php

            $baz = true; // ignore: could be false|true at runtime
            if ($baz) {
                $foo = 'asd';
            }

            if ($baz) {
                $bar = $foo; 
            }

https://phpstan.org/r/112e67a0d4bd81232da6a687b6605d42

@dantleech
Copy link
Author

dantleech commented Jul 28, 2018

There is a potentially related issue here: https://phpstan.org/r/07a9e0d5cc770c25203bcc3bc3b9c9b1

<?php

function bar($foo = null) {
  if ($foo !== null) {
	$someBoolean = false;
  }

  if ($foo !== null && $someBoolean === false) {
  }
}
 ------ --------------------------------------------- 
  Line   analyzed.php                                 
 ------ --------------------------------------------- 
  8      Variable $someBoolean might not be defined.  
 ------ ---------------------------------------------

In the above case $someBoolean will not be evaluated if $foo is null, so it should be OK.

@ondrejmirtes
Copy link
Member

Hi, this is hard to solve. There are other issues like this: https://github.com/phpstan/phpstan/milestone/12 I might tackle this one day, but it's not a priority.

Meanwhile, you should always use isset in the second condition.

@dantleech
Copy link
Author

ok thanks, no worries.

Meanwhile, you should always use isset in the second condition.

I think the above is valid (even if horrible - not my code 🙄 ) $someBoolean will not be evaluated if the left side of the && is false.

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jul 29, 2018 via email

@dhaarbrink
Copy link

+1 from me, I have also just run into this problem. Following thread.

@morrisonlevi
Copy link

This is common for things that are only executed in debug mode; see https://phpstan.org/r/1df6d7d1-6a34-499d-93d1-eab18dcef390. Normally there would be other things between the two ifs, of course.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@54284c8

ondrejmirtes added a commit to phpstan/phpstan-src that referenced this issue Dec 17, 2020
ondrejmirtes added a commit that referenced this issue Dec 17, 2020
@github-actions
Copy link

github-actions bot commented May 4, 2021

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 May 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants