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)
Eliminating types after conditions is similar to #62. I agree that
$adminshould be mixed in theelseifbranch 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
$xbefore the for loop?