Skip to content

Commit 4fc34af

Browse files
committed
PHPStan level 9
1 parent b663d43 commit 4fc34af

File tree

4 files changed

+74
-20
lines changed

4 files changed

+74
-20
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": "dev-main"
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- cache-command.php
6+
scanDirectories:
7+
- vendor/wp-cli/wp-cli/php
8+
- vendor/wp-cli/wp-cli-tests
9+
scanFiles:
10+
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
11+
treatPhpDocTypesAsCertain: false
12+
dynamicConstantNames:
13+
- WP_DEBUG
14+
- WP_DEBUG_LOG
15+
- WP_DEBUG_DISPLAY
16+
ignoreErrors:
17+
- identifier: missingType.parameter
18+
- identifier: missingType.return

src/Cache_Command.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ public function delete( $args, $assoc_args ) {
158158
* Success: The cache was flushed.
159159
*/
160160
public function flush( $args, $assoc_args ) {
161-
161+
// TODO: Needs fixing in wp-cli/wp-cli
162+
// @phpstan-ignore offsetAccess.nonOffsetAccessible
162163
if ( WP_CLI::has_config( 'url' ) && ! empty( WP_CLI::get_config()['url'] ) && is_multisite() ) {
163164
WP_CLI::warning( 'Flushing the cache may affect all sites in a multisite installation, depending on the implementation of the object cache.' );
164165
}
@@ -439,7 +440,11 @@ public function flush_group( $args, $assoc_args ) {
439440
*/
440441
public function pluck( $args, $assoc_args ) {
441442
list( $key ) = $args;
442-
$group = Utils\get_flag_value( $assoc_args, 'group' );
443+
444+
/**
445+
* @var string $group
446+
*/
447+
$group = Utils\get_flag_value( $assoc_args, 'group' );
443448

444449
$value = wp_cache_get( $key, $group );
445450

@@ -512,11 +517,21 @@ function ( $key ) {
512517
* - plaintext
513518
* - json
514519
* ---
520+
*
521+
* @param string[] $args
515522
*/
516523
public function patch( $args, $assoc_args ) {
517524
list( $action, $key ) = $args;
518-
$group = Utils\get_flag_value( $assoc_args, 'group' );
519-
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );
525+
526+
/**
527+
* @var string $group
528+
*/
529+
$group = Utils\get_flag_value( $assoc_args, 'group' );
530+
531+
/**
532+
* @var string|null $expiration
533+
*/
534+
$expiration = Utils\get_flag_value( $assoc_args, 'expiration' );
520535

521536
$key_path = array_map(
522537
function ( $key ) {
@@ -569,7 +584,7 @@ function ( $key ) {
569584
if ( $patched_value === $old_value ) {
570585
WP_CLI::success( "Value passed for cache key '$key' is unchanged." );
571586
} else {
572-
$success = wp_cache_set( $key, $patched_value, $group, $expiration );
587+
$success = wp_cache_set( $key, $patched_value, $group, (int) $expiration );
573588
if ( $success ) {
574589
WP_CLI::success( "Updated cache key '$key'." );
575590
} else {

src/Transient_Command.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,16 @@ public function get( $args, $assoc_args ) {
116116
*
117117
* $ wp transient set sample_key "test data" 3600
118118
* Success: Transient added.
119+
*
120+
* @param string[] $args
119121
*/
120122
public function set( $args, $assoc_args ) {
121123
list( $key, $value ) = $args;
122124

123-
$expiration = Utils\get_flag_value( $args, 2, 0 );
125+
$expiration = $args[2] ?? 0;
124126

125127
$func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
126-
if ( $func( $key, $value, $expiration ) ) {
128+
if ( $func( $key, $value, (int) $expiration ) ) {
127129
WP_CLI::success( 'Transient added.' );
128130
} else {
129131
WP_CLI::error( 'Transient could not be set.' );
@@ -180,9 +182,9 @@ public function set( $args, $assoc_args ) {
180182
public function delete( $args, $assoc_args ) {
181183
$key = ( ! empty( $args ) ) ? $args[0] : null;
182184

183-
$all = Utils\get_flag_value( $assoc_args, 'all' );
184-
$expired = Utils\get_flag_value( $assoc_args, 'expired' );
185-
$network = Utils\get_flag_value( $assoc_args, 'network' );
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' );
186188

187189
if ( true === $all ) {
188190
$this->delete_all( $network );
@@ -301,9 +303,9 @@ public function list_( $args, $assoc_args ) {
301303
WP_CLI::warning( 'Transients are stored in an external object cache, and this command only shows those stored in the database.' );
302304
}
303305

304-
$network = Utils\get_flag_value( $assoc_args, 'network', false );
305-
$unserialize = Utils\get_flag_value( $assoc_args, 'unserialize', false );
306-
$human_readable = Utils\get_flag_value( $assoc_args, 'human-readable', false );
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 );
307309

308310
$fields = array( 'name', 'value', 'expiration' );
309311
if ( isset( $assoc_args['fields'] ) ) {
@@ -505,7 +507,12 @@ function ( $key ) {
505507
*/
506508
public function patch( $args, $assoc_args ) {
507509
list( $action, $key ) = $args;
508-
$expiration = (int) Utils\get_flag_value( $assoc_args, 'expiration', 0 );
510+
511+
/**
512+
* @var string $expiration
513+
*/
514+
$expiration = Utils\get_flag_value( $assoc_args, 'expiration', 0 );
515+
$expiration = (int) $expiration;
509516

510517
$read_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'get_site_transient' : 'get_transient';
511518
$write_func = Utils\get_flag_value( $assoc_args, 'network' ) ? 'set_site_transient' : 'set_transient';
@@ -582,20 +589,31 @@ function ( $key ) {
582589
private function get_transient_expiration( $name, $is_site_transient = false, $human_readable = false ) {
583590
if ( $is_site_transient ) {
584591
if ( is_multisite() ) {
585-
$expiration = (int) get_site_option( '_site_transient_timeout_' . $name );
592+
/**
593+
* @var string $expiration
594+
*/
595+
$expiration = get_site_option( '_site_transient_timeout_' . $name );
586596
} else {
587-
$expiration = (int) get_option( '_site_transient_timeout_' . $name );
597+
/**
598+
* @var string $expiration
599+
*/
600+
$expiration = get_option( '_site_transient_timeout_' . $name );
588601
}
589602
} else {
590-
$expiration = (int) get_option( '_transient_timeout_' . $name );
603+
/**
604+
* @var string $expiration
605+
*/
606+
$expiration = get_option( '_transient_timeout_' . $name );
591607
}
592608

609+
$expiration = (int) $expiration;
610+
593611
if ( 0 === $expiration ) {
594612
return $human_readable ? 'never expires' : 'false';
595613
}
596614

597615
if ( ! $human_readable ) {
598-
return $expiration;
616+
return (string) $expiration;
599617
}
600618

601619
$now = time();

0 commit comments

Comments
 (0)