Skip to content

Commit 232266b

Browse files
committed
Code Quality: Replace void with proper return types in wpdb and related functions.
Replace `void` in union return types with `null`, `false`, or `never` as appropriate, and add explicit `return null` statements where methods previously fell through without a return value. Methods updated in `wpdb`: `prepare()`, `print_error()`, `check_connection()`, `get_row()`, `get_col_info()`, `bail()`, `check_database_version()`. Also adds `@return never` to `dead_db()` and fixes the `@phpstan-return` syntax for `wp_die()`. Developed in WordPress#11009 Props apermo, westonruter, xate, mukesh27, SergeyBiryukov. Fixes #64703. git-svn-id: https://develop.svn.wordpress.org/trunk@62177 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c2f2558 commit 232266b

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/wp-includes/class-wpdb.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,11 +1453,11 @@ private function _escape_identifier_value( $identifier ) {
14531453
* individual arguments.
14541454
* @param mixed ...$args Further variables to substitute into the query's placeholders
14551455
* if being called with individual arguments.
1456-
* @return string|void Sanitized query string, if there is a query to prepare.
1456+
* @return string|null Sanitized query string, if there is a query to prepare.
14571457
*/
14581458
public function prepare( $query, ...$args ) {
14591459
if ( is_null( $query ) ) {
1460-
return;
1460+
return null;
14611461
}
14621462

14631463
/*
@@ -1666,7 +1666,7 @@ public function prepare( $query, ...$args ) {
16661666
'6.2.0'
16671667
);
16681668

1669-
return;
1669+
return null;
16701670
}
16711671

16721672
$args_count = count( $args );
@@ -1684,7 +1684,7 @@ public function prepare( $query, ...$args ) {
16841684
'4.9.0'
16851685
);
16861686

1687-
return;
1687+
return null;
16881688
} else {
16891689
/*
16901690
* If we don't have the right number of placeholders,
@@ -1794,7 +1794,7 @@ public function esc_like( $text ) {
17941794
* @global array $EZSQL_ERROR Stores error information of query and error string.
17951795
*
17961796
* @param string $str The error to display.
1797-
* @return void|false Void if the showing of errors is enabled, false if disabled.
1797+
* @return null|false Null if the showing of errors is enabled, false if disabled.
17981798
*/
17991799
public function print_error( $str = '' ) {
18001800
global $EZSQL_ERROR;
@@ -1855,6 +1855,8 @@ public function print_error( $str = '' ) {
18551855
$query
18561856
);
18571857
}
1858+
1859+
return null;
18581860
}
18591861

18601862
/**
@@ -2117,7 +2119,7 @@ public function parse_db_host( $host ) {
21172119
* @since 3.9.0
21182120
*
21192121
* @param bool $allow_bail Optional. Allows the function to bail. Default true.
2120-
* @return bool|void True if the connection is up.
2122+
* @return bool Whether the connection is up. Exits if down and $allow_bail is true.
21212123
*/
21222124
public function check_connection( $allow_bail = true ) {
21232125
// Check if the connection is alive.
@@ -3056,7 +3058,7 @@ public function get_var( $query = null, $x = 0, $y = 0 ) {
30563058
* correspond to an stdClass object, an associative array, or a numeric array,
30573059
* respectively. Default OBJECT.
30583060
* @param int $y Optional. Row to return. Indexed from 0. Default 0.
3059-
* @return array|object|null|void Database query result in format specified by $output or null on failure.
3061+
* @return array|object|null Database query result in format specified by $output or null on failure.
30603062
*/
30613063
public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
30623064
$this->func_call = "\$db->get_row(\"$query\",$output,$y)";
@@ -3087,6 +3089,7 @@ public function get_row( $query = null, $output = OBJECT, $y = 0 ) {
30873089
} else {
30883090
$this->print_error( ' $db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N' );
30893091
}
3092+
return null;
30903093
}
30913094

30923095
/**
@@ -3902,6 +3905,8 @@ public function get_col_info( $info_type = 'name', $col_offset = -1 ) {
39023905
return $this->col_info[ $col_offset ]->{$info_type};
39033906
}
39043907
}
3908+
3909+
return null;
39053910
}
39063911

39073912
/**
@@ -3937,7 +3942,7 @@ public function timer_stop() {
39373942
* @param string $message The error message.
39383943
* @param string $error_code Optional. A computer-readable string to identify the error.
39393944
* Default '500'.
3940-
* @return void|false Void if the showing of errors is enabled, false if disabled.
3945+
* @return false False if the showing of errors is disabled.
39413946
*/
39423947
public function bail( $message, $error_code = '500' ) {
39433948
if ( $this->show_errors ) {
@@ -3995,7 +4000,7 @@ public function close() {
39954000
* @since 2.5.0
39964001
*
39974002
* @global string $required_mysql_version The minimum required MySQL version string.
3998-
* @return void|WP_Error
4003+
* @return WP_Error|null
39994004
*/
40004005
public function check_database_version() {
40014006
global $required_mysql_version;
@@ -4006,6 +4011,8 @@ public function check_database_version() {
40064011
/* translators: 1: WordPress version number, 2: Minimum required MySQL version number. */
40074012
return new WP_Error( 'database_version', sprintf( __( '<strong>Error:</strong> WordPress %1$s requires MySQL %2$s or higher' ), $wp_version, $required_mysql_version ) );
40084013
}
4014+
4015+
return null;
40094016
}
40104017

40114018
/**

src/wp-includes/functions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3776,7 +3776,7 @@ function wp_nonce_ays( $action ) {
37763776
* }
37773777
* @return never|void Returns void if `$args['exit']` is false, otherwise exits.
37783778
*
3779-
* @phpstan-return ( $args['exit'] is false ? void : never )
3779+
* @phpstan-return ( $args is array{exit: false} ? void : never )
37803780
*/
37813781
function wp_die( $message = '', $title = '', $args = array() ) {
37823782
global $wp_query;
@@ -5511,6 +5511,8 @@ function wp_ob_end_flush_all() {
55115511
* @since 2.3.2
55125512
*
55135513
* @global wpdb $wpdb WordPress database abstraction object.
5514+
*
5515+
* @return never
55145516
*/
55155517
function dead_db() {
55165518
global $wpdb;

0 commit comments

Comments
 (0)