Skip to content

Commit 93b45f4

Browse files
committed
Docs: Modernize and improve specificity of types in WP_Error class.
This brings the `WP_Error` class to full PHPStan rule level 10 compliance. Also make use of null coalescing operator where appropriate, and simplify `has_errors()` method. Developed in WordPress#12405. Follow-up to r42761, r49115, r49116. See #64898, #64897. git-svn-id: https://develop.svn.wordpress.org/trunk@62635 602fd350-edb4-49c9-b593-d223f7449a82
1 parent ec8b004 commit 93b45f4

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ class WP_Error {
2121
* Stores the list of errors.
2222
*
2323
* @since 2.1.0
24-
* @var array
24+
* @var array<int|string, string[]>
2525
*/
2626
public $errors = array();
2727

2828
/**
2929
* Stores the most recently added data for each error code.
3030
*
3131
* @since 2.1.0
32-
* @var array
32+
* @var array<int|string, mixed>
3333
*/
3434
public $error_data = array();
3535

3636
/**
3737
* Stores previously added data added for error codes, oldest-to-newest by code.
3838
*
3939
* @since 5.6.0
40-
* @var array[]
40+
* @var array<int|string, mixed[]>
4141
*/
4242
protected $additional_data = array();
4343

@@ -71,7 +71,7 @@ public function __construct( $code = '', $message = '', $data = '' ) {
7171
*
7272
* @since 2.1.0
7373
*
74-
* @return array List of error codes, if available.
74+
* @return list<int|string> List of error codes, if available.
7575
*/
7676
public function get_error_codes() {
7777
if ( ! $this->has_errors() ) {
@@ -111,18 +111,14 @@ public function get_error_messages( $code = '' ) {
111111
// Return all messages if no code specified.
112112
if ( empty( $code ) ) {
113113
$all_messages = array();
114-
foreach ( (array) $this->errors as $code => $messages ) {
114+
foreach ( (array) $this->errors as $messages ) {
115115
$all_messages = array_merge( $all_messages, $messages );
116116
}
117117

118118
return $all_messages;
119119
}
120120

121-
if ( isset( $this->errors[ $code ] ) ) {
122-
return $this->errors[ $code ];
123-
} else {
124-
return array();
125-
}
121+
return $this->errors[ $code ] ?? array();
126122
}
127123

128124
/**
@@ -161,9 +157,7 @@ public function get_error_data( $code = '' ) {
161157
$code = $this->get_error_code();
162158
}
163159

164-
if ( isset( $this->error_data[ $code ] ) ) {
165-
return $this->error_data[ $code ];
166-
}
160+
return $this->error_data[ $code ] ?? null;
167161
}
168162

169163
/**
@@ -174,10 +168,7 @@ public function get_error_data( $code = '' ) {
174168
* @return bool If the instance contains errors.
175169
*/
176170
public function has_errors() {
177-
if ( ! empty( $this->errors ) ) {
178-
return true;
179-
}
180-
return false;
171+
return (bool) $this->errors;
181172
}
182173

183174
/**
@@ -188,6 +179,7 @@ public function has_errors() {
188179
* @param string|int $code Error code.
189180
* @param string $message Error message.
190181
* @param mixed $data Optional. Error data. Default empty string.
182+
* @return void
191183
*/
192184
public function add( $code, $message, $data = '' ) {
193185
$this->errors[ $code ][] = $message;
@@ -217,6 +209,7 @@ public function add( $code, $message, $data = '' ) {
217209
*
218210
* @param mixed $data Error data.
219211
* @param string|int $code Error code.
212+
* @return void
220213
*/
221214
public function add_data( $data, $code = '' ) {
222215
if ( empty( $code ) ) {
@@ -265,6 +258,7 @@ public function get_all_error_data( $code = '' ) {
265258
* @since 4.1.0
266259
*
267260
* @param string|int $code Error code.
261+
* @return void
268262
*/
269263
public function remove( $code ) {
270264
unset( $this->errors[ $code ] );
@@ -278,6 +272,7 @@ public function remove( $code ) {
278272
* @since 5.6.0
279273
*
280274
* @param WP_Error $error Error object to merge.
275+
* @return void
281276
*/
282277
public function merge_from( WP_Error $error ) {
283278
static::copy_errors( $error, $this );
@@ -289,6 +284,7 @@ public function merge_from( WP_Error $error ) {
289284
* @since 5.6.0
290285
*
291286
* @param WP_Error $error Error object to export into.
287+
* @return void
292288
*/
293289
public function export_to( WP_Error $error ) {
294290
static::copy_errors( $this, $error );
@@ -301,6 +297,7 @@ public function export_to( WP_Error $error ) {
301297
*
302298
* @param WP_Error $from The WP_Error to copy from.
303299
* @param WP_Error $to The WP_Error to copy to.
300+
* @return void
304301
*/
305302
protected static function copy_errors( WP_Error $from, WP_Error $to ) {
306303
foreach ( $from->get_error_codes() as $code ) {

0 commit comments

Comments
 (0)