Skip to content

Commit 8ba637a

Browse files
authored
Merge pull request #197 from wp-cli/add/phpstan
2 parents e40397b + 12a3ded commit 8ba637a

6 files changed

Lines changed: 62 additions & 165 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
]

features/config-create.feature

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -227,56 +227,6 @@ Feature: Create a wp-config file
227227
define( 'AUTH_SALT',
228228
"""
229229
230-
@less-than-php-7.0
231-
Scenario: Configure with salts fetched from WordPress.org
232-
Given an empty directory
233-
And WP files
234-
235-
When I run `wp config create {CORE_CONFIG_SETTINGS}`
236-
Then the wp-config.php file should contain:
237-
"""
238-
define( 'AUTH_SALT',
239-
"""
240-
241-
Scenario: Define WPLANG when running WP < 4.0
242-
Given an empty directory
243-
And I run `wp core download --version=3.9 --force`
244-
245-
When I run `wp config create {CORE_CONFIG_SETTINGS} --skip-check`
246-
Then the wp-config.php file should contain:
247-
"""
248-
define( 'WPLANG', '' );
249-
"""
250-
251-
When I try `wp config create {CORE_CONFIG_SETTINGS}`
252-
Then the return code should be 1
253-
And STDERR should contain:
254-
"""
255-
Error: The 'wp-config.php' file already exists.
256-
"""
257-
258-
When I run `wp config create {CORE_CONFIG_SETTINGS} --skip-check --locale=ja --force`
259-
Then the return code should be 0
260-
And STDOUT should contain:
261-
"""
262-
Success: Generated 'wp-config.php' file.
263-
"""
264-
And the wp-config.php file should contain:
265-
"""
266-
define( 'WPLANG', 'ja' );
267-
"""
268-
269-
When I run `wp config create {CORE_CONFIG_SETTINGS} --skip-check --config-file=wp-custom-config.php --locale=ja --force`
270-
Then the return code should be 0
271-
And STDOUT should contain:
272-
"""
273-
Success: Generated 'wp-custom-config.php' file.
274-
"""
275-
And the wp-custom-config.php file should contain:
276-
"""
277-
define( 'WPLANG', 'ja' );
278-
"""
279-
280230
Scenario: Values are properly escaped to avoid creating invalid config files
281231
Given an empty directory
282232
And WP files

features/config-shuffle-salts.feature

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -265,90 +265,3 @@ Feature: Refresh the salts in the wp-config.php file
265265
"""
266266
{NEW_KEY_ORIG}
267267
"""
268-
269-
@less-than-php-7.0
270-
Scenario: Force adding missing salts to shuffle fails on PHP < 7.0
271-
Given a WP install
272-
When I run `wp config shuffle-salts`
273-
Then STDOUT should contain:
274-
"""
275-
Shuffled the salt keys.
276-
"""
277-
And the wp-config.php file should contain:
278-
"""
279-
define( 'AUTH_KEY'
280-
"""
281-
And the wp-config.php file should contain:
282-
"""
283-
define( 'LOGGED_IN_SALT'
284-
"""
285-
And the wp-config.php file should not contain:
286-
"""
287-
define( 'NEW_KEY'
288-
"""
289-
290-
When I run `wp config get AUTH_KEY --type=constant`
291-
Then save STDOUT as {AUTH_KEY_ORIG}
292-
When I run `wp config get LOGGED_IN_SALT --type=constant`
293-
Then save STDOUT as {LOGGED_IN_SALT_ORIG}
294-
295-
When I try `wp config shuffle-salts AUTH_KEY NEW_KEY`
296-
Then STDOUT should contain:
297-
"""
298-
Shuffled 1 of 2 salts (1 skipped).
299-
"""
300-
And STDERR should contain:
301-
"""
302-
Warning: Could not shuffle the unknown key 'NEW_KEY'.
303-
"""
304-
And the wp-config.php file should not contain:
305-
"""
306-
{AUTH_KEY_ORIG}
307-
"""
308-
And the wp-config.php file should contain:
309-
"""
310-
{LOGGED_IN_SALT_ORIG}
311-
"""
312-
And the wp-config.php file should not contain:
313-
"""
314-
define( 'NEW_KEY'
315-
"""
316-
317-
When I run `wp config get AUTH_KEY --type=constant`
318-
Then save STDOUT as {AUTH_KEY_ORIG}
319-
320-
When I try `wp config shuffle-salts AUTH_KEY NEW_KEY --force`
321-
Then STDOUT should contain:
322-
"""
323-
Shuffled 1 of 2 salts (1 skipped).
324-
"""
325-
And STDERR should contain:
326-
"""
327-
Warning: Could not add the key 'NEW_KEY' because 'random_int()' is not supported.
328-
"""
329-
And the wp-config.php file should not contain:
330-
"""
331-
{AUTH_KEY_ORIG}
332-
"""
333-
And the wp-config.php file should contain:
334-
"""
335-
{LOGGED_IN_SALT_ORIG}
336-
"""
337-
And the wp-config.php file should not contain:
338-
"""
339-
define( 'NEW_KEY'
340-
"""
341-
342-
@less-than-php-7.0
343-
Scenario: Shuffling salts duplicate warnings on PHP < 7.0
344-
Given a WP install
345-
When I try `wp config shuffle-salts WP_CACHE_KEY_SALT NONCE_SALT`
346-
Then STDERR should contain:
347-
"""
348-
Warning: Could not shuffle the unknown key 'WP_CACHE_KEY_SALT'.
349-
"""
350-
And STDERR should not contain:
351-
"""
352-
Warning: Could not shuffle the unknown key 'WP_CACHE_KEY_SALT'.
353-
Warning: Could not shuffle the unknown key 'WP_CACHE_KEY_SALT'.
354-
"""

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)