Skip to content

Commit 8e99afb

Browse files
authored
Merge pull request #110 from wp-cli/add/phpstan
2 parents 025bd1a + a2acbe8 commit 8e99afb

File tree

4 files changed

+110
-23
lines changed

4 files changed

+110
-23
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
},
1717
"require-dev": {
1818
"wp-cli/entity-command": "^1.3 || ^2",
19-
"wp-cli/wp-cli-tests": "^4"
19+
"wp-cli/wp-cli-tests": "^5"
2020
},
2121
"config": {
2222
"process-timeout": 7200,
2323
"sort-packages": true,
2424
"allow-plugins": {
2525
"dealerdirect/phpcodesniffer-composer-installer": true,
26-
"johnpbloch/wordpress-core-installer": true
26+
"johnpbloch/wordpress-core-installer": true,
27+
"phpstan/extension-installer": true
2728
},
2829
"lock": false
2930
},
@@ -72,12 +73,14 @@
7273
"behat-rerun": "rerun-behat-tests",
7374
"lint": "run-linter-tests",
7475
"phpcs": "run-phpcs-tests",
76+
"phpstan": "run-phpstan-tests",
7577
"phpcbf": "run-phpcbf-cleanup",
7678
"phpunit": "run-php-unit-tests",
7779
"prepare-tests": "install-package-tests",
7880
"test": [
7981
"@lint",
8082
"@phpcs",
83+
"@phpstan",
8184
"@phpunit",
8285
"@behat"
8386
]

phpstan.neon.dist

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- cache-command.php
6+
scanDirectories:
7+
- vendor/wp-cli/wp-cli/php
8+
scanFiles:
9+
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
treatPhpDocTypesAsCertain: false
11+
strictRules:
12+
uselessCast: true
13+
closureUsesThis: true
14+
overwriteVariablesWithLoop: true
15+
matchingInheritedMethodNames: true
16+
numericOperandsInArithmeticOperators: true
17+
switchConditionsMatchingType: true
18+
ignoreErrors:
19+
- identifier: missingType.return

src/Cache_Command.php

Lines changed: 49 additions & 14 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,8 +166,9 @@ public function delete( $args, $assoc_args ) {
157166
* $ wp cache flush
158167
* Success: The cache was flushed.
159168
*/
160-
public function flush( $args, $assoc_args ) {
161-
169+
public function flush() {
170+
// TODO: Needs fixing in wp-cli/wp-cli
171+
// @phpstan-ignore offsetAccess.nonOffsetAccessible
162172
if ( WP_CLI::has_config( 'url' ) && ! empty( WP_CLI::get_config()['url'] ) && is_multisite() ) {
163173
WP_CLI::warning( 'Flushing the cache may affect all sites in a multisite installation, depending on the implementation of the object cache.' );
164174
}
@@ -192,6 +202,9 @@ public function flush( $args, $assoc_args ) {
192202
* # Get cache.
193203
* $ wp cache get my_key my_group
194204
* my_value
205+
*
206+
* @param array{string, string} $args Positional arguments.
207+
* @param array<mixed> $assoc_args Associative arguments.
195208
*/
196209
public function get( $args, $assoc_args ) {
197210
list( $key, $group ) = $args;
@@ -231,10 +244,13 @@ public function get( $args, $assoc_args ) {
231244
* # Increase cache value.
232245
* $ wp cache incr my_key 2 my_group
233246
* 50
247+
*
248+
* @param array{string, string, string} $args Positional arguments.
249+
* @param array<mixed> $assoc_args Associative arguments.
234250
*/
235251
public function incr( $args, $assoc_args ) {
236252
list( $key, $offset, $group ) = $args;
237-
$value = wp_cache_incr( $key, $offset, $group );
253+
$value = wp_cache_incr( $key, (int) $offset, $group );
238254

239255
if ( false === $value ) {
240256
WP_CLI::error( 'The value was not incremented.' );
@@ -273,10 +289,13 @@ public function incr( $args, $assoc_args ) {
273289
* # Replace cache.
274290
* $ wp cache replace my_key new_value my_group
275291
* 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.
276295
*/
277296
public function replace( $args, $assoc_args ) {
278297
list( $key, $value, $group, $expiration ) = $args;
279-
$result = wp_cache_replace( $key, $value, $group, $expiration );
298+
$result = wp_cache_replace( $key, $value, $group, (int) $expiration );
280299

281300
if ( false === $result ) {
282301
WP_CLI::error( "Could not replace object '$key' in group '$group'. Does it not exist?" );
@@ -315,10 +334,13 @@ public function replace( $args, $assoc_args ) {
315334
* # Set cache.
316335
* $ wp cache set my_key my_value my_group 300
317336
* 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.
318340
*/
319341
public function set( $args, $assoc_args ) {
320342
list( $key, $value, $group, $expiration ) = $args;
321-
$result = wp_cache_set( $key, $value, $group, $expiration );
343+
$result = wp_cache_set( $key, $value, $group, (int) $expiration );
322344

323345
if ( false === $result ) {
324346
WP_CLI::error( "Could not add object '$key' in group '$group'." );
@@ -341,7 +363,7 @@ public function set( $args, $assoc_args ) {
341363
* $ wp cache type
342364
* Default
343365
*/
344-
public function type( $args, $assoc_args ) {
366+
public function type() {
345367
$message = WP_CLI\Utils\wp_get_cache_type();
346368
WP_CLI::line( $message );
347369
}
@@ -365,8 +387,10 @@ public function type( $args, $assoc_args ) {
365387
* if ! wp cache supports non_existing; then
366388
* echo 'non_existing is not supported'
367389
* fi
390+
*
391+
* @param array{string} $args Positional arguments.
368392
*/
369-
public function supports( $args, $assoc_args ) {
393+
public function supports( $args ) {
370394
list ( $feature ) = $args;
371395

372396
if ( ! function_exists( 'wp_cache_supports' ) ) {
@@ -396,8 +420,10 @@ public function supports( $args, $assoc_args ) {
396420
* Success: Cache group 'my_group' was flushed.
397421
*
398422
* @subcommand flush-group
423+
*
424+
* @param array{string} $args Positional arguments.
399425
*/
400-
public function flush_group( $args, $assoc_args ) {
426+
public function flush_group( $args ) {
401427
list( $group ) = $args;
402428

403429
if ( ! function_exists( 'wp_cache_supports' ) || ! wp_cache_supports( 'flush_group' ) ) {
@@ -436,10 +462,14 @@ public function flush_group( $args, $assoc_args ) {
436462
* - json
437463
* - yaml
438464
* ---
465+
*
466+
* @param array{string, string} $args Positional arguments.
467+
* @param array{group: string, format: string} $assoc_args Associative arguments.
439468
*/
440469
public function pluck( $args, $assoc_args ) {
441470
list( $key ) = $args;
442-
$group = Utils\get_flag_value( $assoc_args, 'group' );
471+
472+
$group = Utils\get_flag_value( $assoc_args, 'group' );
443473

444474
$value = wp_cache_get( $key, $group );
445475

@@ -512,11 +542,16 @@ function ( $key ) {
512542
* - plaintext
513543
* - json
514544
* ---
545+
*
546+
* @param string[] $args Positional arguments.
547+
* @param array{group: string, expiration: string, format: string} $assoc_args Associative arguments.
515548
*/
516549
public function patch( $args, $assoc_args ) {
517550
list( $action, $key ) = $args;
518-
$group = Utils\get_flag_value( $assoc_args, 'group' );
519-
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );
551+
552+
$group = Utils\get_flag_value( $assoc_args, 'group' );
553+
554+
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );
520555

521556
$key_path = array_map(
522557
function ( $key ) {
@@ -569,7 +604,7 @@ function ( $key ) {
569604
if ( $patched_value === $old_value ) {
570605
WP_CLI::success( "Value passed for cache key '$key' is unchanged." );
571606
} else {
572-
$success = wp_cache_set( $key, $patched_value, $group, $expiration );
607+
$success = wp_cache_set( $key, $patched_value, $group, (int) $expiration );
573608
if ( $success ) {
574609
WP_CLI::success( "Updated cache key '$key'." );
575610
} else {

src/Transient_Command.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class Transient_Command extends WP_CLI_Command {
7777
*
7878
* $ wp transient get random_key
7979
* Warning: Transient with key "random_key" is not set.
80+
*
81+
* @param array{string} $args Positional arguments.
82+
* @param array{format: string} $assoc_args Associative arguments.
8083
*/
8184
public function get( $args, $assoc_args ) {
8285
list( $key ) = $args;
@@ -120,14 +123,17 @@ public function get( $args, $assoc_args ) {
120123
*
121124
* $ wp transient set sample_key "test data" 3600
122125
* Success: Transient added.
126+
*
127+
* @param array{0: string, 1: string, 2?: string} $args Positional arguments.
128+
* @param array{network?: bool} $assoc_args Associative arguments.
123129
*/
124130
public function set( $args, $assoc_args ) {
125131
list( $key, $value ) = $args;
126132

127-
$expiration = Utils\get_flag_value( $args, 2, 0 );
133+
$expiration = $args[2] ?? 0;
128134

129135
$func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
130-
if ( $func( $key, $value, $expiration ) ) {
136+
if ( $func( $key, $value, (int) $expiration ) ) {
131137
WP_CLI::success( 'Transient added.' );
132138
} else {
133139
WP_CLI::error( 'Transient could not be set.' );
@@ -180,6 +186,9 @@ public function set( $args, $assoc_args ) {
180186
*
181187
* # Delete all transients in a multisite.
182188
* $ wp transient delete --all --network && wp site list --field=url | xargs -n1 -I % wp --url=% transient delete --all
189+
*
190+
* @param array{string} $args Positional arguments.
191+
* @param array{network?: bool, all?: bool, expired?: bool} $assoc_args Associative arguments.
183192
*/
184193
public function delete( $args, $assoc_args ) {
185194
$key = ( ! empty( $args ) ) ? $args[0] : null;
@@ -297,6 +306,9 @@ public function type() {
297306
* +------+-------+---------------+
298307
*
299308
* @subcommand list
309+
*
310+
* @param string[] $args Positional arguments. Unused.
311+
* @param array{search?: string, exclude?: string, network?: bool, unserialize?: bool, 'human-readable'?: bool, fields?: string, format?: string} $assoc_args Associative arguments.
300312
*/
301313
public function list_( $args, $assoc_args ) {
302314
global $wpdb;
@@ -434,6 +446,9 @@ public function list_( $args, $assoc_args ) {
434446
* : Get the value of a network|site transient. On single site, this is
435447
* a specially-named cache key. On multisite, this is a global cache
436448
* (instead of local to the site).
449+
*
450+
* @param string[] $args Positional arguments.
451+
* @param array{format: string} $assoc_args Associative arguments.
437452
*/
438453
public function pluck( $args, $assoc_args ) {
439454
list( $key ) = $args;
@@ -506,10 +521,14 @@ function ( $key ) {
506521
* : Get the value of a network|site transient. On single site, this is
507522
* a specially-named cache key. On multisite, this is a global cache
508523
* (instead of local to the site).
524+
*
525+
* @param string[] $args Positional arguments.
526+
* @param array{format: string} $assoc_args Associative arguments.
509527
*/
510528
public function patch( $args, $assoc_args ) {
511529
list( $action, $key ) = $args;
512-
$expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration', 0 );
530+
531+
$expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration', 0 );
513532

514533
$read_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient';
515534
$write_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
@@ -586,20 +605,31 @@ function ( $key ) {
586605
private function get_transient_expiration( $name, $is_site_transient = false, $human_readable = false ) {
587606
if ( $is_site_transient ) {
588607
if ( is_multisite() ) {
589-
$expiration = (int) get_site_option( '_site_transient_timeout_' . $name );
608+
/**
609+
* @var string $expiration
610+
*/
611+
$expiration = get_site_option( '_site_transient_timeout_' . $name );
590612
} else {
591-
$expiration = (int) get_option( '_site_transient_timeout_' . $name );
613+
/**
614+
* @var string $expiration
615+
*/
616+
$expiration = get_option( '_site_transient_timeout_' . $name );
592617
}
593618
} else {
594-
$expiration = (int) get_option( '_transient_timeout_' . $name );
619+
/**
620+
* @var string $expiration
621+
*/
622+
$expiration = get_option( '_transient_timeout_' . $name );
595623
}
596624

625+
$expiration = (int) $expiration;
626+
597627
if ( 0 === $expiration ) {
598628
return $human_readable ? 'never expires' : 'false';
599629
}
600630

601631
if ( ! $human_readable ) {
602-
return $expiration;
632+
return (string) $expiration;
603633
}
604634

605635
$now = time();

0 commit comments

Comments
 (0)