Skip to content

Commit bca0905

Browse files
committed
Improve param types
1 parent 4fc34af commit bca0905

File tree

4 files changed

+71
-24
lines changed

4 files changed

+71
-24
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"require-dev": {
1818
"wp-cli/entity-command": "^1.3 || ^2",
19-
"wp-cli/wp-cli-tests": "dev-main"
19+
"wp-cli/wp-cli-tests": "dev-add/phpstan-enhancements"
2020
},
2121
"config": {
2222
"process-timeout": 7200,

phpstan.neon.dist

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ parameters:
1313
- WP_DEBUG
1414
- WP_DEBUG_LOG
1515
- WP_DEBUG_DISPLAY
16+
strictRules:
17+
uselessCast: true
18+
closureUsesThis: true
19+
overwriteVariablesWithLoop: true
20+
matchingInheritedMethodNames: true
21+
numericOperandsInArithmeticOperators: true
22+
switchConditionsMatchingType: true
1623
ignoreErrors:
17-
- identifier: missingType.parameter
1824
- identifier: missingType.return

src/Cache_Command.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ class Cache_Command extends WP_CLI_Command {
5858
* # Add cache.
5959
* $ wp cache add my_key my_group my_value 300
6060
* Success: Added object 'my_key' in group 'my_value'.
61+
*
62+
* @param array{string, string, string, string} $args Positional arguments.
63+
* @param array<mixed> $assoc_args Associative arguments.
6164
*/
6265
public function add( $args, $assoc_args ) {
6366
list( $key, $value, $group, $expiration ) = $args;
6467

65-
if ( ! wp_cache_add( $key, $value, $group, $expiration ) ) {
68+
if ( ! wp_cache_add( $key, $value, $group, (int) $expiration ) ) {
6669
WP_CLI::error( "Could not add object '$key' in group '$group'. Does it already exist?" );
6770
}
6871

@@ -96,10 +99,13 @@ public function add( $args, $assoc_args ) {
9699
* # Decrease cache value.
97100
* $ wp cache decr my_key 2 my_group
98101
* 48
102+
*
103+
* @param array{string, string, string} $args Positional arguments.
104+
* @param array<mixed> $assoc_args Associative arguments.
99105
*/
100106
public function decr( $args, $assoc_args ) {
101107
list( $key, $offset, $group ) = $args;
102-
$value = wp_cache_decr( $key, $offset, $group );
108+
$value = wp_cache_decr( $key, (int) $offset, $group );
103109

104110
if ( false === $value ) {
105111
WP_CLI::error( 'The value was not decremented.' );
@@ -129,6 +135,9 @@ public function decr( $args, $assoc_args ) {
129135
* # Delete cache.
130136
* $ wp cache delete my_key my_group
131137
* Success: Object deleted.
138+
*
139+
* @param array{string, string} $args Positional arguments.
140+
* @param array<mixed> $assoc_args Associative arguments.
132141
*/
133142
public function delete( $args, $assoc_args ) {
134143
list( $key, $group ) = $args;
@@ -157,7 +166,7 @@ public function delete( $args, $assoc_args ) {
157166
* $ wp cache flush
158167
* Success: The cache was flushed.
159168
*/
160-
public function flush( $args, $assoc_args ) {
169+
public function flush() {
161170
// TODO: Needs fixing in wp-cli/wp-cli
162171
// @phpstan-ignore offsetAccess.nonOffsetAccessible
163172
if ( WP_CLI::has_config( 'url' ) && ! empty( WP_CLI::get_config()['url'] ) && is_multisite() ) {
@@ -193,6 +202,9 @@ public function flush( $args, $assoc_args ) {
193202
* # Get cache.
194203
* $ wp cache get my_key my_group
195204
* my_value
205+
*
206+
* @param array{string, string} $args Positional arguments.
207+
* @param array<mixed> $assoc_args Associative arguments.
196208
*/
197209
public function get( $args, $assoc_args ) {
198210
list( $key, $group ) = $args;
@@ -232,10 +244,13 @@ public function get( $args, $assoc_args ) {
232244
* # Increase cache value.
233245
* $ wp cache incr my_key 2 my_group
234246
* 50
247+
*
248+
* @param array{string, string, string} $args Positional arguments.
249+
* @param array<mixed> $assoc_args Associative arguments.
235250
*/
236251
public function incr( $args, $assoc_args ) {
237252
list( $key, $offset, $group ) = $args;
238-
$value = wp_cache_incr( $key, $offset, $group );
253+
$value = wp_cache_incr( $key, (int) $offset, $group );
239254

240255
if ( false === $value ) {
241256
WP_CLI::error( 'The value was not incremented.' );
@@ -274,10 +289,13 @@ public function incr( $args, $assoc_args ) {
274289
* # Replace cache.
275290
* $ wp cache replace my_key new_value my_group
276291
* Success: Replaced object 'my_key' in group 'my_group'.
292+
*
293+
* @param array{string, string, string, string} $args Positional arguments.
294+
* @param array<mixed> $assoc_args Associative arguments.
277295
*/
278296
public function replace( $args, $assoc_args ) {
279297
list( $key, $value, $group, $expiration ) = $args;
280-
$result = wp_cache_replace( $key, $value, $group, $expiration );
298+
$result = wp_cache_replace( $key, $value, $group, (int) $expiration );
281299

282300
if ( false === $result ) {
283301
WP_CLI::error( "Could not replace object '$key' in group '$group'. Does it not exist?" );
@@ -316,10 +334,13 @@ public function replace( $args, $assoc_args ) {
316334
* # Set cache.
317335
* $ wp cache set my_key my_value my_group 300
318336
* Success: Set object 'my_key' in group 'my_group'.
337+
*
338+
* @param array{string, string, string, string} $args Positional arguments.
339+
* @param array<mixed> $assoc_args Associative arguments.
319340
*/
320341
public function set( $args, $assoc_args ) {
321342
list( $key, $value, $group, $expiration ) = $args;
322-
$result = wp_cache_set( $key, $value, $group, $expiration );
343+
$result = wp_cache_set( $key, $value, $group, (int) $expiration );
323344

324345
if ( false === $result ) {
325346
WP_CLI::error( "Could not add object '$key' in group '$group'." );
@@ -342,7 +363,7 @@ public function set( $args, $assoc_args ) {
342363
* $ wp cache type
343364
* Default
344365
*/
345-
public function type( $args, $assoc_args ) {
366+
public function type() {
346367
$message = WP_CLI\Utils\wp_get_cache_type();
347368
WP_CLI::line( $message );
348369
}
@@ -366,8 +387,10 @@ public function type( $args, $assoc_args ) {
366387
* if ! wp cache supports non_existing; then
367388
* echo 'non_existing is not supported'
368389
* fi
390+
*
391+
* @param array{string} $args Positional arguments.
369392
*/
370-
public function supports( $args, $assoc_args ) {
393+
public function supports( $args ) {
371394
list ( $feature ) = $args;
372395

373396
if ( ! function_exists( 'wp_cache_supports' ) ) {
@@ -397,8 +420,10 @@ public function supports( $args, $assoc_args ) {
397420
* Success: Cache group 'my_group' was flushed.
398421
*
399422
* @subcommand flush-group
423+
*
424+
* @param array{string} $args Positional arguments.
400425
*/
401-
public function flush_group( $args, $assoc_args ) {
426+
public function flush_group( $args ) {
402427
list( $group ) = $args;
403428

404429
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
@@ -437,6 +462,9 @@ public function flush_group( $args, $assoc_args ) {
437462
* - json
438463
* - yaml
439464
* ---
465+
*
466+
* @param array{string, string} $args Positional arguments.
467+
* @param array{group: string, format: string} $assoc_args Associative arguments.
440468
*/
441469
public function pluck( $args, $assoc_args ) {
442470
list( $key ) = $args;
@@ -518,7 +546,8 @@ function ( $key ) {
518546
* - json
519547
* ---
520548
*
521-
* @param string[] $args
549+
* @param string[] $args Positional arguments.
550+
* @param array{group: string, expiration: string, format: string} $assoc_args Associative arguments.
522551
*/
523552
public function patch( $args, $assoc_args ) {
524553
list( $action, $key ) = $args;

src/Transient_Command.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class Transient_Command extends WP_CLI_Command {
7373
*
7474
* $ wp transient get random_key
7575
* Warning: Transient with key "random_key" is not set.
76+
*
77+
* @param array{string} $args Positional arguments.
78+
* @param array{format: string} $assoc_args Associative arguments.
7679
*/
7780
public function get( $args, $assoc_args ) {
7881
list( $key ) = $args;
@@ -117,7 +120,8 @@ public function get( $args, $assoc_args ) {
117120
* $ wp transient set sample_key "test data" 3600
118121
* Success: Transient added.
119122
*
120-
* @param string[] $args
123+
* @param array{0: string, 1: string, 2?: string} $args Positional arguments.
124+
* @param array{network?: bool} $assoc_args Associative arguments.
121125
*/
122126
public function set( $args, $assoc_args ) {
123127
list( $key, $value ) = $args;
@@ -178,13 +182,16 @@ public function set( $args, $assoc_args ) {
178182
*
179183
* # Delete all transients in a multisite.
180184
* $ wp transient delete --all --network && wp site list --field=url | xargs -n1 -I % wp --url=% transient delete --all
185+
*
186+
* @param array{string} $args Positional arguments.
187+
* @param array{network?: bool, all?: bool, expired?: bool} $assoc_args Associative arguments.
181188
*/
182189
public function delete( $args, $assoc_args ) {
183190
$key = ( ! empty( $args ) ) ? $args[0] : null;
184191

185-
$all = (bool) Utils\get_flag_value( $assoc_args, 'all' );
186-
$expired = (bool) Utils\get_flag_value( $assoc_args, 'expired' );
187-
$network = (bool) Utils\get_flag_value( $assoc_args, 'network' );
192+
$all = Utils\get_flag_value( $assoc_args, 'all' );
193+
$expired = Utils\get_flag_value( $assoc_args, 'expired' );
194+
$network = Utils\get_flag_value( $assoc_args, 'network' );
188195

189196
if ( true === $all ) {
190197
$this->delete_all( $network );
@@ -295,6 +302,9 @@ public function type() {
295302
* +------+-------+---------------+
296303
*
297304
* @subcommand list
305+
*
306+
* @param string[] $args Positional arguments. Unused.
307+
* @param array{search?: string, exclude?: string, network?: bool, unserialize?: bool, 'human-readable'?: bool, fields?: string, format?: string} $assoc_args Associative arguments.
298308
*/
299309
public function list_( $args, $assoc_args ) {
300310
global $wpdb;
@@ -303,9 +313,9 @@ public function list_( $args, $assoc_args ) {
303313
WP_CLI::warning( 'Transients are stored in an external object cache, and this command only shows those stored in the database.' );
304314
}
305315

306-
$network = (bool) Utils\get_flag_value( $assoc_args, 'network', false );
307-
$unserialize = (bool) Utils\get_flag_value( $assoc_args, 'unserialize', false );
308-
$human_readable = (bool) Utils\get_flag_value( $assoc_args, 'human-readable', false );
316+
$network = Utils\get_flag_value( $assoc_args, 'network', false );
317+
$unserialize = Utils\get_flag_value( $assoc_args, 'unserialize', false );
318+
$human_readable = Utils\get_flag_value( $assoc_args, 'human-readable', false );
309319

310320
$fields = array( 'name', 'value', 'expiration' );
311321
if ( isset( $assoc_args['fields'] ) ) {
@@ -432,6 +442,9 @@ public function list_( $args, $assoc_args ) {
432442
* : Get the value of a network|site transient. On single site, this is
433443
* a specially-named cache key. On multisite, this is a global cache
434444
* (instead of local to the site).
445+
*
446+
* @param string[] $args Positional arguments.
447+
* @param array{format: string} $assoc_args Associative arguments.
435448
*/
436449
public function pluck( $args, $assoc_args ) {
437450
list( $key ) = $args;
@@ -504,15 +517,14 @@ function ( $key ) {
504517
* : Get the value of a network|site transient. On single site, this is
505518
* a specially-named cache key. On multisite, this is a global cache
506519
* (instead of local to the site).
520+
*
521+
* @param string[] $args Positional arguments.
522+
* @param array{format: string} $assoc_args Associative arguments.
507523
*/
508524
public function patch( $args, $assoc_args ) {
509525
list( $action, $key ) = $args;
510526

511-
/**
512-
* @var string $expiration
513-
*/
514-
$expiration = Utils\get_flag_value( $assoc_args, 'expiration', 0 );
515-
$expiration = (int) $expiration;
527+
$expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration', 0 );
516528

517529
$read_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient';
518530
$write_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';

0 commit comments

Comments
 (0)