Skip to content

Commit 31226ee

Browse files
authored
Merge pull request #174 from wp-cli/fix-phpstan
2 parents b9de844 + 5ed42b6 commit 31226ee

10 files changed

Lines changed: 51 additions & 16 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"wp-cli/wp-cli": "^2.12"
15+
"wp-cli/wp-cli": "^2.13"
1616
},
1717
"require-dev": {
1818
"wp-cli/db-command": "^1.3 || ^2",

phpcs.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,7 @@
8383
<exclude-pattern>*/src/Language_Namespace\.php$</exclude-pattern>
8484
</rule>
8585

86+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
87+
<exclude-pattern>*/tests/phpstan</exclude-pattern>
88+
</rule>
8689
</ruleset>

phpstan.neon.dist

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77
- vendor/wp-cli/wp-cli/php
88
scanFiles:
99
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
- tests/phpstan/scan-files.php
1011
treatPhpDocTypesAsCertain: false
1112
strictRules:
1213
uselessCast: true
@@ -16,7 +17,4 @@ parameters:
1617
numericOperandsInArithmeticOperators: true
1718
switchConditionsMatchingType: true
1819
ignoreErrors:
19-
- identifier: missingType.iterableValue
20-
- identifier: missingType.property
21-
- identifier: missingType.parameter
2220
- identifier: missingType.return

src/Core_Language_Command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ public function activate( $args ) {
386386
$this->activate_language( $language_code );
387387
}
388388

389+
/**
390+
* @param string $language_code
391+
*/
389392
private function activate_language( $language_code ) {
390393
$available = $this->get_installed_languages();
391394

src/Plugin_Language_Command.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,18 @@ public function install( $args, $assoc_args ) {
263263
if ( $all ) {
264264
$this->install_many( $args, $assoc_args );
265265
} else {
266+
/**
267+
* @var non-empty-list<string> $args
268+
*/
266269
$this->install_one( $args, $assoc_args );
267270
}
268271
}
269272

270273
/**
271274
* Installs translations for a plugin.
272275
*
273-
* @param array $args Runtime arguments.
274-
* @param array $assoc_args Runtime arguments.
276+
* @param non-empty-list<string> $args Positional arguments.
277+
* @param array{all?: bool, format: string} $assoc_args Associative arguments.
275278
*/
276279
private function install_one( $args, $assoc_args ) {
277280
$plugin = array_shift( $args );
@@ -314,8 +317,8 @@ private function install_one( $args, $assoc_args ) {
314317
/**
315318
* Installs translations for all installed plugins.
316319
*
317-
* @param array $args Runtime arguments.
318-
* @param array $assoc_args Runtime arguments.
320+
* @param string[] $args Positional arguments.
321+
* @param array{all?: bool, format: string} $assoc_args Associative arguments.
319322
*/
320323
private function install_many( $args, $assoc_args ) {
321324
$language_codes = (array) $args;
@@ -627,7 +630,7 @@ public function update( $args, $assoc_args ) {
627630
* Uses the same filter core uses in plugins.php to determine which plugins
628631
* should be available to manage through the WP_Plugins_List_Table class.
629632
*
630-
* @return array
633+
* @return array<string, array{Name: string}>
631634
*/
632635
private function get_all_plugins() {
633636
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Using WP native hook.

src/Site_Switch_Language_Command.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class Site_Switch_Language_Command extends WP_CLI\CommandWithTranslation {
1717
* Success: Language activated.
1818
*
1919
* @throws WP_CLI\ExitException
20+
*
21+
* @param array{0: string} $args Positional arguments.
22+
* @param array<mixed> $assoc_args Associative arguments.
2023
*/
2124
public function __invoke( $args, $assoc_args ) {
2225
list( $language_code ) = $args;

src/Theme_Language_Command.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,15 +268,18 @@ public function install( $args, $assoc_args ) {
268268
if ( $all ) {
269269
$this->install_many( $args, $assoc_args );
270270
} else {
271+
/**
272+
* @var non-empty-list<string> $args
273+
*/
271274
$this->install_one( $args, $assoc_args );
272275
}
273276
}
274277

275278
/**
276279
* Installs translations for a theme.
277280
*
278-
* @param array $args Runtime arguments.
279-
* @param array $assoc_args Runtime arguments.
281+
* @param non-empty-list<string> $args Positional arguments.
282+
* @param array{all?: bool, format: string} $assoc_args Associative arguments.
280283
*/
281284
private function install_one( $args, $assoc_args ) {
282285
$theme = array_shift( $args );
@@ -318,8 +321,8 @@ private function install_one( $args, $assoc_args ) {
318321
/**
319322
* Installs translations for all installed themes.
320323
*
321-
* @param array $args Runtime arguments.
322-
* @param array $assoc_args Runtime arguments.
324+
* @param string[] $args Positional arguments.
325+
* @param array{all?: bool, format: string} $assoc_args Associative arguments.
323326
*/
324327
private function install_many( $args, $assoc_args ) {
325328
$language_codes = (array) $args;

src/WP_CLI/CommandWithTranslation.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@
1111
* @package wp-cli
1212
*/
1313
abstract class CommandWithTranslation extends WP_CLI_Command {
14+
/**
15+
* @var string
16+
*/
1417
protected $obj_type;
1518

19+
/**
20+
* @var string[]
21+
*/
1622
protected $obj_fields;
1723

1824
/**
1925
* Callback to sort array by a 'language' key.
26+
*
27+
* @param array{language: string, english_name: string, native_name: string, updated: string} $a
28+
* @param array{language: string, english_name: string, native_name: string, updated: string} $b
2029
*/
2130
protected function sort_translations_callback( $a, $b ) {
2231
return strnatcasecmp( $a['language'], $b['language'] );
@@ -66,7 +75,7 @@ public function update( $args, $assoc_args ) {
6675
$plugins = get_plugins( '/' . $update->slug );
6776

6877
/**
69-
* @var array{Name: string}> $plugin_data
78+
* @var array{Name: string} $plugin_data
7079
*/
7180
$plugin_data = array_shift( $plugins );
7281
$name = $plugin_data['Name'];
@@ -115,6 +124,8 @@ public function update( $args, $assoc_args ) {
115124
*/
116125
$upgrader_instance = Utils\get_upgrader( $upgrader );
117126

127+
// Wrong docblock in core.
128+
// @phpstan-ignore argument.type
118129
$result = $upgrader_instance->upgrade( $update );
119130

120131
$results[] = $result;
@@ -157,7 +168,7 @@ public function update( $args, $assoc_args ) {
157168
*
158169
* @see wp_get_translation_updates()
159170
*
160-
* @return array
171+
* @return array<object{type: string, slug: string, language: string}&\stdClass>
161172
*/
162173
protected function get_translation_updates() {
163174
$available = $this->get_installed_languages();
@@ -202,7 +213,7 @@ protected function get_translation_updates() {
202213
$updates = array();
203214

204215
/**
205-
* @var object{translations: array} $transient
216+
* @var object{translations: list<array{type: string, slug: string, language: string}>} $transient
206217
*/
207218
$transient = get_site_transient( $transient );
208219

@@ -214,6 +225,10 @@ protected function get_translation_updates() {
214225
$updates[] = (object) $translation;
215226
}
216227

228+
/**
229+
* @var array<object{type: string, slug: string, language: string}&\stdClass> $updates
230+
*/
231+
217232
return $updates;
218233
}
219234

@@ -376,6 +391,8 @@ protected function get_all_languages( $slug = null ) {
376391
*
377392
* @param array $assoc_args Parameters passed to command. Determines formatting.
378393
* @return Formatter
394+
*
395+
* @phpstan-ignore missingType.iterableValue
379396
*/
380397
protected function get_formatter( &$assoc_args ) {
381398
return new Formatter( $assoc_args, $this->obj_fields, $this->obj_type );

src/WP_CLI/LanguagePackUpgrader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public function generic_strings() {
5757
* @param bool $check_signatures Whether to validate file signatures. Default false.
5858
* @param array $hook_extra Extra arguments to pass to the filter hooks. Default empty array.
5959
* @return string|\WP_Error The full path to the downloaded package file, or a WP_Error object.
60+
*
61+
* @phpstan-ignore missingType.iterableValue
6062
*/
6163
public function download_package( $package, $check_signatures = false, $hook_extra = [] ) {
6264

tests/phpstan/scan-files.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
define( 'WPINC', '' );

0 commit comments

Comments
 (0)