Skip to content

Commit 72daaa2

Browse files
committed
Narrow the comment_meta argument before parsing it
PHPStan reported two errors on the calls added in the previous commit: Parameter #1 $assoc_args of function WP_CLI\Utils\parse_shell_arrays expects array<string, string>, array<string, mixed> given. Utils\parse_shell_arrays() is annotated for string values throughout, while Comment_Command's methods correctly declare their arguments as array<string, mixed> - a flag arrives as true, not as a string. Rather than weaken that annotation, hand the function only the one key it needs to look at, and only when it holds a string. That is not a workaround for the type checker: is_json() rejects anything that is not a string, so a non-string value was never going to be parsed either way. Post_Command makes the same call without complaint only because its $assoc_args parameters carry no type at all, and missingType.parameter is in the ignore list. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014SSZzqMJRDTiLiDxQEPYcL
1 parent bb0e04b commit 72daaa2

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

‎src/Comment_Command.php‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ public function __construct() {
7575
$this->fetcher = new CommentFetcher();
7676
}
7777

78+
/**
79+
* Decodes the `--comment_meta` argument from its JSON representation.
80+
*
81+
* Utils\parse_shell_arrays() is typed for string values throughout, while
82+
* comment arguments are not, so narrow to the single key it needs to see.
83+
* It only ever acts on strings anyway, since is_json() rejects everything
84+
* else.
85+
*
86+
* @param array<string, mixed> $assoc_args Associative arguments.
87+
* @return array<string, mixed> Associative arguments, with comment_meta decoded.
88+
*/
89+
private static function parse_comment_meta( $assoc_args ) {
90+
if ( ! isset( $assoc_args['comment_meta'] ) || ! is_string( $assoc_args['comment_meta'] ) ) {
91+
return $assoc_args;
92+
}
93+
94+
$parsed = Utils\parse_shell_arrays(
95+
[ 'comment_meta' => $assoc_args['comment_meta'] ],
96+
[ 'comment_meta' ]
97+
);
98+
99+
$assoc_args['comment_meta'] = $parsed['comment_meta'];
100+
101+
return $assoc_args;
102+
}
103+
78104
/**
79105
* Creates a new comment.
80106
*
@@ -145,7 +171,7 @@ public function __construct() {
145171
* @param array<string, mixed> $assoc_args Associative arguments.
146172
*/
147173
public function create( $args, $assoc_args ) {
148-
$assoc_args = Utils\parse_shell_arrays( $assoc_args, [ 'comment_meta' ] );
174+
$assoc_args = self::parse_comment_meta( $assoc_args );
149175

150176
$assoc_args = wp_slash( $assoc_args );
151177
parent::_create(
@@ -243,7 +269,7 @@ function ( $params ) {
243269
* @param array<string, mixed> $assoc_args Associative arguments.
244270
*/
245271
public function update( $args, $assoc_args ) {
246-
$assoc_args = Utils\parse_shell_arrays( $assoc_args, [ 'comment_meta' ] );
272+
$assoc_args = self::parse_comment_meta( $assoc_args );
247273

248274
$assoc_args = wp_slash( $assoc_args );
249275
parent::_update(

0 commit comments

Comments
 (0)