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

Allow foreach loops which exit to act like array_filter #649

Closed
muglug opened this issue Apr 6, 2018 · 2 comments
Closed

Allow foreach loops which exit to act like array_filter #649

muglug opened this issue Apr 6, 2018 · 2 comments

Comments

@muglug
Copy link
Collaborator

muglug commented Apr 6, 2018

class A {}
class B extends A {}

/**
 * @param A[] $arr
 * @return B[]
 */
function filterAList(array $arr) : array {
  foreach ($arr as $a) {
    if (!$a instanceof B) {
      throw new \Exception("bad");
    }
  }
  return $arr;
}

Expected: no error
Actual: MoreSpecificReturnType issue: https://getpsalm.org/r/bcf8ce6c1d

This is fine:

/**
 * @param A[] $arr
 * @return B[]
 */
function filterAList(array $arr) : array {
  return array_filter(
    $arr,
    function(A $a) : bool {
      return $a instanceof B;
    }
  );
}
@muglug
Copy link
Collaborator Author

muglug commented Nov 22, 2018

I've decided not to fix this - it's fairly straightforward to code around it, and there are more common problems still to fix.

@muglug muglug closed this as completed Nov 22, 2018
@muglug
Copy link
Collaborator Author

muglug commented Nov 22, 2018

For reference, neither Hack nor Typescript nor Flow nor PHPStan understands this type of filtering, so I don't think it's really necessary for Psalm to, either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant