Skip to content

Correct the value type of parse_shell_arrays()'s $assoc_args - #6393

Merged
swissspidy merged 1 commit into
mainfrom
claude/wp-cli-issue-5286-n50evd-parse-shell-arrays
Aug 16, 2026
Merged

Correct the value type of parse_shell_arrays()'s $assoc_args#6393
swissspidy merged 1 commit into
mainfrom
claude/wp-cli-issue-5286-n50evd-parse-shell-arrays

Conversation

@swissspidy

@swissspidy swissspidy commented Aug 16, 2026

Copy link
Copy Markdown
Member

Utils\parse_shell_arrays() is annotated @param array<string, string> $assoc_args, 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 looks only at the keys named in $array_arguments, and acts only 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.

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 reporting argument.type at level 9. A command whose $assoc_args parameter carries no type at all passes silently, because implicit mixed is not checked at that level and missingType.parameter is generally ignored.

That is exactly what happens in entity-command today: Post_Command and User_Command call it without complaint from untyped parameters, while Comment_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 satisfies array<string, mixed>. The return type is already array<string, mixed>.

The one thing worth checking was whether the @phpstan-ignore cast.useless inside 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 dataParseShellArray provider, 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.

$ phpunit --filter testParseShellArray tests/UtilsTest.php
OK (7 tests, 7 assertions)

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 plain Utils\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

    • Clarified how mixed-value associative arguments are handled when parsing shell arrays.
  • Bug Fixes

    • Ensured only configured values containing valid JSON strings are converted to arrays.
    • Preserved booleans, existing arrays, invalid JSON, and other non-string values unchanged.
  • Tests

    • Added coverage for mixed-value inputs and combinations of valid and non-JSON values.

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
Copilot AI lite review requested due to automatic review settings August 16, 2026 14:29
@swissspidy
swissspidy requested a review from a team as a code owner August 16, 2026 14:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Aug 16, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: bf2b2b0c-e7a8-4c96-8654-530883351657

📥 Commits

Reviewing files that changed from the base of the PR and between 12b8009 and 2e40a3a.

📒 Files selected for processing (2)
  • php/utils.php
  • tests/UtilsTest.php

Included review availability: Your plan includes up to 8 reviews per rolling hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

parse_shell_arrays() documentation now describes mixed associative values. Tests verify that only valid JSON strings for configured keys are decoded, while booleans, arrays, invalid JSON, and other values remain unchanged.

Changes

parse_shell_arrays behavior

Layer / File(s) Summary
Mixed-value parsing contract and validation
php/utils.php, tests/UtilsTest.php
The documentation permits mixed associative values. Tests cover valid JSON strings, booleans, arrays, invalid JSON, and mixed arguments.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 2e40a

This localized annotation correction is merge-ready after normal checks and review; no actionable merge-blocking risk remains.

Suggested reviewers: copilot

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: correcting the value type annotation for parse_shell_arrays()'s $assoc_args parameter.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/wp-cli-issue-5286-n50evd-parse-shell-arrays

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the bug label Aug 16, 2026
@swissspidy swissspidy added this to the 3.0.0 milestone Aug 16, 2026
@swissspidy
swissspidy merged commit d25eb2c into main Aug 16, 2026
64 checks passed
@swissspidy
swissspidy deleted the claude/wp-cli-issue-5286-n50evd-parse-shell-arrays branch August 16, 2026 15:21
swissspidy pushed a commit to wp-cli/entity-command that referenced this pull request Aug 16, 2026
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

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants