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

array_filter w/ empty callback may return empty array #4043

Closed
b1rdex opened this issue Nov 2, 2020 · 4 comments
Closed

array_filter w/ empty callback may return empty array #4043

b1rdex opened this issue Nov 2, 2020 · 4 comments

Comments

@b1rdex
Copy link
Contributor

b1rdex commented Nov 2, 2020

The playground is broken, so the raw code:

$someVar = 'now';
$uppedDate = new DateTime($someVar);
$diff = $uppedDate->diff(new DateTime());

$mdiff = (int)$diff->format('%m');
$ddiff = (int)$diff->format('%d');
$content = array_filter([$mdiff, $ddiff]);

// if condition is always true
if ($content) {
    die();
}

// the error is gone
if (\count($content) > 0) {
	die();
}

$content = array_filter([$mdiff, $ddiff], fn(): bool => false);
// the error is gone
if ($content) {
	die();
}
  1. array_filter w/ empty callback might return an empty array
  2. why count()ing the result removes the error?
@ondrejmirtes
Copy link
Member

Please reproduce it now.

@b1rdex
Copy link
Contributor Author

b1rdex commented Nov 2, 2020

The original code: https://phpstan.org/r/af2a9cfe-e058-47b5-a63f-b6d0eeb90709
Surprisingly, count() doesn't fix it.

Here is the more complicated (but closer to the original code w/ the issue) example: https://phpstan.org/r/4b5a185e-ec7a-4b38-a9ce-1a6e8be65a40
count() fixes it somehow

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@d22abbe

@github-actions
Copy link

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

No branches or pull requests

2 participants