Correct the value type of parse_shell_arrays()'s $assoc_args - #6393
Conversation
The parameter is annotated array<string, string>, but associative arguments are not all strings: a flag arrives as boolean true, and a key that has already been parsed arrives as an array. The function has never required strings. It only looks at the keys named in $array_arguments, and only acts when is_json() returns true - which begins by rejecting anything that is not a string. Every other value is returned exactly as it came in. The narrow annotation is contagious. A command that documents its arguments accurately as array<string, mixed> cannot call this function without PHPStan reporting argument.type, while a command whose parameters carry no type at all passes, because implicit mixed is not checked at level 9. In other words the callers doing the right thing are the ones penalised, and the workaround is to either weaken the annotation or wrap the call. Widening the parameter is safe for existing callers, since array<string, string> satisfies array<string, mixed>. Four data provider cases cover the behaviour that the annotation had been hiding. Refs wp-cli/entity-command#636 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014SSZzqMJRDTiLiDxQEPYcL
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan includes up to 8 reviews per rolling hour; 7 remain after this review. 📝 WalkthroughWalkthrough
Changesparse_shell_arrays behavior
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to This localized annotation correction is merge-ready after normal checks and review; no actionable merge-blocking risk remains. Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
wp-cli/wp-cli#6393 corrected parse_shell_arrays()'s $assoc_args parameter to array<string, mixed>, which is what it always accepted. The local wrapper existed only to work around the previous annotation, so revert to the plain call and let all four call sites in this repo look the same again. This does not need to wait for a wp-cli release: composer.json sets minimum-stability to dev and requires ^3.0, which resolves to dev-main. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014SSZzqMJRDTiLiDxQEPYcL
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Utils\parse_shell_arrays()is annotated@param array<string, string> $assoc_args, but associative arguments are not all strings — a flag arrives as booleantrue, and a key that has already been parsed arrives as an array.The function has never required strings. It looks only at the keys named in
$array_arguments, and acts only whenis_json()returns true — which begins by rejecting anything that is not a string. Every other value is returned exactly as it came in.Why it is worth fixing rather than working around
The narrow annotation is contagious, and it penalises the callers doing the right thing.
A command that documents its arguments accurately as
array<string, mixed>cannot call this function without PHPStan reportingargument.typeat level 9. A command whose$assoc_argsparameter carries no type at all passes silently, because implicitmixedis not checked at that level andmissingType.parameteris generally ignored.That is exactly what happens in
entity-commandtoday:Post_CommandandUser_Commandcall it without complaint from untyped parameters, whileComment_Command— the one file that annotates its arguments correctly — fails. The available fixes there are to weaken an accurate annotation or to wrap the call, and neither is a good trade for a docblock that is simply wrong.Safety
Widening a parameter type cannot break existing callers: everything that satisfied
array<string, string>still satisfiesarray<string, mixed>. The return type is alreadyarray<string, mixed>.The one thing worth checking was whether the
@phpstan-ignore cast.uselessinside the function would become an unmatched ignore once the value type is no longer known to be a string. It does not —is_json()carries@phpstan-assert-if-true =non-empty-string, so the value is still narrowed to a string inside the branch where the cast happens.Testing
Four cases added to the existing
dataParseShellArrayprovider, covering what the annotation had been hiding: a boolean flag, an already-decoded array, a non-JSON string, and a mixed set where one key decodes and another passes through.PHPStan over the full analysed paths is unchanged at 1552 errors before and after in my environment (that absolute number is inflated — this environment cannot install the PHPStan extensions CI uses — but the before/after comparison is like-for-like, and the only differences are messages that embed line numbers, shifted by the five lines added to the docblock). PHPCS is clean on both changed files.
I also verified the effect downstream: with this branch in place,
Comment_Command's plainUtils\parse_shell_arrays( $assoc_args, [ 'comment_meta' ] )call reports no PHPStan errors, so wp-cli/entity-command#636 can drop its local workaround once this lands.🤖 Generated with Claude Code
https://claude.ai/code/session_014SSZzqMJRDTiLiDxQEPYcL
Generated by Claude Code
Summary by CodeRabbit
Documentation
Bug Fixes
Tests