Complex classes like WPCOM_REST_API_V2_Endpoint_Admin_Menu 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 WPCOM_REST_API_V2_Endpoint_Admin_Menu, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class WPCOM_REST_API_V2_Endpoint_Admin_Menu extends WP_REST_Controller { |
||
13 | |||
14 | /** |
||
15 | * Namespace prefix. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | public $namespace = 'wpcom/v2'; |
||
20 | |||
21 | /** |
||
22 | * Endpoint base route. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | public $rest_base = 'admin-menu'; |
||
27 | |||
28 | /** |
||
29 | * WPCOM_REST_API_V2_Endpoint_Admin_Menu constructor. |
||
30 | */ |
||
31 | public function __construct() { |
||
34 | |||
35 | /** |
||
36 | * Register routes. |
||
37 | */ |
||
38 | public function register_routes() { |
||
52 | |||
53 | /** |
||
54 | * Checks if a given request has access to admin menus. |
||
55 | * |
||
56 | * @param WP_REST_Request $request Full details about the request. |
||
57 | * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. |
||
58 | */ |
||
59 | public function get_item_permissions_check( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
70 | |||
71 | /** |
||
72 | * Retrieves the admin menu. |
||
73 | * |
||
74 | * @param WP_REST_Request $request Full details about the request. |
||
75 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
||
76 | */ |
||
77 | public function get_item( $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
95 | |||
96 | /** |
||
97 | * Prepares the admin menu for the REST response. |
||
98 | * |
||
99 | * @param array $menu Admin menu. |
||
100 | * @return array Admin menu |
||
101 | */ |
||
102 | public function prepare_menu_for_response( array $menu ) { |
||
137 | |||
138 | /** |
||
139 | * Retrieves the admin menu's schema, conforming to JSON Schema. |
||
140 | * |
||
141 | * Note: if the shape of the API endpoint data changes it is important to also update |
||
142 | * the corresponding schema.js file. |
||
143 | * |
||
144 | * @see https://github.com/Automattic/wp-calypso/blob/ebde236ec9b21ea9621c0b0523bd5ea185523731/client/state/admin-menu/schema.js |
||
145 | * |
||
146 | * @return array Item schema data. |
||
147 | */ |
||
148 | public function get_item_schema() { |
||
205 | |||
206 | /** |
||
207 | * Sets up a menu item for consumption by Calypso. |
||
208 | * |
||
209 | * @param array $menu_item Menu item. |
||
210 | * @return array Prepared menu item. |
||
211 | */ |
||
212 | private function prepare_menu_item( array $menu_item ) { |
||
238 | |||
239 | /** |
||
240 | * Sets up a submenu item for consumption by Calypso. |
||
241 | * |
||
242 | * @param array $submenu_item Submenu item. |
||
243 | * @param array $menu_item Menu item. |
||
244 | * @return array Prepared submenu item. |
||
245 | */ |
||
246 | private function prepare_submenu_item( array $submenu_item, array $menu_item ) { |
||
266 | |||
267 | /** |
||
268 | * Prepares a menu icon for consumption by Calypso. |
||
269 | * |
||
270 | * @param string $icon Menu icon. |
||
271 | * @return string |
||
272 | */ |
||
273 | private function prepare_menu_item_icon( $icon ) { |
||
288 | |||
289 | /** |
||
290 | * Prepares a menu item url for consumption by Calypso. |
||
291 | * |
||
292 | * @param string $url Menu slug. |
||
293 | * @param string $parent_slug Optional. Parent menu item slug. Default empty string. |
||
294 | * @return string |
||
295 | */ |
||
296 | private function prepare_menu_item_url( $url, $parent_slug = '' ) { |
||
328 | |||
329 | /** |
||
330 | * Parses the update count from a given menu item title and removes the associated markup. |
||
331 | * |
||
332 | * "Plugin" and "Updates" menu items have a count badge when there are updates available. |
||
333 | * This method parses that information and adds it to the response. |
||
334 | * |
||
335 | * @param array $item containing title to parse. |
||
336 | * @return array |
||
337 | */ |
||
338 | private function parse_count_data( $item ) { |
||
352 | |||
353 | /** |
||
354 | * Removes unexpected markup from the title. |
||
355 | * |
||
356 | * @param array $item containing title to parse. |
||
357 | * @return array |
||
358 | */ |
||
359 | private function sanitize_title( $item ) { |
||
368 | |||
369 | /** |
||
370 | * Parses data from the markup in titles and sanitizes titles from unexpected markup. |
||
371 | * |
||
372 | * @param string $title Title to parse. |
||
373 | * @return array |
||
374 | */ |
||
375 | private function parse_markup_data( $title ) { |
||
386 | } |
||
387 | |||
389 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.