Skip to content

Add precise return types for RedisArray methods in function signature map#5743

Merged
VincentLanglet merged 2 commits into
phpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-81hon2j
May 24, 2026
Merged

Add precise return types for RedisArray methods in function signature map#5743
VincentLanglet merged 2 commits into
phpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-81hon2j

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

RedisArray methods like keys() had imprecise return types from the phpstorm stubs (bool|array), which includes true as a possible return value (wrong — these methods return false on failure, not true). The untyped array also prevented proper type narrowing of keys and values.

Changes

  • Added RedisArray::keys to resources/functionMap.php with return type array<string, list<string>>|false — per phpredis docs, KEYS() on a RedisArray executes on each node and returns an associative array indexed by host name
  • Added RedisArray::info with return type array<string, array<string, mixed>>|false — similarly runs on each node and returns per-host results
  • Added RedisArray::mget with return type list<mixed>|false — distributes keys across nodes and merges results
  • Added RedisArray::scan with return type list<string>|false — runs on a specific node (has $node parameter)
  • Added RedisArray::hscan with return type array<string, string>|false — key-routed, matches Redis::hscan semantics
  • Added RedisArray::sscan with return type list<string>|false — key-routed, matches Redis::sscan semantics
  • Added RedisArray::zscan with return type array<string, float>|false — key-routed, matches Redis::zscan semantics
  • Added RedisArray to stubs/Redis.stub with @phpstan-all-methods-impure for consistency with Redis and RedisCluster

Analogous cases probed but not changed

  • Administrative per-host commands (flushall, flushdb, save, select, setOption, getOption, ping) also have bool|array return types, but the exact array structure for per-host results is not well-documented, so these were left unchanged

Root cause

The phpstorm stubs for RedisArray use the imprecise bool|array return 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 for false (strict comparison against bool which includes true) and when iterating the returned arrays (untyped array meant mixed keys and values).

Test

  • Added tests/PHPStan/Analyser/nsrt/bug-9748.php with type assertions for all seven fixed methods:
    • keys(): asserts array<string, list<string>>|false, verifies narrowing after === false check, and verifies correct key/value types in nested foreach
    • info(): asserts array<string, array<string, mixed>>|false
    • mget(): asserts list<mixed>|false
    • scan(): asserts list<string>|false
    • hscan(): asserts array<string, string>|false
    • sscan(): asserts list<string>|false
    • zscan(): asserts array<string, float>|false

Fixes phpstan/phpstan#9748

Copy link
Copy Markdown
Contributor

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redis method are using __benevolent so I think we can do the same here

@phpstan-bot
Copy link
Copy Markdown
Collaborator Author

Done. I wrapped all RedisArray method return types in __benevolent<...> to match the convention used by Redis methods, and updated the test assertions to expect the parenthesized format that BenevolentUnionType::describe() produces. Tests and static analysis all pass.

@VincentLanglet VincentLanglet self-assigned this May 23, 2026
@VincentLanglet VincentLanglet requested a review from staabm May 23, 2026 23:25
VincentLanglet and others added 2 commits May 24, 2026 19:07
…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>
@staabm staabm force-pushed the create-pull-request/patch-81hon2j branch from 0b7b978 to de90589 Compare May 24, 2026 17:07
@VincentLanglet VincentLanglet merged commit 6f2f01a into phpstan:2.1.x May 24, 2026
653 of 659 checks passed
@VincentLanglet VincentLanglet deleted the create-pull-request/patch-81hon2j branch May 24, 2026 17:20
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.

3 participants