Skip to content

Commit b5df0f3

Browse files
committed
PHPStan level 9
1 parent c821b92 commit b5df0f3

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
},
1717
"require-dev": {
1818
"wp-cli/entity-command": "^1.3 || ^2",
19-
"wp-cli/wp-cli-tests": "^4"
19+
"wp-cli/wp-cli-tests": "dev-add/phpstan-enhancements"
2020
},
2121
"config": {
2222
"process-timeout": 7200,
2323
"sort-packages": true,
2424
"allow-plugins": {
2525
"dealerdirect/phpcodesniffer-composer-installer": true,
26-
"johnpbloch/wordpress-core-installer": true
26+
"johnpbloch/wordpress-core-installer": true,
27+
"phpstan/extension-installer": true
2728
},
2829
"lock": false
2930
},
@@ -67,6 +68,7 @@
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",

phpstan.neon.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- db-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+
- tests/phpstan/scan-files.php
12+
treatPhpDocTypesAsCertain: false
13+
dynamicConstantNames:
14+
- DB_HOST
15+
- DB_NAME
16+
- DB_USER
17+
- DB_PASSWORD
18+
- DB_CHARSET
19+
- DB_COLLATE
20+
ignoreErrors:
21+
- identifier: missingType.iterableValue
22+
- identifier: missingType.parameter
23+
- identifier: missingType.return

src/DB_Command.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ public function cli( $_, $assoc_args ) {
409409
}
410410

411411
WP_CLI::debug( 'Associative arguments: ' . json_encode( $assoc_args ), 'db' );
412-
self::run( $command, $assoc_args, null, true );
412+
self::run( $command, $assoc_args, false, true );
413413
}
414414

415415
/**
@@ -634,7 +634,7 @@ public function export( $args, $assoc_args ) {
634634
$result_file = $args[0];
635635
} else {
636636
// phpcs:ignore WordPress.WP.AlternativeFunctions.rand_mt_rand -- WordPress is not loaded.
637-
$hash = substr( md5( mt_rand() ), 0, 7 );
637+
$hash = substr( md5( (string) mt_rand() ), 0, 7 );
638638
$result_file = sprintf( '%s-%s-%s.sql', DB_NAME, date( 'Y-m-d' ), $hash ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
639639

640640
}
@@ -709,7 +709,7 @@ public function export( $args, $assoc_args ) {
709709
}
710710
}
711711

712-
$escaped_command = call_user_func_array( '\WP_CLI\Utils\esc_cmd', array_merge( [ $command ], $command_esc_args ) );
712+
$escaped_command = \WP_CLI\Utils\esc_cmd( $command, $command_esc_args );
713713

714714
// Remove parameters not needed for SQL run.
715715
unset( $assoc_args['porcelain'] );
@@ -727,7 +727,7 @@ public function export( $args, $assoc_args ) {
727727
/**
728728
* Get the current character set of the posts table.
729729
*
730-
* @param array Associative array of associative arguments.
730+
* @param array $assoc_args Associative arguments.
731731
* @return string Posts table character set.
732732
*/
733733
private function get_posts_table_charset( $assoc_args ) {
@@ -891,6 +891,9 @@ public function import( $args, $assoc_args ) {
891891
* Success: Exported to wordpress_dbase.sql
892892
*
893893
* @when after_wp_load
894+
*
895+
* @param array<string> $args Positional arguments.
896+
* @param array{scope?: string, network?: bool, 'all-tables-with-prefix'?: bool, 'all-tables'?: bool, format: string} $assoc_args Associative arguments.
894897
*/
895898
public function tables( $args, $assoc_args ) {
896899

@@ -1042,6 +1045,9 @@ public function tables( $args, $assoc_args ) {
10421045
* 6
10431046
*
10441047
* @when after_wp_load
1048+
*
1049+
* @param array $args Positional arguments. Unused.
1050+
* @param array{size_format?: string, tables?: bool, 'human-readable'?: bool, format?: string, scope?: string, network?: bool, decimals?: string, 'all-tables-with-prefix'?: bool, 'all-tables'?: bool, order: string, orderby: string} $assoc_args Associative arguments.
10451051
*/
10461052
public function size( $args, $assoc_args ) {
10471053
global $wpdb;
@@ -1132,7 +1138,7 @@ public function size( $args, $assoc_args ) {
11321138
// phpcs:enable
11331139

11341140
if ( $human_readable ) {
1135-
$size_key = floor( log( $row['Size'] ) / log( 1000 ) );
1141+
$size_key = floor( log( (float) $row['Size'] ) / log( 1000 ) );
11361142
$sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
11371143

11381144
$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0];
@@ -1184,7 +1190,7 @@ public function size( $args, $assoc_args ) {
11841190
}
11851191
$size_format_display = preg_replace( '/IB$/u', 'iB', strtoupper( $size_format ) );
11861192

1187-
$decimals = Utils\get_flag_value( $assoc_args, 'decimals', 0 );
1193+
$decimals = (int) Utils\get_flag_value( $assoc_args, 'decimals', 0 );
11881194
$rows[ $index ]['Size'] = round( (int) $row['Bytes'] / $divisor, $decimals ) . ' ' . $size_format_display;
11891195
}
11901196
}
@@ -1428,6 +1434,10 @@ public function search( $args, $assoc_args ) {
14281434
$after_context = Utils\get_flag_value( $assoc_args, 'after_context', 40 );
14291435
$after_context = '' === $after_context ? $after_context : (int) $after_context;
14301436

1437+
$default_regex_delimiter = false;
1438+
$regex_flags = false;
1439+
$regex_delimiter = '';
1440+
14311441
$regex = Utils\get_flag_value( $assoc_args, 'regex', false );
14321442
if ( false !== $regex ) {
14331443
$regex_flags = Utils\get_flag_value( $assoc_args, 'regex-flags', false );
@@ -1857,7 +1867,7 @@ private static function get_dbuser_dbpass_args( $assoc_args ) {
18571867
* Gets the column names of a db table differentiated into key columns and text columns and all columns.
18581868
*
18591869
* @param string $table The table name.
1860-
* @return array A 3 element array consisting of an array of primary key column names, an array of text column names, and an array containing all column names.
1870+
* @return array{0: string[], 1: string[], 2: string[]} A 3 element array consisting of an array of primary key column names, an array of text column names, and an array containing all column names.
18611871
*/
18621872
private static function get_columns( $table ) {
18631873
global $wpdb;
@@ -1890,7 +1900,7 @@ private static function get_columns( $table ) {
18901900
/**
18911901
* Determines whether a column is considered text or not.
18921902
*
1893-
* @param string Column type.
1903+
* @param string $type Column type.
18941904
* @return bool True if text column, false otherwise.
18951905
*/
18961906
private static function is_text_col( $type ) {
@@ -1909,6 +1919,8 @@ private static function is_text_col( $type ) {
19091919
*
19101920
* @param string|array $idents A single identifier or an array of identifiers.
19111921
* @return string|array An escaped string if given a string, or an array of escaped strings if given an array of strings.
1922+
*
1923+
* @phpstan-return ($idents is string ? string : array)
19121924
*/
19131925
private static function esc_sql_ident( $idents ) {
19141926
$backtick = static function ( $v ) {
@@ -2155,11 +2167,6 @@ protected function get_current_sql_modes( $assoc_args ) {
21552167
// Make sure the provided arguments don't interfere with the expected
21562168
// output here.
21572169
$args = [];
2158-
foreach ( [] as $arg ) {
2159-
if ( isset( $assoc_args[ $arg ] ) ) {
2160-
$args[ $arg ] = $assoc_args[ $arg ];
2161-
}
2162-
}
21632170

21642171
if ( null === $modes ) {
21652172
$modes = [];
@@ -2183,17 +2190,14 @@ protected function get_current_sql_modes( $assoc_args ) {
21832190
}
21842191

21852192
if ( ! empty( $stdout ) ) {
2193+
$lines = preg_split( "/\r\n|\n|\r|,/", $stdout );
21862194
$modes = array_filter(
21872195
array_map(
21882196
'trim',
2189-
preg_split( "/\r\n|\n|\r|,/", $stdout )
2197+
$lines ? $lines : []
21902198
)
21912199
);
21922200
}
2193-
2194-
if ( false === $modes ) {
2195-
$modes = [];
2196-
}
21972201
}
21982202

21992203
return $modes;

tests/phpstan/scan-files.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace {
4+
define( 'DB_HOST', '' );
5+
define( 'DB_NAME', '' );
6+
define( 'DB_USER', '' );
7+
define( 'DB_PASSWORD', '' );
8+
define( 'DB_CHARSET', '' );
9+
define( 'DB_COLLATE', '' );
10+
}

0 commit comments

Comments
 (0)