Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
10 | class PodsRESTHandlers { |
||
11 | |||
12 | /** |
||
13 | * Holds a Pods object to avoid extra DB queries |
||
14 | * |
||
15 | * @since 2.5.6 |
||
16 | * |
||
17 | * @var Pods |
||
18 | */ |
||
19 | private static $pod; |
||
20 | |||
21 | /** |
||
22 | * Get Pod object |
||
23 | * |
||
24 | * @since 2.5.6 |
||
25 | * |
||
26 | * @param $pod_name |
||
27 | * @param $id |
||
28 | * |
||
29 | * @return bool|Pods |
||
30 | */ |
||
31 | protected static function get_pod( $pod_name, $id ) { |
||
44 | |||
45 | /** |
||
46 | * Handler for getting custom field data. |
||
47 | * |
||
48 | * @since 2.5.6 |
||
49 | * |
||
50 | * @param array $object The object from the response |
||
51 | * @param string $field_name Name of field |
||
52 | * @param WP_REST_Request $request Current request |
||
53 | * @param string $object_type Type of object |
||
54 | * |
||
55 | * @return mixed |
||
56 | */ |
||
57 | public static function get_handler( $object, $field_name, $request, $object_type ) { |
||
176 | |||
177 | /** |
||
178 | * Handler for updating custom field data. |
||
179 | * |
||
180 | * @since 2.5.6 |
||
181 | * |
||
182 | * @param mixed $value Value to write |
||
183 | * @param object $object The object from the response |
||
184 | * @param string $field_name Name of field |
||
185 | * |
||
186 | * @return bool|int |
||
187 | */ |
||
188 | public static function write_handler( $value, $object, $field_name ) { |
||
205 | |||
206 | /** |
||
207 | * Add REST API support to a post type |
||
208 | * |
||
209 | * @since 2.5.6 |
||
210 | * |
||
211 | * @param string $post_type_name Name of post type |
||
212 | * @param bool|false $rest_base Optional. Base url segment. If not set, post type name is used |
||
213 | * @param string $controller Optional, controller class for route. If not set "WP_REST_Posts_Controller" is |
||
214 | * used. |
||
215 | */ |
||
216 | View Code Duplication | public static function post_type_rest_support( $post_type_name, $rest_base = false, $controller = 'WP_REST_Posts_Controller' ) { |
|
231 | |||
232 | /** |
||
233 | * Add REST API support to an already registered taxonomy. |
||
234 | * |
||
235 | * @since 2.5.6 |
||
236 | * |
||
237 | * @param string $taxonomy_name Taxonomy name. |
||
238 | * @param bool|false $rest_base Optional. Base url segment. If not set, taxonomy name is used. |
||
239 | * @param string $controller Optional, controller class for route. If not set "WP_REST_Terms_Controller" is |
||
240 | * used. |
||
241 | */ |
||
242 | View Code Duplication | public static function taxonomy_rest_support( $taxonomy_name, $rest_base = false, $controller = 'WP_REST_Terms_Controller' ) { |
|
257 | |||
258 | /** |
||
259 | * Check if a Pod supports extending core REST response. |
||
260 | * |
||
261 | * @since 2.5.6 |
||
262 | * |
||
263 | * @param array|Pods $pod Pod object or the pod_data array |
||
264 | * |
||
265 | * @return bool |
||
266 | */ |
||
267 | public static function pod_extends_core_route( $pod ) { |
||
282 | |||
283 | } |
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.