Fix phpstan/phpstan#14223: Variable $ in empty() always exists and is not falsy.#5124
Merged
VincentLanglet merged 1 commit intophpstan:2.1.xfrom Mar 3, 2026
Conversation
… should not return non-empty-array - ArrayCountValuesDynamicReturnTypeExtension always added NonEmptyArrayType to the result, even when the input array could be empty - Now checks $inputType->isIterableAtLeastOnce() before marking the output as non-empty - Updated existing test assertion for returnsStringOrObjectArray() which returns a possibly-empty array - New regression test in tests/PHPStan/Analyser/nsrt/bug-14223.php
VincentLanglet
approved these changes
Mar 3, 2026
staabm
approved these changes
Mar 3, 2026
Contributor
|
@VincentLanglet please double check the issue-bot results on issue 14107 |
Contributor
Seems ok to me |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
array_count_values()on a possibly-empty input array was incorrectly returningnon-empty-array<..., int<1, max>>instead ofarray<..., int<1, max>>. This caused a false positiveempty.variableerror when usingempty()on the result, since PHPStan believed the value was always non-falsy.Changes
src/Type/Php/ArrayCountValuesDynamicReturnTypeExtension.phpto check$inputType->isIterableAtLeastOnce()->yes()before addingNonEmptyArrayTypeto the resulttests/PHPStan/Analyser/nsrt/array-count-values.phpfor thereturnsStringOrObjectArray()case which returns a possibly-empty arraytests/PHPStan/Analyser/nsrt/bug-14223.phpRoot cause
ArrayCountValuesDynamicReturnTypeExtensionunconditionally wrapped every output type in anIntersectionTypewithNonEmptyArrayType. This was incorrect because when the input array could be empty (e.g.,list<string>),array_count_values([])returns[]. The fix checks whether the input type is definitely non-empty (isIterableAtLeastOnce()->yes()) before adding theNonEmptyArrayTypeaccessory type.Test
The regression test
tests/PHPStan/Analyser/nsrt/bug-14223.phpverifies:array_count_values()onlist<string>(possibly empty) returnsarray<string, int<1, max>>(notnon-empty-array)array_count_values()onnon-empty-list<string>(definitely non-empty) still returnsnon-empty-array<string, int<1, max>>Fixes phpstan/phpstan#14223