Skip to content

Commit e5becb4

Browse files
committed
PHPStan level 9
1 parent c125c84 commit e5becb4

10 files changed

+228
-72
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
"wp-cli/entity-command": "^1.3 || ^2",
2626
"wp-cli/language-command": "^2.0",
2727
"wp-cli/scaffold-command": "^1.2 || ^2",
28-
"wp-cli/wp-cli-tests": "^4.3.7"
28+
"wp-cli/wp-cli-tests": "dev-main"
2929
},
3030
"config": {
3131
"process-timeout": 7200,
3232
"sort-packages": true,
3333
"allow-plugins": {
3434
"dealerdirect/phpcodesniffer-composer-installer": true,
35-
"johnpbloch/wordpress-core-installer": true
35+
"johnpbloch/wordpress-core-installer": true,
36+
"phpstan/extension-installer": true
3637
},
3738
"lock": false
3839
},
@@ -92,11 +93,13 @@
9293
"lint": "run-linter-tests",
9394
"phpcs": "run-phpcs-tests",
9495
"phpcbf": "run-phpcbf-cleanup",
96+
"phpstan": "run-phpstan-tests",
9597
"phpunit": "run-php-unit-tests",
9698
"prepare-tests": "install-package-tests",
9799
"test": [
98100
"@lint",
99101
"@phpcs",
102+
"@phpstan",
100103
"@phpunit",
101104
"@behat"
102105
]

phpstan.neon.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- tests
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.iterableValue
18+
- identifier: missingType.property
19+
- identifier: missingType.parameter
20+
- identifier: missingType.return

src/Plugin_AutoUpdates_Command.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ public function __construct() {
7272
* $ wp plugin auto-updates enable hello
7373
* Plugin auto-updates for 'hello' enabled.
7474
* Success: Enabled 1 of 1 plugin auto-updates.
75+
*
76+
* @param array $args
77+
* @param array $assoc_args
7578
*/
7679
public function enable( $args, $assoc_args ) {
77-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
78-
$disabled_only = Utils\get_flag_value( $assoc_args, 'disabled-only', false );
80+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
81+
$disabled_only = (bool) Utils\get_flag_value( $assoc_args, 'disabled-only', false );
7982

8083
$args = $this->check_optional_args_and_all( $args, $all );
8184
if ( ! $args ) {
@@ -151,7 +154,7 @@ public function enable( $args, $assoc_args ) {
151154
* Success: Disabled 1 of 1 plugin auto-updates.
152155
*/
153156
public function disable( $args, $assoc_args ) {
154-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
157+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
155158
$enabled_only = Utils\get_flag_value( $assoc_args, 'enabled-only', false );
156159

157160
$args = $this->check_optional_args_and_all( $args, $all );
@@ -259,7 +262,7 @@ public function disable( $args, $assoc_args ) {
259262
* duplicate-post
260263
*/
261264
public function status( $args, $assoc_args ) {
262-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
265+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
263266
$enabled_only = Utils\get_flag_value( $assoc_args, 'enabled-only', false );
264267
$disabled_only = Utils\get_flag_value( $assoc_args, 'disabled-only', false );
265268

src/Plugin_Command.php

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
* Success: Installed 1 of 1 plugins.
4242
*
4343
* @package wp-cli
44+
*
45+
* @phpstan-type PluginInformation object{name: string, slug: non-empty-string, version: string, new_version: string, download_link: string, requires_php?: string, requires?: string, package: string}&\stdClass
4446
*/
4547
class Plugin_Command extends \WP_CLI\CommandWithUpgrade {
4648

@@ -209,6 +211,9 @@ public function search( $args, $assoc_args ) {
209211
}
210212

211213
protected function status_single( $args ) {
214+
/**
215+
* @var object{name: string, file: string} $plugin
216+
*/
212217
$plugin = $this->fetcher->get_check( $args[0] );
213218
$file = $plugin->file;
214219

@@ -345,11 +350,18 @@ protected function get_all_items() {
345350
* Plugin 'bbpress' network activated.
346351
* Plugin 'buddypress' network activated.
347352
* Success: Activated 2 of 2 plugins.
353+
*
354+
* @param array $args
355+
* @param array $assoc_args
348356
*/
349357
public function activate( $args, $assoc_args = array() ) {
350-
$network_wide = Utils\get_flag_value( $assoc_args, 'network', false );
351-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
352-
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
358+
$network_wide = (bool) Utils\get_flag_value( $assoc_args, 'network', false );
359+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
360+
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', '' );
361+
362+
/**
363+
* @var string $all_exclude
364+
*/
353365

354366
$args = $this->check_optional_args_and_all( $args, $all, 'activate', $all_exclude );
355367
if ( ! $args ) {
@@ -358,7 +370,11 @@ public function activate( $args, $assoc_args = array() ) {
358370

359371
$successes = 0;
360372
$errors = 0;
361-
$plugins = $this->fetcher->get_many( $args );
373+
374+
/**
375+
* @var array<object{name: string, file: string}> $plugins
376+
*/
377+
$plugins = $this->fetcher->get_many( $args );
362378
if ( count( $plugins ) < count( $args ) ) {
363379
$errors = count( $args ) - count( $plugins );
364380
}
@@ -387,7 +403,7 @@ public function activate( $args, $assoc_args = array() ) {
387403

388404
if ( is_wp_error( $result ) ) {
389405
$message = $result->get_error_message();
390-
$message = preg_replace( '/<a\s[^>]+>.*<\/a>/im', '', $message );
406+
$message = (string) preg_replace( '/<a\s[^>]+>.*<\/a>/im', '', $message );
391407
$message = wp_strip_all_tags( $message );
392408
$message = str_replace( 'Error: ', '', $message );
393409
WP_CLI::warning( "Failed to activate plugin. {$message}" );
@@ -438,9 +454,13 @@ public function activate( $args, $assoc_args = array() ) {
438454
* Success: Deactivated 2 of 2 plugins.
439455
*/
440456
public function deactivate( $args, $assoc_args = array() ) {
441-
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );
442-
$disable_all = Utils\get_flag_value( $assoc_args, 'all' );
443-
$disable_all_exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
457+
$network_wide = (bool) Utils\get_flag_value( $assoc_args, 'network' );
458+
$disable_all = (bool) Utils\get_flag_value( $assoc_args, 'all' );
459+
$disable_all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', '' );
460+
461+
/**
462+
* @var string $disable_all_exclude
463+
*/
444464

445465
$args = $this->check_optional_args_and_all( $args, $disable_all, 'deactivate', $disable_all_exclude );
446466
if ( ! $args ) {
@@ -574,6 +594,9 @@ public function path( $args, $assoc_args ) {
574594
$path = untrailingslashit( WP_PLUGIN_DIR );
575595

576596
if ( ! empty( $args ) ) {
597+
/**
598+
* @var object{name: string, file: string} $plugin
599+
*/
577600
$plugin = $this->fetcher->get_check( $args[0] );
578601
$path .= '/' . $plugin->file;
579602

@@ -591,6 +614,9 @@ protected function install_from_repo( $slug, $assoc_args ) {
591614
list($wp_core_version) = explode( '-', $wp_version );
592615
$wp_core_version = implode( '.', array_slice( explode( '.', $wp_core_version ), 0, 2 ) );
593616

617+
/**
618+
* @var \WP_Error|PluginInformation $api
619+
*/
594620
$api = plugins_api( 'plugin_information', array( 'slug' => $slug ) );
595621

596622
if ( is_wp_error( $api ) ) {
@@ -726,7 +752,7 @@ protected function install_from_repo( $slug, $assoc_args ) {
726752
* @alias upgrade
727753
*/
728754
public function update( $args, $assoc_args ) {
729-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
755+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
730756

731757
$args = $this->check_optional_args_and_all( $args, $all );
732758
if ( ! $args ) {
@@ -755,6 +781,9 @@ protected function get_item_list() {
755781
$auto_updates = [];
756782
}
757783

784+
/**
785+
* @var string[] $recently_active
786+
*/
758787
$recently_active = is_network_admin() ? get_site_option( 'recently_activated' ) : get_option( 'recently_activated' );
759788

760789
if ( false === $recently_active ) {
@@ -875,7 +904,7 @@ protected function get_item_list() {
875904

876905
if ( isset( $plugin_update_info->requires ) && version_compare( $wp_version, $requires, '>=' ) ) {
877906
$reason = "This update requires WordPress version $plugin_update_info->requires, but the version installed is $wp_version.";
878-
} elseif ( ! isset( $update_info['package'] ) ) {
907+
} elseif ( ! isset( $plugin_update_info->package ) ) {
879908
$reason = 'Update file not provided. Contact author for more details';
880909
} else {
881910
$reason = 'Update not available';
@@ -904,7 +933,7 @@ protected function get_item_list() {
904933
*
905934
* @param string $plugin_name The plugin slug.
906935
*
907-
* @return string The status of the plugin, includes the last update date.
936+
* @return array{status: string, last_updated: string|false, status?: string, last_updated?: string} The status of the plugin, includes the last update date.
908937
*/
909938
protected function get_wporg_data( $plugin_name ) {
910939
$data = [
@@ -947,10 +976,12 @@ protected function get_wporg_data( $plugin_name ) {
947976
$r_body = wp_remote_retrieve_body( $request );
948977
if ( strpos( $r_body, 'pubDate' ) !== false ) {
949978
// Very raw check, not validating the format or anything else.
950-
$xml = simplexml_load_string( $r_body );
951-
$xml_pub_date = $xml->xpath( '//pubDate' );
952-
if ( $xml_pub_date ) {
953-
$data['last_updated'] = wp_date( 'Y-m-d', (string) strtotime( $xml_pub_date[0] ) );
979+
$xml = simplexml_load_string( $r_body );
980+
if ( false !== $xml ) {
981+
$xml_pub_date = $xml->xpath( '//pubDate' );
982+
if ( $xml_pub_date ) {
983+
$data['last_updated'] = wp_date( 'Y-m-d', strtotime( $xml_pub_date[0] ) ?: null );
984+
}
954985
}
955986
}
956987

@@ -1115,6 +1146,9 @@ public function get( $args, $assoc_args ) {
11151146
'status',
11161147
);
11171148

1149+
/**
1150+
* @var object{name: string, file: string} $plugin
1151+
*/
11181152
$plugin = $this->fetcher->get_check( $args[0] );
11191153
$file = $plugin->file;
11201154

@@ -1174,10 +1208,13 @@ public function get( $args, $assoc_args ) {
11741208
* Success: Uninstalled 2 of 2 plugins.
11751209
*/
11761210
public function uninstall( $args, $assoc_args = array() ) {
1177-
1178-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
1211+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
11791212
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', false );
11801213

1214+
/**
1215+
* @var string $all_exclude
1216+
*/
1217+
11811218
// Check if plugin names or --all is passed.
11821219
$args = $this->check_optional_args_and_all( $args, $all, 'uninstall', $all_exclude );
11831220
if ( ! $args ) {
@@ -1222,6 +1259,9 @@ public function uninstall( $args, $assoc_args = array() ) {
12221259
if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
12231260
$translations = $plugin_translations[ $plugin_slug ];
12241261

1262+
/**
1263+
* @var \WP_Filesystem_Base $wp_filesystem
1264+
*/
12251265
global $wp_filesystem;
12261266
require_once ABSPATH . '/wp-admin/includes/file.php';
12271267
WP_Filesystem();
@@ -1233,7 +1273,11 @@ public function uninstall( $args, $assoc_args = array() ) {
12331273

12341274
$json_translation_files = glob( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '-*.json' );
12351275
if ( $json_translation_files ) {
1236-
array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
1276+
/**
1277+
* @var callable $callback
1278+
*/
1279+
$callback = array( $wp_filesystem, 'delete' );
1280+
array_map( $callback, $json_translation_files );
12371281
}
12381282
}
12391283
}
@@ -1257,6 +1301,10 @@ public function uninstall( $args, $assoc_args = array() ) {
12571301
// Remove deleted plugins from the plugin updates list.
12581302
$current = get_site_transient( $this->upgrade_transient );
12591303
if ( $current ) {
1304+
/**
1305+
* @var object{response: array<string, mixed>, checked: array<string, mixed>}&\stdClass $current
1306+
*/
1307+
12601308
// Don't remove the plugins that weren't deleted.
12611309
$deleted = array_diff( $deleted_plugin_files, $delete_errors );
12621310

@@ -1323,7 +1371,7 @@ public function is_installed( $args, $assoc_args = array() ) {
13231371
* @subcommand is-active
13241372
*/
13251373
public function is_active( $args, $assoc_args = array() ) {
1326-
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );
1374+
$network_wide = (bool) Utils\get_flag_value( $assoc_args, 'network' );
13271375

13281376
$plugin = $this->fetcher->get( $args[0] );
13291377

@@ -1367,9 +1415,13 @@ public function is_active( $args, $assoc_args = array() ) {
13671415
* Success: Deleted 2 of 2 plugins.
13681416
*/
13691417
public function delete( $args, $assoc_args = array() ) {
1370-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
1418+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
13711419
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', false );
13721420

1421+
/**
1422+
* @var string $all_exclude
1423+
*/
1424+
13731425
// Check if plugin names or --all is passed.
13741426
$args = $this->check_optional_args_and_all( $args, $all, 'delete', $all_exclude );
13751427
if ( ! $args ) {
@@ -1505,7 +1557,11 @@ public function delete( $args, $assoc_args = array() ) {
15051557
* @subcommand list
15061558
*/
15071559
public function list_( $_, $assoc_args ) {
1560+
/**
1561+
* @var string $fields
1562+
*/
15081563
$fields = Utils\get_flag_value( $assoc_args, 'fields' );
1564+
15091565
if ( ! empty( $fields ) ) {
15101566
$fields = explode( ',', $fields );
15111567
$this->check_wporg['status'] = in_array( 'wporg_status', $fields, true );
@@ -1578,7 +1634,7 @@ private static function get_template_path( $template ) {
15781634
/**
15791635
* Gets the details of a plugin.
15801636
*
1581-
* @param object
1637+
* @param string $file Plugin file name.
15821638
* @return array
15831639
*/
15841640
private function get_details( $file ) {
@@ -1591,8 +1647,8 @@ private function get_details( $file ) {
15911647
/**
15921648
* Performs deletion of plugin files
15931649
*
1594-
* @param $plugin - Plugin fetcher object (name, file)
1595-
* @return bool - If plugin was deleted
1650+
* @param $plugin Plugin fetcher object (name, file)
1651+
* @return bool Whether plugin was deleted
15961652
*/
15971653
private function delete_plugin( $plugin ) {
15981654
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound

src/Theme_AutoUpdates_Command.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __construct() {
7474
* Success: Enabled 1 of 1 theme auto-updates.
7575
*/
7676
public function enable( $args, $assoc_args ) {
77-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
77+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
7878
$disabled_only = Utils\get_flag_value( $assoc_args, 'disabled-only', false );
7979

8080
$args = $this->check_optional_args_and_all( $args, $all );
@@ -151,8 +151,8 @@ public function enable( $args, $assoc_args ) {
151151
* Success: Disabled 1 of 1 theme auto-updates.
152152
*/
153153
public function disable( $args, $assoc_args ) {
154-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
155-
$enabled_only = Utils\get_flag_value( $assoc_args, 'enabled-only', false );
154+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
155+
$enabled_only = (bool) Utils\get_flag_value( $assoc_args, 'enabled-only', false );
156156

157157
$args = $this->check_optional_args_and_all( $args, $all );
158158
if ( ! $args ) {
@@ -259,9 +259,9 @@ public function disable( $args, $assoc_args ) {
259259
* twentyseventeen
260260
*/
261261
public function status( $args, $assoc_args ) {
262-
$all = Utils\get_flag_value( $assoc_args, 'all', false );
263-
$enabled_only = Utils\get_flag_value( $assoc_args, 'enabled-only', false );
264-
$disabled_only = Utils\get_flag_value( $assoc_args, 'disabled-only', false );
262+
$all = (bool) Utils\get_flag_value( $assoc_args, 'all', false );
263+
$enabled_only = (bool) Utils\get_flag_value( $assoc_args, 'enabled-only', false );
264+
$disabled_only = (bool) Utils\get_flag_value( $assoc_args, 'disabled-only', false );
265265

266266
if ( $enabled_only && $disabled_only ) {
267267
WP_CLI::error(

0 commit comments

Comments
 (0)