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 */
4547class 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
0 commit comments