-
Notifications
You must be signed in to change notification settings - Fork 3.3k
REST API: Restore is_array() check in rest_convert_error_to_response()
#11307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
ad51bbf
e060139
d88933d
6398886
1692243
ea57364
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3428,7 +3428,7 @@ function rest_convert_error_to_response( $error ) { | |||||||||||||||||
| $status = array_reduce( | ||||||||||||||||||
| $error->get_all_error_data(), | ||||||||||||||||||
| static function ( $status, $error_data ) { | ||||||||||||||||||
| return $error_data['status'] ?? $status; | ||||||||||||||||||
| return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status; | ||||||||||||||||||
|
||||||||||||||||||
| public function set_status( $code ) { | |
| $this->status = absint( $code ); | |
| } |
Passing a non-numeric value to absint() results in a value of 1 being set as the status. This is not a fatal error, but it is wrong. Therefore, this would be even safer:
| return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status; | |
| if ( is_array( $error_data ) && isset( $error_data['status'] ) && is_numeric( $error_data['status'] ) ) { | |
| $status = (int) $error_data['status']; | |
| } | |
| return $status; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Great suggestion. I've added the is_numeric() check and (int) cast, along with a test for non-numeric status values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nate-allen Great! I also followed up with ea57364 to add explicit types, to use phpdoc instead of the inline comment, and I updated the test to account for the possibility that there could be more than one data item added that has status. Please take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! The type hints and expanded multi-status test are nice additions
Uh oh!
There was an error while loading. Please reload this page.