Skip to content

Avoid falsely specifying never types via count()#3898

Merged
ondrejmirtes merged 1 commit into
phpstan:2.1.xfrom
herndlm:count-avoid-never
Mar 22, 2025
Merged

Avoid falsely specifying never types via count()#3898
ondrejmirtes merged 1 commit into
phpstan:2.1.xfrom
herndlm:count-avoid-never

Conversation

@herndlm

@herndlm herndlm commented Mar 22, 2025

Copy link
Copy Markdown
Contributor

Refs #3709 (comment)

The issue was that in a falsey scope with count(array{stdClass}) < int<1, max> it wasn't able to further narrow down the array, and return it as sureNotType, leading to *NEVER* in the end. Hope I'm not inversing the logic too much here.. Then it would try to use never with an array operation, leading to *ERROR* which basically made it then behave like mixed afterwards.

I hate the complex if conditions. This could be somehow more simplified I think, but there are some special cases in the list-count.php test which deals basically with removing arrays from a union of arrays by narrowing via count in both truthy and falsey scopes and I ended up with at least 2 of those not working properly anymore. So this, apparently better, exclusion strategy it is..

Unfortunately "unfixes" phpstan/phpstan#1311, but https://phpstan.org/r/a1ce8144-60c8-43fc-bfac-fcb7086b3a25 shows it was never really properly fixed anyway.

@ondrejmirtes ondrejmirtes merged commit 69db4ae into phpstan:2.1.x Mar 22, 2025
@ondrejmirtes

Copy link
Copy Markdown
Member

Thank you!

@herndlm herndlm deleted the count-avoid-never branch March 23, 2025 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants