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

foreach already checked for zero length should not give $i might not be defined #1505

Closed
asgrim opened this issue Oct 12, 2018 · 6 comments
Closed
Labels

Comments

@asgrim
Copy link

asgrim commented Oct 12, 2018

Summary of a problem or a feature request

When performing a foreach on an array, you can use the dangling reference of the loop ($things as $thing, you can use $thing still after the loop), but of course if the count of the array is zero, then $thing is rightly pointed out to be possibly not defined (Variable $thing might not be defined.). However, if we check the count($things) is non-zero before the iteration, phpstan should notice this and be satisfied that $thing would always be set.

Code snippet that reproduces the problem

See results: https://phpstan.org/r/48ec08fa293fa3ac9857d2c975188a13

<?php declare(strict_types = 1);

class Thing { public function foo() : void {} }

/** @var Thing[] $things */
$things = [new Thing(), new Thing()];

// This count check should tell phpstan that $thing exists down below
if (count($things) === 0) {
	// it's return from a method in our case, but die for example sake
	die();
}

foreach ($things as $thing) {
}

/** @var Thing $thing */
$thing->foo();

Actual output

 ------ --------------------------------------- 
  Line   analyzed.php                           
 ------ --------------------------------------- 
  18     Variable $thing might not be defined.  
 ------ --------------------------------------- 

Expected output

I expect no errors, the detected issue is a false positive.

@ondrejmirtes
Copy link
Member

Yeah, unfortunately PHPStan doesn't track non-empty arrays yet. See also #1073.

@asgrim
Copy link
Author

asgrim commented Oct 13, 2018

Aye I guess this is a duplicate, but the @var annotation doesn't seem to work in this case either :/

@ondrejmirtes
Copy link
Member

I usually use this pattern: https://phpstan.org/r/f5ea92315b90dfbe8224c0deeed49a78

@adaamz
Copy link
Contributor

adaamz commented Oct 14, 2018

Or check it after loop
https://phpstan.org/r/c5838a9a32825418d2c3d38e082614f3

@asgrim
Copy link
Author

asgrim commented Oct 15, 2018

Thanks folks! I'm happy for this to be a duplicate of #1073 then (assuming they would be solved using the same approach). Meanwhile, workarounds work 👍

@ondrejmirtes ondrejmirtes added this to the Non-empty arrays milestone Oct 23, 2018
@ondrejmirtes
Copy link
Member

@lock lock bot locked as resolved and limited conversation to collaborators Dec 19, 2019
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