phpstan: false positive on loop with several types for the same variable

Among other things

  226    Cannot call method hasChild() on false.                                  
  227    Cannot call method getChild() on false. 

is returned for this piece of code:

public function getAdminByAdminCode($adminCode)
{
    $codes = explode('|', $adminCode);
    $admin = false;
    foreach ($codes as $code) {
        if ($admin == false) {
            $admin = $this->getInstance($code);
        } elseif ($admin->hasChild($code)) {
            $admin = $admin->getChild($code);
        }
    }
    return $admin;
}

The false case is already handled.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Eliminating types after conditions is similar to #62. I agree that $admin should be mixed in the elseif branch here. I will solve it in one sweep when implementing this for union types mentioned in the linked issue. Thanks! 😃

@samsonasik This isn’t really an intuitive piece of code 😃 Wouldn’t you like more to assign $x before the for loop?