Conditions | 16 |
Paths | 30 |
Total Lines | 111 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 |
||
56 | public static function get_handler( $object, $field_name, $request ) { |
||
57 | |||
58 | $pod_name = pods_v( 'type', $object ); |
||
59 | |||
60 | /** |
||
61 | * If $pod_name in the line above is empty then the route invoked |
||
62 | * may be for a taxonomy, so lets try and check for that |
||
63 | * |
||
64 | */ |
||
65 | if ( empty( $pod_name ) ) { |
||
66 | $pod_name = pods_v( 'taxonomy', $object ); |
||
67 | } |
||
68 | |||
69 | $id = pods_v( 'id', $object ); |
||
70 | $pod = self::get_pod( $pod_name, $id ); |
||
71 | |||
72 | $value = false; |
||
73 | |||
74 | if ( $pod && PodsRESTFields::field_allowed_to_extend( $field_name, $pod, 'read' ) ) { |
||
75 | $params = null; |
||
76 | |||
77 | $field_data = $pod->fields( $field_name ); |
||
78 | |||
79 | if ( 'pick' == pods_v( 'type', $field_data ) ) { |
||
80 | $output_type = pods_v( 'rest_pick_response', $field_data['options'], 'array' ); |
||
81 | |||
82 | if ( 'array' == $output_type ) { |
||
83 | $related_pod_items = $pod->field( $field_name, array( 'output' => 'pod' ) ); |
||
84 | |||
85 | if ( $related_pod_items ) { |
||
86 | $fields = false; |
||
87 | $items = array(); |
||
88 | $depth = pods_v( 'rest_pick_depth', $field_data['options'], 2 ); |
||
89 | |||
90 | if ( ! is_array( $related_pod_items ) ) { |
||
91 | $related_pod_items = array( $related_pod_items ); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @var $related_pod Pods |
||
96 | */ |
||
97 | foreach ( $related_pod_items as $related_pod ) { |
||
98 | if ( ! is_object( $related_pod ) || ! is_a( $related_pod, 'Pods' ) ) { |
||
99 | $items = $related_pod_items; |
||
100 | |||
101 | break; |
||
102 | } |
||
103 | |||
104 | if ( false === $fields ) { |
||
105 | $fields = $related_pod->fields(); |
||
106 | $fields = array_keys( $fields ); |
||
107 | |||
108 | if ( isset( $related_pod->pod_data['object_fields'] ) && ! empty( $related_pod->pod_data['object_fields'] ) ) { |
||
109 | $fields = array_merge( $fields, array_keys( $related_pod->pod_data['object_fields'] ) ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * What fields to show in a related field REST response. |
||
114 | * |
||
115 | * @since 0.0.1 |
||
116 | * |
||
117 | * @param array $fields The fields to show |
||
118 | * @param string $field_name The name of the field |
||
119 | * @param object|Pods $pod The Pods object for Pod relationship is from. |
||
120 | * @param object|Pods $pod The Pods object for Pod relationship is to. |
||
121 | * @param int $id Current item ID |
||
122 | * @param object|WP_REST_Request Current request object. |
||
123 | */ |
||
124 | $fields = apply_filters( 'pods_rest_api_fields_for_relationship_response', $fields, $field_name, $pod, $related_pod, $id, $request ); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * What depth to use for a related field REST response. |
||
129 | * |
||
130 | * @since 0.0.1 |
||
131 | * |
||
132 | * @param array $depth The depth. |
||
133 | * @param string $field_name The name of the field |
||
134 | * @param object|Pods $pod The Pods object for Pod relationship is from. |
||
135 | * @param object|Pods $pod The Pods object for Pod relationship is to. |
||
136 | * @param int $id Current item ID |
||
137 | * @param object|WP_REST_Request Current request object. |
||
138 | */ |
||
139 | $depth = apply_filters( 'pods_rest_api_depth_for_relationship_response', $depth, $field_name, $pod, $related_pod, $id, $request ); |
||
140 | |||
141 | $params = array( |
||
142 | 'fields' => $fields, |
||
143 | 'depth' => $depth, |
||
144 | ); |
||
145 | |||
146 | $items[] = $related_pod->export( $params ); |
||
147 | } |
||
148 | |||
149 | $value = $items; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | $params = array( |
||
154 | 'output' => $output_type, |
||
155 | ); |
||
156 | } |
||
157 | |||
158 | // If no value set yet, get normal field value |
||
159 | if ( ! $value && ! is_array( $value ) ) { |
||
160 | $value = $pod->field( $field_name, $params ); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | return $value; |
||
165 | |||
166 | } |
||
167 | |||
274 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.