| Conditions | 18 |
| Paths | 17 |
| Total Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 91 | /* translators: %s: get_schema() */ |
||
| 92 | _doing_it_wrong( 'WPCOM_REST_API_V2_Field_Controller::get_schema', sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), 'Jetpack 6.8' ); |
||
| 93 | } |
||
| 94 | |||
| 95 | function is_valid_for_context( $schema, $context ) { |
||
| 96 | return empty( $schema['context'] ) || in_array( $context, $schema['context'], true ); |
||
| 97 | } |
||
| 98 | |||
| 99 | function filter_response_by_context( $value, $schema, $context ) { |
||
| 100 | if ( ! $this->is_valid_for_context( $schema, $context ) ) { |
||
| 101 | return new WP_Error( '__wrong-context__' ); |
||
| 102 | } |
||
| 103 | |||
| 104 | switch ( $schema['type'] ) { |
||
| 105 | case 'array' : |
||
| 106 | if ( ! isset( $schema['items'] ) ) { |
||
| 107 | return $value; |
||
| 108 | } |
||
| 109 | |||
| 110 | // Shortcircuit if we know none of the items are valid for this context. |
||
| 111 | // This would only happen in a strangely written schema. |
||
| 112 | if ( ! $this->is_valid_for_context( $schema['items'], $context ) ) { |
||
| 113 | return array(); |
||
| 114 | } |
||
| 115 | |||
| 116 | // Recurse to prune sub-properties of each item. |
||
| 117 | |||
| 118 | $keys = array_keys( $value ); |
||
| 119 | |||
| 120 | $items = array_map( |
||
| 121 | array( $this, 'filter_response_by_context' ), |
||
| 122 | $value, |
||
| 123 | array_fill( 0, count( $keys ), $schema['items'] ), |
||
| 124 | array_fill( 0, count( $keys ), $context ) |
||
| 125 | ); |
||
| 126 | |||
| 127 | return array_combine( $keys, $items ); |
||
| 128 | case 'object' : |
||
| 129 | if ( ! isset( $schema['properties'] ) ) { |
||
| 130 | return $value; |
||
| 131 | } |
||
| 132 | |||
| 133 | foreach ( $value as $field_name => $field_value ) { |
||
| 134 | if ( isset( $schema['properties'][$field_name] ) ) { |
||
| 135 | $field_value = $this->filter_response_by_context( $field_value, $schema['properties'][$field_name], $context ); |
||
| 136 | if ( is_wp_error( $field_value ) && '__wrong-context__' === $field_value->get_error_code() ) { |
||
| 137 | unset( $value[$field_name] ); |
||
| 138 | } else { |
||
| 139 | // Respect recursion that pruned sub-properties of each property. |
||
| 140 | $value[$field_name] = $field_value; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return (object) $value; |
||
| 146 | } |
||
| 147 | |||
| 148 | return $value; |
||
| 151 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.