| Conditions | 19 |
| Paths | 180 |
| Total Lines | 142 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 57 | public static function get_handler( $object, $field_name, $request, $object_type ) { |
||
| 58 | |||
| 59 | $pod_name = pods_v( 'type', $object ); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * If $pod_name in the line above is empty then the route invoked |
||
| 63 | * may be for a taxonomy, so lets try and check for that |
||
| 64 | * |
||
| 65 | */ |
||
| 66 | if ( empty( $pod_name ) ) { |
||
| 67 | $pod_name = pods_v( 'taxonomy', $object ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * $pod_name is still empty, so check lets check $object_type |
||
| 72 | * |
||
| 73 | */ |
||
| 74 | |||
| 75 | if ( empty( $pod_name ) ) { |
||
| 76 | if ( 'attachment' == $object_type ) { |
||
| 77 | $pod_name = 'media'; |
||
| 78 | } else { |
||
| 79 | $pod_name = $object_type; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Filter the pod name |
||
| 85 | * |
||
| 86 | * @since 2.6.7 |
||
| 87 | * |
||
| 88 | * @param array $pod_name Pod name |
||
| 89 | * @param Pods $object Rest object |
||
| 90 | * @param string $field_name Name of the field |
||
| 91 | * @param WP_REST_Request $request Current request |
||
| 92 | * @param string $object_type Rest Object type |
||
| 93 | */ |
||
| 94 | $pod_name = apply_filters( 'pods_rest_api_pod_name', $pod_name, $object, $field_name, $request, $object_type ); |
||
| 95 | |||
| 96 | $id = pods_v( 'id', $object ); |
||
| 97 | |||
| 98 | if ( empty( $id ) ) { |
||
| 99 | $id = pods_v( 'ID', $object ); |
||
| 100 | } |
||
| 101 | |||
| 102 | $pod = self::get_pod( $pod_name, $id ); |
||
| 103 | |||
| 104 | $value = false; |
||
| 105 | |||
| 106 | if ( $pod && PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'read' ) ) { |
||
| 107 | $params = null; |
||
| 108 | |||
| 109 | $field_data = $pod->fields( $field_name ); |
||
| 110 | |||
| 111 | if ( 'pick' == pods_v( 'type', $field_data ) ) { |
||
| 112 | $output_type = pods_v( 'rest_pick_response', $field_data['options'], 'array' ); |
||
| 113 | |||
| 114 | if ( 'array' == $output_type ) { |
||
| 115 | $related_pod_items = $pod->field( $field_name, array( 'output' => 'pod' ) ); |
||
| 116 | |||
| 117 | if ( $related_pod_items ) { |
||
| 118 | $fields = false; |
||
| 119 | $items = array(); |
||
| 120 | $depth = pods_v( 'rest_pick_depth', $field_data['options'], 2 ); |
||
| 121 | |||
| 122 | if ( ! is_array( $related_pod_items ) ) { |
||
| 123 | $related_pod_items = array( $related_pod_items ); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var $related_pod Pods |
||
| 128 | */ |
||
| 129 | foreach ( $related_pod_items as $related_pod ) { |
||
| 130 | if ( ! is_object( $related_pod ) || ! is_a( $related_pod, 'Pods' ) ) { |
||
| 131 | $items = $related_pod_items; |
||
| 132 | |||
| 133 | break; |
||
| 134 | } |
||
| 135 | |||
| 136 | if ( false === $fields ) { |
||
| 137 | $fields = $related_pod->fields(); |
||
| 138 | $fields = array_keys( $fields ); |
||
| 139 | |||
| 140 | if ( isset( $related_pod->pod_data['object_fields'] ) && ! empty( $related_pod->pod_data['object_fields'] ) ) { |
||
| 141 | $fields = array_merge( $fields, array_keys( $related_pod->pod_data['object_fields'] ) ); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * What fields to show in a related field REST response. |
||
| 146 | * |
||
| 147 | * @since 0.0.1 |
||
| 148 | * |
||
| 149 | * @param array $fields The fields to show |
||
| 150 | * @param string $field_name The name of the field |
||
| 151 | * @param object|Pods $pod The Pods object for Pod relationship is from. |
||
| 152 | * @param object|Pods $pod The Pods object for Pod relationship is to. |
||
| 153 | * @param int $id Current item ID |
||
| 154 | * @param object|WP_REST_Request Current request object. |
||
| 155 | */ |
||
| 156 | $fields = apply_filters( 'pods_rest_api_fields_for_relationship_response', $fields, $field_name, $pod, $related_pod, $id, $request ); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * What depth to use for a related field REST response. |
||
| 161 | * |
||
| 162 | * @since 0.0.1 |
||
| 163 | * |
||
| 164 | * @param array $depth The depth. |
||
| 165 | * @param string $field_name The name of the field |
||
| 166 | * @param object|Pods $pod The Pods object for Pod relationship is from. |
||
| 167 | * @param object|Pods $pod The Pods object for Pod relationship is to. |
||
| 168 | * @param int $id Current item ID |
||
| 169 | * @param object|WP_REST_Request Current request object. |
||
| 170 | */ |
||
| 171 | $depth = apply_filters( 'pods_rest_api_depth_for_relationship_response', $depth, $field_name, $pod, $related_pod, $id, $request ); |
||
| 172 | |||
| 173 | $params = array( |
||
| 174 | 'fields' => $fields, |
||
| 175 | 'depth' => $depth, |
||
| 176 | ); |
||
| 177 | |||
| 178 | $items[] = $related_pod->export( $params ); |
||
| 179 | } |
||
| 180 | |||
| 181 | $value = $items; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | $params = array( |
||
| 186 | 'output' => $output_type, |
||
| 187 | ); |
||
| 188 | } |
||
| 189 | |||
| 190 | // If no value set yet, get normal field value |
||
| 191 | if ( ! $value && ! is_array( $value ) ) { |
||
| 192 | $value = $pod->field( $field_name, $params ); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | return $value; |
||
| 197 | |||
| 198 | } |
||
| 199 | |||
| 354 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.