Skip to content

Commit 317adff

Browse files
committed
Code Quality: Replace void with proper return types in union PHPDoc annotations.
In PHP's type system, `void` means a function does not return a value and cannot be part of a union type. Many functions in core were documented as returning e.g. `string|void` while actually returning `null` implicitly via bare `return;` statements. This replaces `void` with `null` in union return types, adds explicit `return null;` statements, and updates `@return` annotations across 22 files in `wp-includes`. Additionally: * Adds `@return never` for `WP_Recovery_Mode::redirect_protected()`. * Fixes `WP_Theme_JSON::set_spacing_sizes()` to use `@return void` instead of `@return null|void`. * Removes `void` from return types where the function always returns a value or dies: `remove_theme_support()`, `WP_Recovery_Mode::handle_error()`. * Fixes `wp_die()` return type from `never|void` to `void` with clarified description. * Initializes `$primary` variable in `get_active_blog_for_user()` to prevent a possible undefined variable notice. Developed in WordPress#11012 Follow-up to r62177, r61766, r61719. Props apermo, xateman, westonruter, parthvataliya, nimeshatxecurify. See #64704. git-svn-id: https://develop.svn.wordpress.org/trunk@62178 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 232266b commit 317adff

22 files changed

Lines changed: 104 additions & 87 deletions

src/wp-includes/canonical.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
* @param string $requested_url Optional. The URL that was requested, used to
3838
* figure if redirect is needed.
3939
* @param bool $do_redirect Optional. Redirect to the new URL.
40-
* @return string|void The string of the URL, if redirect needed.
40+
* @return string|null The string of the URL, if redirect needed. Never returns if a redirect occurs, depending on $do_redirect.
4141
*/
4242
function redirect_canonical( $requested_url = null, $do_redirect = true ) {
4343
global $wp_rewrite, $is_IIS, $wp_query, $wpdb, $wp;
4444

4545
if ( isset( $_SERVER['REQUEST_METHOD'] ) && ! in_array( strtoupper( $_SERVER['REQUEST_METHOD'] ), array( 'GET', 'HEAD' ), true ) ) {
46-
return;
46+
return null;
4747
}
4848

4949
/*
@@ -62,7 +62,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
6262
if ( is_admin() || is_search() || is_preview() || is_trackback() || is_favicon()
6363
|| ( $is_IIS && ! iis7_supports_permalinks() )
6464
) {
65-
return;
65+
return null;
6666
}
6767

6868
if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
@@ -74,7 +74,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
7474

7575
$original = parse_url( $requested_url );
7676
if ( false === $original ) {
77-
return;
77+
return null;
7878
}
7979

8080
// Notice fixing.
@@ -771,7 +771,7 @@ function redirect_canonical( $requested_url = null, $do_redirect = true ) {
771771
}
772772

773773
if ( ! $redirect_url || $redirect_url === $requested_url ) {
774-
return;
774+
return null;
775775
}
776776

777777
// Hex-encoded octets are case-insensitive.
@@ -830,7 +830,7 @@ function lowercase_octets( $matches ) {
830830

831831
// Yes, again -- in case the filter aborted the request.
832832
if ( ! $redirect_url || strip_fragment_from_url( $redirect_url ) === strip_fragment_from_url( $requested_url ) ) {
833-
return;
833+
return null;
834834
}
835835

836836
if ( $do_redirect ) {
@@ -841,7 +841,7 @@ function lowercase_octets( $matches ) {
841841
} else {
842842
// Debug.
843843
// die("1: $redirect_url<br />2: " . redirect_canonical( $redirect_url, false ) );
844-
return;
844+
return null;
845845
}
846846
} else {
847847
return $redirect_url;

src/wp-includes/capabilities.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,11 +1130,11 @@ function get_role( $role ) {
11301130
* @param string $display_name Display name for role.
11311131
* @param array<string,bool>|array<int,string> $capabilities Capabilities to be added to the role.
11321132
* Default empty array.
1133-
* @return WP_Role|void WP_Role object, if the role is added.
1133+
* @return WP_Role|null WP_Role object, if the role is added.
11341134
*/
11351135
function add_role( $role, $display_name, $capabilities = array() ) {
11361136
if ( empty( $role ) ) {
1137-
return;
1137+
return null;
11381138
}
11391139

11401140
return wp_roles()->add_role( $role, $display_name, $capabilities );

src/wp-includes/class-wp-admin-bar.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,24 +193,25 @@ final protected function _set_node( $args ) {
193193
* @since 3.3.0
194194
*
195195
* @param string $id
196-
* @return object|void Node.
196+
* @return object|null Node.
197197
*/
198198
final public function get_node( $id ) {
199199
$node = $this->_get_node( $id );
200200
if ( $node ) {
201201
return clone $node;
202202
}
203+
return null;
203204
}
204205

205206
/**
206207
* @since 3.3.0
207208
*
208209
* @param string $id
209-
* @return object|void
210+
* @return object|null
210211
*/
211212
final protected function _get_node( $id ) {
212213
if ( $this->bound ) {
213-
return;
214+
return null;
214215
}
215216

216217
if ( empty( $id ) ) {
@@ -220,17 +221,18 @@ final protected function _get_node( $id ) {
220221
if ( isset( $this->nodes[ $id ] ) ) {
221222
return $this->nodes[ $id ];
222223
}
224+
return null;
223225
}
224226

225227
/**
226228
* @since 3.3.0
227229
*
228-
* @return array|void
230+
* @return array|null
229231
*/
230232
final public function get_nodes() {
231233
$nodes = $this->_get_nodes();
232234
if ( ! $nodes ) {
233-
return;
235+
return null;
234236
}
235237

236238
foreach ( $nodes as &$node ) {
@@ -242,11 +244,11 @@ final public function get_nodes() {
242244
/**
243245
* @since 3.3.0
244246
*
245-
* @return array|void
247+
* @return array|null
246248
*/
247249
final protected function _get_nodes() {
248250
if ( $this->bound ) {
249-
return;
251+
return null;
250252
}
251253

252254
return $this->nodes;
@@ -307,11 +309,11 @@ public function render() {
307309
/**
308310
* @since 3.3.0
309311
*
310-
* @return object|void
312+
* @return object|null
311313
*/
312314
final protected function _bind() {
313315
if ( $this->bound ) {
314-
return;
316+
return null;
315317
}
316318

317319
/*

src/wp-includes/class-wp-block-type.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,8 @@ public function __construct( $block_type, $args = array() ) {
358358
*
359359
* @param string $name Deprecated property name.
360360
*
361-
* @return string|string[]|null|void The value read from the new property if the first item in the array provided,
362-
* null when value not found, or void when unknown property name provided.
361+
* @return string|string[]|null The value read from the new property if the first item in the array provided,
362+
* null when value not found or when unknown property name provided.
363363
*/
364364
public function __get( $name ) {
365365
if ( 'variations' === $name ) {
@@ -371,7 +371,7 @@ public function __get( $name ) {
371371
}
372372

373373
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
374-
return;
374+
return null;
375375
}
376376

377377
$new_name = $name . '_handles';

src/wp-includes/class-wp-plugin-dependencies.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,13 @@ protected static function get_dependency_filepaths() {
643643
*
644644
* @global string $pagenow The filename of the current screen.
645645
*
646-
* @return array|void An array of dependency API data, or void on early exit.
646+
* @return array|null An array of dependency API data, or null on early exit.
647647
*/
648648
protected static function get_dependency_api_data() {
649649
global $pagenow;
650650

651651
if ( ! is_admin() || ( 'plugins.php' !== $pagenow && 'plugin-install.php' !== $pagenow ) ) {
652-
return;
652+
return null;
653653
}
654654

655655
if ( is_array( self::$dependency_api_data ) ) {

src/wp-includes/class-wp-recovery-mode.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ public function is_initialized() {
161161
* @since 5.2.0
162162
*
163163
* @param array $error Error details from `error_get_last()`.
164-
* @return true|WP_Error|void True if the error was handled and headers have already been sent.
165-
* Or the request will exit to try and catch multiple errors at once.
166-
* WP_Error if an error occurred preventing it from being handled.
164+
* @return true|WP_Error True if the error was handled and headers have already been sent.
165+
* Or the request will exit to try and catch multiple errors at once.
166+
* WP_Error if an error occurred preventing it from being handled.
167167
*/
168168
public function handle_error( array $error ) {
169169

@@ -455,6 +455,8 @@ protected function store_error( $error ) {
455455
* next request again. Otherwise it will create a redirect loop.
456456
*
457457
* @since 5.2.0
458+
*
459+
* @return never
458460
*/
459461
protected function redirect_protected() {
460462
// Pluggable is usually loaded after plugins, so we manually include it here for redirection functionality.

src/wp-includes/class-wp-roles.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ public function reinit() {
171171
* @param string $display_name Role display name.
172172
* @param array<string,bool>|array<int,string> $capabilities Capabilities to be added to the role.
173173
* Default empty array.
174-
* @return WP_Role|void WP_Role object, if the role is added.
174+
* @return WP_Role|null WP_Role object, if the role is added.
175175
*/
176176
public function add_role( $role, $display_name, $capabilities = array() ) {
177177
if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
178-
return;
178+
return null;
179179
}
180180

181181
if ( wp_is_numeric_array( $capabilities ) ) {

src/wp-includes/class-wp-scripts.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public function print_scripts( $handles = false, $group = false ) {
201201
* @param string $handle The script's registered handle.
202202
* @param bool $display Optional. Whether to print the extra script
203203
* instead of just returning it. Default true.
204-
* @return bool|string|void Void if no data exists, extra scripts if `$display` is true,
204+
* @return bool|string|null Null if no data exists, extra scripts if `$display` is true,
205205
* true otherwise.
206206
*/
207207
public function print_scripts_l10n( $handle, $display = true ) {
@@ -217,13 +217,13 @@ public function print_scripts_l10n( $handle, $display = true ) {
217217
* @param string $handle The script's registered handle.
218218
* @param bool $display Optional. Whether to print the extra script
219219
* instead of just returning it. Default true.
220-
* @return bool|string|void Void if no data exists, extra scripts if `$display` is true,
220+
* @return bool|string|null Null if no data exists, extra scripts if `$display` is true,
221221
* true otherwise.
222222
*/
223223
public function print_extra_script( $handle, $display = true ) {
224224
$output = $this->get_data( $handle, 'data' );
225225
if ( ! $output ) {
226-
return;
226+
return null;
227227
}
228228

229229
/*

src/wp-includes/class-wp-theme-json.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4267,7 +4267,7 @@ public function get_data() {
42674267
* generated in the constructor and merge methods instead
42684268
* of manually after instantiation.
42694269
*
4270-
* @return null|void
4270+
* @return void
42714271
*/
42724272
public function set_spacing_sizes() {
42734273
_deprecated_function( __METHOD__, '6.6.0' );
@@ -4296,12 +4296,12 @@ public function set_spacing_sizes() {
42964296
E_USER_NOTICE
42974297
);
42984298
}
4299-
return null;
4299+
return;
43004300
}
43014301

43024302
// If theme authors want to prevent the generation of the core spacing scale they can set their theme.json spacingScale.steps to 0.
43034303
if ( 0 === $spacing_scale['steps'] ) {
4304-
return null;
4304+
return;
43054305
}
43064306

43074307
$spacing_sizes = static::compute_spacing_sizes( $spacing_scale );

src/wp-includes/class-wp-widget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function update( $new_instance, $old_instance ) {
138138
* @since 2.8.0
139139
*
140140
* @param array $instance The settings for the particular instance of the widget.
141-
* @return string|void Default return is 'noform'.
141+
* @return string|null Default return is 'noform'. A subclass may opt to return null.
142142
*/
143143
public function form( $instance ) {
144144
echo '<p class="no-options-widget">' . __( 'There are no options for this widget.' ) . '</p>';

0 commit comments

Comments
 (0)