Conversation
|
Context on test fatal: WordPress/WordPress-Coding-Standards#2219 To correct/prevent the fatal, I've updated both |
| */ | ||
| public function clean_private_data( $data ): array { | ||
| foreach ( $data as &$details ) { | ||
| // remove private info. |
There was a problem hiding this comment.
| // remove private info. | |
| // Remove private info. |
| * | ||
| * @return array<string,mixed> Filtered Site Health data. | ||
| */ | ||
| public function clean_private_data( $data ): array { |
There was a problem hiding this comment.
If you're sure the $data parameter will always be an array, then type-hint it.
If you cannot be always sure of that, add an is_array($data) check to bail if $data is not an array.
There was a problem hiding this comment.
Same applies to the return type: either you're sure of the type or not.
If you're not, remove the return type.
| $details['fields'] = array_filter( | ||
| $details['fields'], | ||
| function ( $field ) { | ||
| return empty( $field['private'] ); | ||
| } | ||
| ); |
There was a problem hiding this comment.
When the data is removed, the array_filter function will leave "holes" in the numeric indexes.
Some code down the line might assume continuity in the indexes.
Use array_values to restore continuity:
| $details['fields'] = array_filter( | |
| $details['fields'], | |
| function ( $field ) { | |
| return empty( $field['private'] ); | |
| } | |
| ); | |
| $details['fields'] = array_values( array_filter( | |
| $details['fields'], | |
| function ( $field ) { | |
| return empty( $field['private'] ); | |
| } | |
| ) ); |
| foreach ( $data as &$details ) { | ||
| // remove private info. | ||
| $details['fields'] = array_filter( | ||
| $details['fields'], |
There was a problem hiding this comment.
You're assuming $details['fields'] will be set, check with isset first.
Don't send fields that are marked private!