Skip to content

PHPStan level 9 #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@
],
"require": {
"composer/semver": "^1.4 || ^2 || ^3",
"wp-cli/wp-cli": "^2.12"
"wp-cli/wp-cli": "^2.13"
},
"require-dev": {
"wp-cli/cache-command": "^2.0",
"wp-cli/entity-command": "^1.3 || ^2",
"wp-cli/language-command": "^2.0",
"wp-cli/scaffold-command": "^1.2 || ^2",
"wp-cli/wp-cli-tests": "^4.3.7"
"wp-cli/wp-cli-tests": "^5"
},
"config": {
"process-timeout": 7200,
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"johnpbloch/wordpress-core-installer": true
"johnpbloch/wordpress-core-installer": true,
"phpstan/extension-installer": true
},
"lock": false
},
Expand Down Expand Up @@ -92,11 +93,13 @@
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpcbf": "run-phpcbf-cleanup",
"phpstan": "run-phpstan-tests",
"phpunit": "run-php-unit-tests",
"prepare-tests": "install-package-tests",
"test": [
"@lint",
"@phpcs",
"@phpstan",
"@phpunit",
"@behat"
]
Expand Down
15 changes: 15 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
parameters:
level: 9
paths:
- src
- extension-command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.property
- identifier: missingType.parameter
- identifier: missingType.return
3 changes: 3 additions & 0 deletions src/Plugin_AutoUpdates_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public function __construct() {
* $ wp plugin auto-updates enable hello
* Plugin auto-updates for 'hello' enabled.
* Success: Enabled 1 of 1 plugin auto-updates.
*
* @param string[] $args Positional arguments.
* @param array{all?: bool, 'disabled-only'?: bool} $assoc_args Associative arguments.
*/
public function enable( $args, $assoc_args ) {
$all = Utils\get_flag_value( $assoc_args, 'all', false );
Expand Down
121 changes: 86 additions & 35 deletions src/Plugin_Command.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use WP_CLI\CommandWithUpgrade;
use WP_CLI\ParsePluginNameInput;
use WP_CLI\Utils;
use WP_CLI\WpOrgApi;
Expand Down Expand Up @@ -41,9 +42,11 @@
* Success: Installed 1 of 1 plugins.
*
* @package wp-cli
*
* @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
* @extends CommandWithUpgrade<string,>
*/
class Plugin_Command extends \WP_CLI\CommandWithUpgrade {

class Plugin_Command extends CommandWithUpgrade {
use ParsePluginNameInput;

protected $item_type = 'plugin';
Expand All @@ -66,13 +69,6 @@
'auto_update',
);

/**
* Plugin fetcher instance.
*
* @var \WP_CLI\Fetchers\Plugin
*/
protected $fetcher;

public function __construct() {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
Expand Down Expand Up @@ -209,6 +205,9 @@
}

protected function status_single( $args ) {
/**
* @var object{name: string, file: string} $plugin
*/
$plugin = $this->fetcher->get_check( $args[0] );
$file = $plugin->file;

Expand Down Expand Up @@ -345,11 +344,18 @@
* Plugin 'bbpress' network activated.
* Plugin 'buddypress' network activated.
* Success: Activated 2 of 2 plugins.
*
* @param array $args
* @param array $assoc_args
*/
public function activate( $args, $assoc_args = array() ) {
public function activate( $args, $assoc_args = [] ) {
$network_wide = Utils\get_flag_value( $assoc_args, 'network', false );
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', '' );

/**
* @var string $all_exclude
*/

$args = $this->check_optional_args_and_all( $args, $all, 'activate', $all_exclude );
if ( ! $args ) {
Expand All @@ -358,7 +364,11 @@

$successes = 0;
$errors = 0;
$plugins = $this->fetcher->get_many( $args );

/**
* @var array<object{name: string, file: string}> $plugins
*/
$plugins = $this->fetcher->get_many( $args );
if ( count( $plugins ) < count( $args ) ) {
$errors = count( $args ) - count( $plugins );
}
Expand Down Expand Up @@ -387,7 +397,7 @@

if ( is_wp_error( $result ) ) {
$message = $result->get_error_message();
$message = preg_replace( '/<a\s[^>]+>.*<\/a>/im', '', $message );
$message = (string) preg_replace( '/<a\s[^>]+>.*<\/a>/im', '', $message );
$message = wp_strip_all_tags( $message );
$message = str_replace( 'Error: ', '', $message );
WP_CLI::warning( "Failed to activate plugin. {$message}" );
Expand Down Expand Up @@ -437,10 +447,14 @@
* Plugin 'ninja-forms' deactivated.
* Success: Deactivated 2 of 2 plugins.
*/
public function deactivate( $args, $assoc_args = array() ) {
public function deactivate( $args, $assoc_args = [] ) {
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );
$disable_all = Utils\get_flag_value( $assoc_args, 'all' );
$disable_all_exclude = Utils\get_flag_value( $assoc_args, 'exclude' );
$disable_all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', '' );

/**
* @var string $disable_all_exclude
*/

$args = $this->check_optional_args_and_all( $args, $disable_all, 'deactivate', $disable_all_exclude );
if ( ! $args ) {
Expand Down Expand Up @@ -530,7 +544,7 @@
* Plugin 'akismet' activated.
* Success: Toggled 1 of 1 plugins.
*/
public function toggle( $args, $assoc_args = array() ) {
public function toggle( $args, $assoc_args ) {
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );

$successes = 0;
Expand Down Expand Up @@ -574,6 +588,9 @@
$path = untrailingslashit( WP_PLUGIN_DIR );

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

Expand All @@ -591,6 +608,9 @@
list($wp_core_version) = explode( '-', $wp_version );
$wp_core_version = implode( '.', array_slice( explode( '.', $wp_core_version ), 0, 2 ) );

/**
* @var \WP_Error|PluginInformation $api
*/
$api = plugins_api( 'plugin_information', array( 'slug' => $slug ) );

if ( is_wp_error( $api ) ) {
Expand Down Expand Up @@ -755,6 +775,9 @@
$auto_updates = [];
}

/**
* @var string[] $recently_active
*/
$recently_active = is_network_admin() ? get_site_option( 'recently_activated' ) : get_option( 'recently_activated' );

if ( false === $recently_active ) {
Expand All @@ -763,10 +786,11 @@

foreach ( $this->get_all_plugins() as $file => $details ) {
$all_update_info = $this->get_update_info();
$update_info = ( isset( $all_update_info->response[ $file ] ) && null !== $all_update_info->response[ $file ] ) ? (array) $all_update_info->response[ $file ] : null;
$name = Utils\get_plugin_name( $file );
$wporg_info = $this->get_wporg_data( $name );
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );
// @phpstan-ignore notIdentical.alwaysTrue
$update_info = ( isset( $all_update_info->response[ $file ] ) && null !== $all_update_info->response[ $file ] ) ? (array) $all_update_info->response[ $file ] : null;
$name = Utils\get_plugin_name( $file );
$wporg_info = $this->get_wporg_data( $name );
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );

if ( ! isset( $duplicate_names[ $name ] ) ) {
$duplicate_names[ $name ] = array();
Expand Down Expand Up @@ -875,7 +899,7 @@

if ( isset( $plugin_update_info->requires ) && version_compare( $wp_version, $requires, '>=' ) ) {
$reason = "This update requires WordPress version $plugin_update_info->requires, but the version installed is $wp_version.";
} elseif ( ! isset( $update_info['package'] ) ) {
} elseif ( ! isset( $plugin_update_info->package ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$update_info does not exist at this point.

Introduced in #451

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ FYI @mrsdizzie

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♀️ yes that is a typo since these are similarly named. I should set up PHPStan (!)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thing that I am working on a unified setup :-)

$reason = 'Update file not provided. Contact author for more details';
} else {
$reason = 'Update not available';
Expand Down Expand Up @@ -904,7 +928,7 @@
*
* @param string $plugin_name The plugin slug.
*
* @return string The status of the plugin, includes the last update date.
* @return array{status: string, last_updated: string|false, status?: string, last_updated?: string} The status of the plugin, includes the last update date.
*/
protected function get_wporg_data( $plugin_name ) {
$data = [
Expand Down Expand Up @@ -947,10 +971,12 @@
$r_body = wp_remote_retrieve_body( $request );
if ( strpos( $r_body, 'pubDate' ) !== false ) {
// Very raw check, not validating the format or anything else.
$xml = simplexml_load_string( $r_body );
$xml_pub_date = $xml->xpath( '//pubDate' );
if ( $xml_pub_date ) {
$data['last_updated'] = wp_date( 'Y-m-d', (string) strtotime( $xml_pub_date[0] ) );
$xml = simplexml_load_string( $r_body );
if ( false !== $xml ) {
$xml_pub_date = $xml->xpath( '//pubDate' );
if ( $xml_pub_date ) {
$data['last_updated'] = wp_date( 'Y-m-d', strtotime( $xml_pub_date[0] ) ?: null );
}
}
}

Expand Down Expand Up @@ -1115,6 +1141,9 @@
'status',
);

/**
* @var object{name: string, file: string} $plugin
*/
$plugin = $this->fetcher->get_check( $args[0] );
$file = $plugin->file;

Expand Down Expand Up @@ -1173,11 +1202,14 @@
* Uninstalled and deleted 'tinymce-templates' plugin.
* Success: Uninstalled 2 of 2 plugins.
*/
public function uninstall( $args, $assoc_args = array() ) {

public function uninstall( $args, $assoc_args = [] ) {
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', false );

/**
* @var string $all_exclude
*/

// Check if plugin names or --all is passed.
$args = $this->check_optional_args_and_all( $args, $all, 'uninstall', $all_exclude );
if ( ! $args ) {
Expand Down Expand Up @@ -1222,6 +1254,9 @@
if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) {
$translations = $plugin_translations[ $plugin_slug ];

/**
* @var \WP_Filesystem_Base $wp_filesystem
*/
global $wp_filesystem;
require_once ABSPATH . '/wp-admin/includes/file.php';
WP_Filesystem();
Expand All @@ -1233,7 +1268,11 @@

$json_translation_files = glob( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '-*.json' );
if ( $json_translation_files ) {
array_map( array( $wp_filesystem, 'delete' ), $json_translation_files );
/**
* @var callable $callback
*/
$callback = [ $wp_filesystem, 'delete' ];
array_map( $callback, $json_translation_files );

Check warning on line 1275 in src/Plugin_Command.php

View check run for this annotation

Codecov / codecov/patch

src/Plugin_Command.php#L1274-L1275

Added lines #L1274 - L1275 were not covered by tests
}
}
}
Expand All @@ -1257,6 +1296,10 @@
// Remove deleted plugins from the plugin updates list.
$current = get_site_transient( $this->upgrade_transient );
if ( $current ) {
/**
* @var object{response: array<string, mixed>, checked: array<string, mixed>}&\stdClass $current
*/

// Don't remove the plugins that weren't deleted.
$deleted = array_diff( $deleted_plugin_files, $delete_errors );

Expand Down Expand Up @@ -1292,7 +1335,7 @@
*
* @subcommand is-installed
*/
public function is_installed( $args, $assoc_args = array() ) {
public function is_installed( $args, $assoc_args ) {
if ( $this->fetcher->get( $args[0] ) ) {
WP_CLI::halt( 0 );
} else {
Expand Down Expand Up @@ -1322,7 +1365,7 @@
*
* @subcommand is-active
*/
public function is_active( $args, $assoc_args = array() ) {
public function is_active( $args, $assoc_args ) {
$network_wide = Utils\get_flag_value( $assoc_args, 'network' );

$plugin = $this->fetcher->get( $args[0] );
Expand Down Expand Up @@ -1366,10 +1409,14 @@
* Deleted 'tinymce-templates' plugin.
* Success: Deleted 2 of 2 plugins.
*/
public function delete( $args, $assoc_args = array() ) {
public function delete( $args, $assoc_args ) {
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$all_exclude = Utils\get_flag_value( $assoc_args, 'exclude', false );

/**
* @var string $all_exclude
*/

// Check if plugin names or --all is passed.
$args = $this->check_optional_args_and_all( $args, $all, 'delete', $all_exclude );
if ( ! $args ) {
Expand Down Expand Up @@ -1505,7 +1552,11 @@
* @subcommand list
*/
public function list_( $_, $assoc_args ) {
/**
* @var string $fields
*/
$fields = Utils\get_flag_value( $assoc_args, 'fields' );

if ( ! empty( $fields ) ) {
$fields = explode( ',', $fields );
$this->check_wporg['status'] = in_array( 'wporg_status', $fields, true );
Expand Down Expand Up @@ -1578,7 +1629,7 @@
/**
* Gets the details of a plugin.
*
* @param object
* @param string $file Plugin file name.
* @return array
*/
private function get_details( $file ) {
Expand All @@ -1591,8 +1642,8 @@
/**
* Performs deletion of plugin files
*
* @param $plugin - Plugin fetcher object (name, file)
* @return bool - If plugin was deleted
* @param $plugin Plugin fetcher object (name, file)
* @return bool Whether plugin was deleted
*/
private function delete_plugin( $plugin ) {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
Expand Down
3 changes: 3 additions & 0 deletions src/Theme_AutoUpdates_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
*/
class Theme_AutoUpdates_Command {

/**
* @use ParseThemeNameInput<\WP_Theme>
*/
use ParseThemeNameInput;

/**
Expand Down
Loading