Add precise return types for RedisArray methods in function signature map#5743
Merged
VincentLanglet merged 2 commits intoMay 24, 2026
Merged
Conversation
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
Redis method are using __benevolent so I think we can do the same here
Collaborator
Author
|
Done. I wrapped all |
VincentLanglet
approved these changes
May 23, 2026
…re map - Add `RedisArray::keys` with return type `array<string, list<string>>|false` instead of the imprecise `bool|array` from phpstorm stubs - Add `RedisArray::info` with return type `array<string, array<string, mixed>>|false` - Add `RedisArray::mget` with return type `list<mixed>|false` - Add `RedisArray::scan` with return type `list<string>|false` - Add `RedisArray::hscan` with return type `array<string, string>|false` - Add `RedisArray::sscan` with return type `list<string>|false` - Add `RedisArray::zscan` with return type `array<string, float>|false` - Add `RedisArray` to stubs/Redis.stub with `@phpstan-all-methods-impure` for consistency with `Redis` and `RedisCluster`
Consistent with Redis method signatures, wrap RedisArray return types in __benevolent<> so users are not forced to check for false on every call. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
0b7b978 to
de90589
Compare
staabm
approved these changes
May 24, 2026
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
RedisArraymethods likekeys()had imprecise return types from the phpstorm stubs (bool|array), which includestrueas a possible return value (wrong — these methods returnfalseon failure, nottrue). The untypedarrayalso prevented proper type narrowing of keys and values.Changes
RedisArray::keystoresources/functionMap.phpwith return typearray<string, list<string>>|false— per phpredis docs,KEYS()on a RedisArray executes on each node and returns an associative array indexed by host nameRedisArray::infowith return typearray<string, array<string, mixed>>|false— similarly runs on each node and returns per-host resultsRedisArray::mgetwith return typelist<mixed>|false— distributes keys across nodes and merges resultsRedisArray::scanwith return typelist<string>|false— runs on a specific node (has$nodeparameter)RedisArray::hscanwith return typearray<string, string>|false— key-routed, matchesRedis::hscansemanticsRedisArray::sscanwith return typelist<string>|false— key-routed, matchesRedis::sscansemanticsRedisArray::zscanwith return typearray<string, float>|false— key-routed, matchesRedis::zscansemanticsRedisArraytostubs/Redis.stubwith@phpstan-all-methods-impurefor consistency withRedisandRedisClusterAnalogous cases probed but not changed
flushall,flushdb,save,select,setOption,getOption,ping) also havebool|arrayreturn types, but the exact array structure for per-host results is not well-documented, so these were left unchangedRoot cause
The phpstorm stubs for
RedisArrayuse the imprecisebool|arrayreturn type for most methods. PHPStan's function signature map (resources/functionMap.php) did not override these with more precise types, causing false positives when users checked forfalse(strict comparison againstboolwhich includestrue) and when iterating the returned arrays (untypedarraymeantmixedkeys and values).Test
tests/PHPStan/Analyser/nsrt/bug-9748.phpwith type assertions for all seven fixed methods:keys(): assertsarray<string, list<string>>|false, verifies narrowing after=== falsecheck, and verifies correct key/value types in nested foreachinfo(): assertsarray<string, array<string, mixed>>|falsemget(): assertslist<mixed>|falsescan(): assertslist<string>|falsehscan(): assertsarray<string, string>|falsesscan(): assertslist<string>|falsezscan(): assertsarray<string, float>|falseFixes phpstan/phpstan#9748