-
Notifications
You must be signed in to change notification settings - Fork 2
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
Private fields #101
base: develop
Are you sure you want to change the base?
Private fields #101
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're assuming $details['fields']
will be set, check with isset
first.
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.
Don't send fields that are marked private!