Skip to content

PHPStan level 9 #166

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 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
"wp-cli/db-command": "^1.3 || ^2",
"wp-cli/entity-command": "^1.3 || ^2",
"wp-cli/extension-command": "^1.2 || ^2",
"wp-cli/wp-cli-tests": "^4"
"wp-cli/wp-cli-tests": "dev-main"
},
"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 @@ -73,12 +74,14 @@
"behat-rerun": "rerun-behat-tests",
"lint": "run-linter-tests",
"phpcs": "run-phpcs-tests",
"phpstan": "run-phpstan-tests",
"phpcbf": "run-phpcbf-cleanup",
"phpunit": "run-php-unit-tests",
"prepare-tests": "install-package-tests",
"test": [
"@lint",
"@phpcs",
"@phpstan",
"@phpunit",
"@behat"
]
Expand Down
20 changes: 20 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
parameters:
level: 9
paths:
- src
- language-command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
- vendor/wp-cli/wp-cli-tests
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
dynamicConstantNames:
- WP_DEBUG
- WP_DEBUG_LOG
- WP_DEBUG_DISPLAY
ignoreErrors:
- identifier: missingType.iterableValue
- identifier: missingType.property
- identifier: missingType.parameter
- identifier: missingType.return
17 changes: 13 additions & 4 deletions src/Theme_Language_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,11 @@ private function install_one( $args, $assoc_args ) {
*/
private function install_many( $args, $assoc_args ) {
$language_codes = (array) $args;
$themes = wp_get_themes();

/**
* @var \WP_Theme[] $themes
*/
$themes = wp_get_themes();

if ( empty( $assoc_args['format'] ) ) {
$assoc_args['format'] = 'table';
Expand Down Expand Up @@ -343,22 +347,27 @@ private function install_many( $args, $assoc_args ) {

$available = $this->get_installed_languages( $theme_name );

/**
* @var string $display_name
*/
$display_name = $theme_details['Name'];

foreach ( $language_codes as $language_code ) {
$result = [
'name' => $theme_name,
'locale' => $language_code,
];

if ( in_array( $language_code, $available, true ) ) {
\WP_CLI::log( "Language '{$language_code}' for '{$theme_details['Name']}' already installed." );
\WP_CLI::log( "Language '{$language_code}' for '{$display_name}' already installed." );
$result['status'] = 'already installed';
++$skips;
} else {
$response = $this->download_language_pack( $language_code, $theme_name );

if ( is_wp_error( $response ) ) {
\WP_CLI::warning( $response );
\WP_CLI::log( "Language '{$language_code}' for '{$theme_details['Name']}' not installed." );
\WP_CLI::log( "Language '{$language_code}' for '{$display_name}' not installed." );

if ( 'not_found' === $response->get_error_code() ) {
$result['status'] = 'not available';
Expand All @@ -368,7 +377,7 @@ private function install_many( $args, $assoc_args ) {
++$errors;
}
} else {
\WP_CLI::log( "Language '{$language_code}' for '{$theme_details['Name']}' installed." );
\WP_CLI::log( "Language '{$language_code}' for '{$display_name}' installed." );
$result['status'] = 'installed';
++$successes;
}
Expand Down
42 changes: 36 additions & 6 deletions src/WP_CLI/CommandWithTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@ public function update( $args, $assoc_args ) {
$name = 'WordPress'; // Core.

if ( 'plugin' === $update->type ) {
$plugins = get_plugins( '/' . $update->slug );
/**
* @var array<array{Name: string}> $plugins
*/
$plugins = get_plugins( '/' . $update->slug );

/**
* @var array{Name: string}> $plugin_data
*/
$plugin_data = array_shift( $plugins );
$name = $plugin_data['Name'];
} elseif ( 'theme' === $update->type ) {
$theme_data = wp_get_theme( $update->slug );
$name = $theme_data['Name'];
/**
* @var string $name
*/
$name = $theme_data['Name'];
}

// Gets the translation data.
Expand Down Expand Up @@ -97,7 +107,12 @@ public function update( $args, $assoc_args ) {
foreach ( $available_updates as $update ) {
WP_CLI::line( "Updating '{$update->Language}' translation for {$update->Name} {$update->Version}..." );

$result = Utils\get_upgrader( $upgrader )->upgrade( $update );
/**
* @var \WP_CLI\LanguagePackUpgrader $upgrader_instance
*/
$upgrader_instance = Utils\get_upgrader( $upgrader );

$result = $upgrader_instance->upgrade( $update );

$results[] = $result;
}
Expand Down Expand Up @@ -181,7 +196,11 @@ protected function get_translation_updates() {
break;
}

$updates = array();
$updates = array();

/**
* @var object{translations: array} $transient
*/
$transient = get_site_transient( $transient );

if ( empty( $transient->translations ) ) {
Expand Down Expand Up @@ -218,7 +237,8 @@ protected function download_language_pack( $download, $slug = null ) {
if ( ! $translation_to_load ) {
return new \WP_Error( 'not_found', $slug ? "Language '{$download}' for '{$slug}' not available." : "Language '{$download}' not available." );
}
$translation = (object) $translation;

$translation = (object) $translation_to_load;

$translation->type = rtrim( $this->obj_type, 's' );

Expand All @@ -228,7 +248,15 @@ protected function download_language_pack( $download, $slug = null ) {
}

$upgrader = 'WP_CLI\\LanguagePackUpgrader';
$result = Utils\get_upgrader( $upgrader )->upgrade( $translation, array( 'clear_update_cache' => false ) );

/**
* @var \WP_CLI\LanguagePackUpgrader $upgrader_instance
*/
$upgrader_instance = Utils\get_upgrader( $upgrader );

// Incorrect docblock in WordPress core.
// @phpstan-ignore argument.type
$result = $upgrader_instance->upgrade( $translation, array( 'clear_update_cache' => false ) );

if ( is_wp_error( $result ) ) {
return $result;
Expand Down Expand Up @@ -268,6 +296,8 @@ protected function get_all_languages( $slug = null ) {
require ABSPATH . WPINC . '/version.php'; // Include an unmodified $wp_version

$args = array(
// False positive, because it's defined in version.php
// @phpstan-ignore variable.undefined
'version' => $wp_version,
);

Expand Down
23 changes: 17 additions & 6 deletions src/WP_CLI/LanguagePackUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
* @package wp-cli
*/
class LanguagePackUpgrader extends \Language_Pack_Upgrader {
/**
* The upgrader skin being used.
*
* @var \Language_Pack_Upgrader_Skin $skin
*/
public $skin = null;

/**
* Initialize the upgrade strings.
*
Expand Down Expand Up @@ -57,10 +64,10 @@ public function download_package( $package, $check_signatures = false, $hook_ext
* @since 3.7.0
* @since 5.5.0 Added the `$hook_extra` parameter.
*
* @param bool $reply Whether to bail without returning the package. Default is false.
* @param string $package The package file name.
* @param \WP_Upgrader $this The WP_Upgrader instance.
* @param array $hook_extra Extra arguments passed to hooked filters.
* @param false|string|\WP_Error $reply Whether to bail without returning the package. Default is false.
* @param string $package The package file name.
* @param \WP_Upgrader $upgrader The WP_Upgrader instance.
* @param array $hook_extra Extra arguments passed to hooked filters.
*
* @phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using WP native hook.
*/
Expand Down Expand Up @@ -90,8 +97,12 @@ public function download_package( $package, $check_signatures = false, $hook_ext

$temp = \WP_CLI\Utils\get_temp_dir() . uniqid( 'wp_' ) . '.' . $ext;

$cache = WP_CLI::get_cache();
$cache_key = "translation/{$type}-{$slug}-{$version}-{$language}-{$updated}.{$ext}";
$cache = WP_CLI::get_cache();
$cache_key = "translation/{$type}-{$slug}-{$version}-{$language}-{$updated}.{$ext}";

/**
* @var string|false $cache_file
*/
$cache_file = $cache->has( $cache_key );

if ( $cache_file ) {
Expand Down