Fix phpstan/phpstan#14312: Parameter #2 of array_key_exists expects array, non-empty-array given#5237
Merged
staabm merged 4 commits intophpstan:2.1.xfrom Mar 17, 2026
Conversation
… not accepted as array - Fixed ArrayKeyExistsFunctionTypeSpecifyingExtension to include ArrayType in the narrowing type when the variable is not already an array type - When mixed is narrowed via array_key_exists with a non-constant key, the type now becomes array<mixed, mixed> & non-empty-array instead of standalone non-empty-array, which is properly accepted by the array parameter type - Added regression test in tests/PHPStan/Rules/Functions/data/bug-14312.php
staabm
reviewed
Mar 17, 2026
Contributor
staabm
left a comment
There was a problem hiding this comment.
check whether we have a similar problem in other *TypeSpecifyingExtension classes.
try to come up with a failling unit test. if you find a failling test add a fix.
…SpecifyingExtension Apply the same fix from ArrayKeyExistsFunctionTypeSpecifyingExtension: when the variable is not already an array type, include ArrayType(mixed, mixed) in the intersection to produce a proper array & non-empty-array type. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Collaborator
Author
|
Already retrieved — static analysis passed with no errors. Everything is green and pushed. |
VincentLanglet
approved these changes
Mar 17, 2026
Collaborator
Author
|
Already retrieved — all 11,584 tests passed. Everything is green, nothing more to do. |
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
When a
mixedvariable was narrowed tonon-empty-arrayviaarray_key_exists()with a non-constant key, a subsequentarray_key_exists()call on the same variable would report a false positive: "Parameter #2 $array of function array_key_exists expects array, non-empty-array given."Changes
src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.phpto includeArrayType(mixed, mixed)in the narrowing type when the variable is not already an array typetests/PHPStan/Rules/Functions/data/bug-14312.phpandtests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.phpRoot cause
The
ArrayKeyExistsFunctionTypeSpecifyingExtensionnarrowed non-constant key cases to a standaloneNonEmptyArrayTypeaccessory type. When intersected withmixed(the original type), the result was a standaloneNonEmptyArrayType— an incomplete type that lacked the underlyingArrayType. This standalone accessory type was not recognized as a validarraysubtype byArrayType::accepts(), causing the false positive.The fix conditionally includes
ArrayType(mixed, mixed)in the narrowing type only when the variable is not already an array (to avoid interfering with existing key type refinements like subtracted types). This produces a properarray<mixed, mixed> & non-empty-arrayintersection type that is correctly accepted byarrayparameters.Test
Added a rule test that verifies no error is reported when
array_key_exists()is called on a variable already narrowed tonon-empty-arrayby a priorarray_key_exists()call.Fixes phpstan/phpstan#14312