Skip to content

Commit 12a3ded

Browse files
committed
PHPStan level 9
1 parent 44f88c7 commit 12a3ded

4 files changed

Lines changed: 62 additions & 28 deletions

File tree

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
},
2323
"require-dev": {
2424
"wp-cli/db-command": "^1.3 || ^2",
25-
"wp-cli/wp-cli-tests": "^4.2.8"
25+
"wp-cli/wp-cli-tests": "dev-main"
2626
},
2727
"config": {
2828
"process-timeout": 7200,
2929
"sort-packages": true,
3030
"allow-plugins": {
3131
"dealerdirect/phpcodesniffer-composer-installer": true,
32-
"johnpbloch/wordpress-core-installer": true
32+
"johnpbloch/wordpress-core-installer": true,
33+
"phpstan/extension-installer": true
3334
},
3435
"lock": false
3536
},
@@ -67,12 +68,14 @@
6768
"behat-rerun": "rerun-behat-tests",
6869
"lint": "run-linter-tests",
6970
"phpcs": "run-phpcs-tests",
71+
"phpstan": "run-phpstan-tests",
7072
"phpcbf": "run-phpcbf-cleanup",
7173
"phpunit": "run-php-unit-tests",
7274
"prepare-tests": "install-package-tests",
7375
"test": [
7476
"@lint",
7577
"@phpcs",
78+
"@phpstan",
7679
"@phpunit",
7780
"@behat"
7881
]

phpstan.neon.dist

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

src/Config_Command.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ public function create( $_, $assoc_args ) {
215215
if ( ! Utils\get_flag_value( $assoc_args, 'skip-check' ) ) {
216216
// phpcs:disable WordPress.DB.RestrictedFunctions
217217
$mysql = mysqli_init();
218+
219+
if ( ! $mysql ) {
220+
WP_CLI::error( 'Database connection error. Could not initialize MySQLi.' );
221+
}
222+
218223
mysqli_report( MYSQLI_REPORT_STRICT );
219224
try {
220225
// Accept similar format to one used by parse_db_host() e.g. 'localhost:/tmp/mysql.sock'
@@ -259,12 +264,6 @@ public function create( $_, $assoc_args ) {
259264
}
260265
}
261266

262-
if ( Utils\wp_version_compare( '4.0', '<' ) ) {
263-
$assoc_args['add-wplang'] = true;
264-
} else {
265-
$assoc_args['add-wplang'] = false;
266-
}
267-
268267
foreach ( $assoc_args as $key => $value ) {
269268
$assoc_args[ $key ] = $this->escape_config_value( $key, $value );
270269
}
@@ -319,7 +318,7 @@ private function config_file_already_exist_error( $wp_config_file_name ) {
319318
public function edit( $_, $assoc_args ) {
320319
$path = $this->get_config_path( $assoc_args );
321320
$wp_config_file_name = basename( $path );
322-
$contents = file_get_contents( $path );
321+
$contents = (string) file_get_contents( $path );
323322
$r = Utils\launch_editor_for_input( $contents, $wp_config_file_name, 'php' );
324323
if ( false === $r ) {
325324
WP_CLI::warning( "No changes made to {$wp_config_file_name}, aborted." );
@@ -415,7 +414,7 @@ public function path() {
415414
public function list_( $args, $assoc_args ) {
416415
$path = $this->get_config_path( $assoc_args );
417416
$wp_config_file_name = basename( $path );
418-
$strict = Utils\get_flag_value( $assoc_args, 'strict' );
417+
$strict = (bool) Utils\get_flag_value( $assoc_args, 'strict' );
419418
if ( $strict && empty( $args ) ) {
420419
WP_CLI::error( 'The --strict option can only be used in combination with a filter.' );
421420
}
@@ -569,7 +568,9 @@ private static function get_wp_config_vars( $wp_config_path = '' ) {
569568
}
570569
}
571570

572-
unset( $wp_config_vars[ $name_backup ] );
571+
if ( isset( $name_backup ) ) {
572+
unset( $wp_config_vars[ $name_backup ] );
573+
}
573574
$wp_config_vars = array_values( $wp_config_vars );
574575
$wp_config_includes = array_diff( get_included_files(), $wp_cli_original_includes );
575576
$wp_config_includes_array = [];
@@ -648,7 +649,11 @@ public function set( $args, $assoc_args ) {
648649
$path = $this->get_config_path( $assoc_args );
649650
$wp_config_file_name = basename( $path );
650651
list( $name, $value ) = $args;
651-
$type = Utils\get_flag_value( $assoc_args, 'type' );
652+
653+
/**
654+
* @var string $type
655+
*/
656+
$type = Utils\get_flag_value( $assoc_args, 'type' );
652657

653658
$options = [];
654659

@@ -661,8 +666,14 @@ public function set( $args, $assoc_args ) {
661666
];
662667

663668
foreach ( $option_flags as $option => $default ) {
669+
/**
670+
* @var string|null $option_value
671+
*/
664672
$option_value = Utils\get_flag_value( $assoc_args, $option, $default );
665673
if ( null !== $option_value ) {
674+
/**
675+
* @var array<string, string> $options
676+
*/
666677
$options[ $option ] = $option_value;
667678
if ( 'separator' === $option ) {
668679
$options['separator'] = $this->parse_separator( $options['separator'] );
@@ -752,7 +763,11 @@ public function delete( $args, $assoc_args ) {
752763
$path = $this->get_config_path( $assoc_args );
753764
$wp_config_file_name = basename( $path );
754765
list( $name ) = $args;
755-
$type = Utils\get_flag_value( $assoc_args, 'type' );
766+
767+
/**
768+
* @var string $type
769+
*/
770+
$type = Utils\get_flag_value( $assoc_args, 'type' );
756771

757772
try {
758773
$config_transformer = new WPConfigTransformer( $path );
@@ -819,7 +834,11 @@ public function has( $args, $assoc_args ) {
819834
$path = $this->get_config_path( $assoc_args );
820835
$wp_config_file_name = basename( $path );
821836
list( $name ) = $args;
822-
$type = Utils\get_flag_value( $assoc_args, 'type' );
837+
838+
/**
839+
* @var string $type
840+
*/
841+
$type = Utils\get_flag_value( $assoc_args, 'type' );
823842

824843
try {
825844
$config_transformer = new WPConfigTransformer( $path, true );
@@ -836,6 +855,7 @@ public function has( $args, $assoc_args ) {
836855
} else {
837856
WP_CLI::halt( 0 );
838857
}
858+
// @phpstan-ignore deadCode.unreachable
839859
break;
840860
case 'constant':
841861
case 'variable':
@@ -993,7 +1013,7 @@ private static function fetch_remote_salts( $insecure = false ) {
9931013
}
9941014

9951015
// Adapt whitespace to adhere to WPCS.
996-
$salts = preg_replace( '/define\(\'(.*?)\'\);/', 'define( \'$1\' );', $salts );
1016+
$salts = (string) preg_replace( '/define\(\'(.*?)\'\);/', 'define( \'$1\' );', $salts );
9971017

9981018
return $salts;
9991019
}
@@ -1153,7 +1173,11 @@ protected function get_value( $assoc_args, $args ) {
11531173
$path = $this->get_config_path( $assoc_args );
11541174
$wp_config_file_name = basename( $path );
11551175
list( $name ) = $args;
1156-
$type = Utils\get_flag_value( $assoc_args, 'type' );
1176+
1177+
/**
1178+
* @var string $type
1179+
*/
1180+
$type = Utils\get_flag_value( $assoc_args, 'type' );
11571181

11581182
$value = $this->return_value(
11591183
$name,

templates/wp-config.mustache

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,6 @@ define( 'WP_CACHE_KEY_SALT', '{{wp-cache-key-salt}}' );
7272
*/
7373
$table_prefix = '{{dbprefix}}';
7474

75-
{{#add-wplang}}
76-
/**
77-
* WordPress localized language, defaults to English.
78-
*
79-
* Change this to localize WordPress. A corresponding MO file for the chosen
80-
* language must be installed to wp-content/languages. For example, install
81-
* de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
82-
* language support.
83-
*/
84-
define( 'WPLANG', '{{locale}}' );
85-
{{/add-wplang}}
86-
8775
/* Add any custom values between this line and the "stop editing" line. */
8876

8977
{{extra-php}}

0 commit comments

Comments
 (0)