Skip to content

Commit cf0dba1

Browse files
authored
Merge pull request #286 from wp-cli/try/phpstan
2 parents c821b92 + 16e7c12 commit cf0dba1

File tree

5 files changed

+66
-24
lines changed

5 files changed

+66
-24
lines changed

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@
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/entity-command": "^1.3 || ^2",
19-
"wp-cli/wp-cli-tests": "^4"
19+
"wp-cli/wp-cli-tests": "^5"
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",

phpcs.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,6 @@
5555
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
5656
<exclude-pattern>*/src/DB_Command\.php$</exclude-pattern>
5757
</rule>
58+
59+
<exclude-pattern>/tests/phpstan/scan-files</exclude-pattern>
5860
</ruleset>

phpstan.neon.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
5+
- db-command.php
6+
scanDirectories:
7+
- vendor/wp-cli/wp-cli/php
8+
scanFiles:
9+
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
10+
- tests/phpstan/scan-files.php
11+
treatPhpDocTypesAsCertain: false
12+
dynamicConstantNames:
13+
- DB_HOST
14+
- DB_NAME
15+
- DB_USER
16+
- DB_PASSWORD
17+
- DB_CHARSET
18+
- DB_COLLATE
19+
ignoreErrors:
20+
- identifier: missingType.iterableValue
21+
- identifier: missingType.parameter
22+
- identifier: missingType.return

src/DB_Command.php

Lines changed: 27 additions & 21 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 = 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;
@@ -1114,6 +1120,8 @@ public function size( $args, $assoc_args ) {
11141120
];
11151121
}
11161122

1123+
$size_format_display = '';
1124+
11171125
if ( ! empty( $size_format ) || $human_readable ) {
11181126
foreach ( $rows as $index => $row ) {
11191127
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Backfilling WP native constants.
@@ -1132,7 +1140,7 @@ public function size( $args, $assoc_args ) {
11321140
// phpcs:enable
11331141

11341142
if ( $human_readable ) {
1135-
$size_key = floor( log( $row['Size'] ) / log( 1000 ) );
1143+
$size_key = floor( log( (float) $row['Size'] ) / log( 1000 ) );
11361144
$sizes = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
11371145

11381146
$size_format = isset( $sizes[ $size_key ] ) ? $sizes[ $size_key ] : $sizes[0];
@@ -1184,7 +1192,7 @@ public function size( $args, $assoc_args ) {
11841192
}
11851193
$size_format_display = preg_replace( '/IB$/u', 'iB', strtoupper( $size_format ) );
11861194

1187-
$decimals = Utils\get_flag_value( $assoc_args, 'decimals', 0 );
1195+
$decimals = (int) Utils\get_flag_value( $assoc_args, 'decimals', 0 );
11881196
$rows[ $index ]['Size'] = round( (int) $row['Bytes'] / $divisor, $decimals ) . ' ' . $size_format_display;
11891197
}
11901198
}
@@ -1203,7 +1211,7 @@ function ( $a, $b ) use ( $order, $orderby ) {
12031211
list( $first, $second ) = $orderby_array;
12041212

12051213
if ( 'size' === $orderby ) {
1206-
return $first['Bytes'] > $second['Bytes'];
1214+
return $first['Bytes'] <=> $second['Bytes'];
12071215
}
12081216

12091217
return strcmp( $first['Name'], $second['Name'] );
@@ -1428,6 +1436,10 @@ public function search( $args, $assoc_args ) {
14281436
$after_context = Utils\get_flag_value( $assoc_args, 'after_context', 40 );
14291437
$after_context = '' === $after_context ? $after_context : (int) $after_context;
14301438

1439+
$default_regex_delimiter = false;
1440+
$regex_flags = false;
1441+
$regex_delimiter = '';
1442+
14311443
$regex = Utils\get_flag_value( $assoc_args, 'regex', false );
14321444
if ( false !== $regex ) {
14331445
$regex_flags = Utils\get_flag_value( $assoc_args, 'regex-flags', false );
@@ -1481,7 +1493,7 @@ public function search( $args, $assoc_args ) {
14811493
$esc_like_search = '%' . Utils\esc_like( $search ) . '%';
14821494
}
14831495

1484-
$encoding = null;
1496+
$encoding = false;
14851497
if ( 0 === strpos( $wpdb->charset, self::ENCODING_UTF8 ) ) {
14861498
$encoding = 'UTF-8';
14871499
}
@@ -1561,7 +1573,7 @@ public function search( $args, $assoc_args ) {
15611573
}
15621574
if ( $after_context ) {
15631575
$end_offset = $offset + strlen( $match );
1564-
$after = \cli\safe_substr( substr( $col_val, $end_offset ), 0, $after_context, false /*is_width*/, $col_encoding );
1576+
$after = (string) \cli\safe_substr( substr( $col_val, $end_offset ), 0, $after_context, false /*is_width*/, $col_encoding );
15651577
// To lessen context duplication in output, shorten the after context if it overlaps with the next match.
15661578
if ( $i + 1 < $match_cnt && $end_offset + strlen( $after ) > $matches[0][ $i + 1 ][1] ) {
15671579
$after = substr( $after, 0, $matches[0][ $i + 1 ][1] - $end_offset );
@@ -1857,7 +1869,7 @@ private static function get_dbuser_dbpass_args( $assoc_args ) {
18571869
* Gets the column names of a db table differentiated into key columns and text columns and all columns.
18581870
*
18591871
* @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.
1872+
* @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.
18611873
*/
18621874
private static function get_columns( $table ) {
18631875
global $wpdb;
@@ -1890,7 +1902,7 @@ private static function get_columns( $table ) {
18901902
/**
18911903
* Determines whether a column is considered text or not.
18921904
*
1893-
* @param string Column type.
1905+
* @param string $type Column type.
18941906
* @return bool True if text column, false otherwise.
18951907
*/
18961908
private static function is_text_col( $type ) {
@@ -1909,6 +1921,8 @@ private static function is_text_col( $type ) {
19091921
*
19101922
* @param string|array $idents A single identifier or an array of identifiers.
19111923
* @return string|array An escaped string if given a string, or an array of escaped strings if given an array of strings.
1924+
*
1925+
* @phpstan-return ($idents is string ? string : array)
19121926
*/
19131927
private static function esc_sql_ident( $idents ) {
19141928
$backtick = static function ( $v ) {
@@ -2155,11 +2169,6 @@ protected function get_current_sql_modes( $assoc_args ) {
21552169
// Make sure the provided arguments don't interfere with the expected
21562170
// output here.
21572171
$args = [];
2158-
foreach ( [] as $arg ) {
2159-
if ( isset( $assoc_args[ $arg ] ) ) {
2160-
$args[ $arg ] = $assoc_args[ $arg ];
2161-
}
2162-
}
21632172

21642173
if ( null === $modes ) {
21652174
$modes = [];
@@ -2183,17 +2192,14 @@ protected function get_current_sql_modes( $assoc_args ) {
21832192
}
21842193

21852194
if ( ! empty( $stdout ) ) {
2195+
$lines = preg_split( "/\r\n|\n|\r|,/", $stdout );
21862196
$modes = array_filter(
21872197
array_map(
21882198
'trim',
2189-
preg_split( "/\r\n|\n|\r|,/", $stdout )
2199+
$lines ? $lines : []
21902200
)
21912201
);
21922202
}
2193-
2194-
if ( false === $modes ) {
2195-
$modes = [];
2196-
}
21972203
}
21982204

21992205
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)