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:
Complex classes like WP_REST_Menus often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WP_REST_Menus, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class WP_REST_Menus { |
||
|
|||
24 | |||
25 | |||
26 | /** |
||
27 | * Get WP API namespace. |
||
28 | * |
||
29 | * @since 1.2.0 |
||
30 | * @return string |
||
31 | */ |
||
32 | public static function get_api_namespace() { |
||
35 | |||
36 | |||
37 | /** |
||
38 | * Get WP API Menus namespace. |
||
39 | * |
||
40 | * @since 1.2.1 |
||
41 | * @return string |
||
42 | */ |
||
43 | public static function get_plugin_namespace() { |
||
46 | |||
47 | |||
48 | /** |
||
49 | * Register menu routes for WP API v2. |
||
50 | * |
||
51 | * @since 1.2.0 |
||
52 | * @return array |
||
53 | */ |
||
54 | public function register_routes() { |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Get menus. |
||
114 | * |
||
115 | * @since 1.2.0 |
||
116 | * @return array All registered menus |
||
117 | */ |
||
118 | public static function get_menus() { |
||
146 | |||
147 | |||
148 | /** |
||
149 | * Get a menu. |
||
150 | * |
||
151 | * @since 1.2.0 |
||
152 | * @param $request |
||
153 | * @return array Menu data |
||
154 | */ |
||
155 | public function get_menu( $request ) { |
||
188 | |||
189 | /** |
||
190 | * Get a menu rendered in html. |
||
191 | * |
||
192 | * @since 1.x.0 |
||
193 | * @param $request |
||
194 | * @return array Menu data |
||
195 | */ |
||
196 | public function get_menu_html( $request ) { |
||
225 | |||
226 | |||
227 | |||
228 | /** |
||
229 | * Handle nested menu items. |
||
230 | * |
||
231 | * Given a flat array of menu items, split them into parent/child items |
||
232 | * and recurse over them to return children nested in their parent. |
||
233 | * |
||
234 | * @since 1.2.0 |
||
235 | * @param $menu_items |
||
236 | * @param $parent |
||
237 | * @return array |
||
238 | */ |
||
239 | private function nested_menu_items( &$menu_items, $parent = null ) { |
||
262 | |||
263 | |||
264 | /** |
||
265 | * Check if a collection of menu items contains an item that is the parent id of 'id'. |
||
266 | * |
||
267 | * @since 1.2.0 |
||
268 | * @param array $items |
||
269 | * @param int $id |
||
270 | * @return array |
||
271 | */ |
||
272 | private function has_children( $items, $id ) { |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Get menu locations. |
||
281 | * |
||
282 | * @since 1.2.0 |
||
283 | * @param $request |
||
284 | * @return array All registered menus locations |
||
285 | */ |
||
286 | public static function get_menu_locations( $request ) { |
||
287 | |||
288 | $locations = get_nav_menu_locations(); |
||
289 | $registered_menus = get_registered_nav_menus(); |
||
290 | $rest_url = get_rest_url() . self::get_plugin_namespace() . '/menu-locations/'; |
||
291 | $rest_url_base = get_rest_url() . self::get_plugin_namespace(); |
||
292 | $rest_menus = array(); |
||
293 | |||
294 | View Code Duplication | if ( $locations && $registered_menus ) : |
|
295 | |||
296 | foreach ( $registered_menus as $slug => $label ) : |
||
297 | |||
298 | // Sanity check |
||
299 | if ( ! isset( $locations[ $slug ] ) ) { |
||
300 | continue; |
||
301 | } |
||
302 | |||
303 | $rest_menus[ $slug ]['ID'] = $locations[ $slug ]; |
||
304 | $rest_menus[ $slug ]['label'] = $label; |
||
305 | $rest_menus[ $slug ]['meta']['links']['collection'] = $rest_url; |
||
306 | $rest_menus[ $slug ]['meta']['links']['self'] = $rest_url . $slug; |
||
307 | $rest_menus[ $slug ]['meta']['links']['html'] = $rest_url_base . '/menu-html-location/' . $slug; |
||
308 | |||
309 | endforeach; |
||
310 | |||
311 | endif; |
||
312 | |||
313 | return $rest_menus; |
||
314 | } |
||
315 | |||
316 | |||
317 | /** |
||
318 | * Get menu for location. |
||
319 | * |
||
320 | * @since 1.2.0 |
||
321 | * @param $request |
||
322 | * @return array The menu for the corresponding location |
||
323 | */ |
||
324 | public function get_menu_location( $request ) { |
||
383 | |||
384 | |||
385 | /** |
||
386 | * Get menu rendered in html for location. |
||
387 | * |
||
388 | * @since 1.x.0 |
||
389 | * @param $request |
||
390 | * @return array The menu for the corresponding location |
||
391 | */ |
||
392 | public function get_menu_html_location( $request ) { |
||
428 | |||
429 | |||
430 | /** |
||
431 | * Returns all child nav_menu_items under a specific parent. |
||
432 | * |
||
433 | * @since 1.2.0 |
||
434 | * @param int $parent_id The parent nav_menu_item ID |
||
435 | * @param array $nav_menu_items Navigation menu items |
||
436 | * @param bool $depth Gives all children or direct children only |
||
437 | * @return array returns filtered array of nav_menu_items |
||
438 | */ |
||
439 | View Code Duplication | public function get_nav_menu_item_children( $parent_id, $nav_menu_items, $depth = true ) { |
|
461 | |||
462 | |||
463 | /** |
||
464 | * Format a menu item for REST API consumption. |
||
465 | * |
||
466 | * @since 1.2.0 |
||
467 | * @param object|array $menu_item The menu item |
||
468 | * @param bool $children Get menu item children (default false) |
||
469 | * @param array $menu The menu the item belongs to (used when $children is set to true) |
||
470 | * @return array a formatted menu item for REST |
||
471 | */ |
||
472 | View Code Duplication | public function format_menu_item( $menu_item, $children = false, $menu = array() ) { |
|
499 | |||
500 | |||
501 | } |
||
502 | |||
505 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.