Complex classes like FS_Admin_Menu_Manager 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 FS_Admin_Menu_Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class FS_Admin_Menu_Manager { |
||
14 | |||
15 | #region Properties |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $_plugin_slug; |
||
21 | |||
22 | /** |
||
23 | * @since 1.0.6 |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $_menu_slug; |
||
28 | /** |
||
29 | * @since 1.1.3 |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $_parent_slug; |
||
34 | /** |
||
35 | * @since 1.1.3 |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $_parent_type; |
||
40 | /** |
||
41 | * @since 1.1.3 |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | private $_type; |
||
46 | /** |
||
47 | * @since 1.1.3 |
||
48 | * |
||
49 | * @var bool |
||
50 | */ |
||
51 | private $_is_top_level; |
||
52 | /** |
||
53 | * @since 1.1.3 |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | private $_is_override_exact; |
||
58 | /** |
||
59 | * @since 1.1.3 |
||
60 | * |
||
61 | * @var array<string,bool> |
||
62 | */ |
||
63 | private $_default_submenu_items; |
||
64 | /** |
||
65 | * @since 1.1.3 |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | private $_first_time_path; |
||
70 | /** |
||
71 | * @since 1.2.2 |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $_menu_exists; |
||
76 | |||
77 | #endregion Properties |
||
78 | |||
79 | /** |
||
80 | * @var FS_Logger |
||
81 | */ |
||
82 | protected $_logger; |
||
83 | |||
84 | #region Singleton |
||
85 | |||
86 | /** |
||
87 | * @var FS_Admin_Menu_Manager[] |
||
88 | */ |
||
89 | private static $_instances = array(); |
||
90 | |||
91 | /** |
||
92 | * @param string $plugin_slug |
||
93 | * |
||
94 | * @return FS_Admin_Menu_Manager |
||
95 | */ |
||
96 | static function instance( $plugin_slug ) { |
||
103 | |||
104 | protected function __construct( $plugin_slug ) { |
||
109 | |||
110 | #endregion Singleton |
||
111 | |||
112 | #region Helpers |
||
113 | |||
114 | private function get_option( &$options, $key, $default = false ) { |
||
117 | |||
118 | private function get_bool_option( &$options, $key, $default = false ) { |
||
121 | |||
122 | #endregion Helpers |
||
123 | |||
124 | /** |
||
125 | * @param array $menu |
||
126 | * @param bool $is_addon |
||
127 | */ |
||
128 | function init( $menu, $is_addon = false ) { |
||
180 | |||
181 | /** |
||
182 | * Check if top level menu. |
||
183 | * |
||
184 | * @author Vova Feldman (@svovaf) |
||
185 | * @since 1.1.3 |
||
186 | * |
||
187 | * @return bool False if submenu item. |
||
188 | */ |
||
189 | function is_top_level() { |
||
192 | |||
193 | /** |
||
194 | * Check if the page should be override on exact URL match. |
||
195 | * |
||
196 | * @author Vova Feldman (@svovaf) |
||
197 | * @since 1.1.3 |
||
198 | * |
||
199 | * @return bool False if submenu item. |
||
200 | */ |
||
201 | function is_override_exact() { |
||
204 | |||
205 | |||
206 | /** |
||
207 | * Get the path of the page the user should be forwarded to after first activation. |
||
208 | * |
||
209 | * @author Vova Feldman (@svovaf) |
||
210 | * @since 1.1.3 |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | function get_first_time_path() { |
||
217 | |||
218 | /** |
||
219 | * Check if plugin's menu item is part of a custom top level menu. |
||
220 | * |
||
221 | * @author Vova Feldman (@svovaf) |
||
222 | * @since 1.1.3 |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | function has_custom_parent() { |
||
229 | |||
230 | /** |
||
231 | * @author Leo Fajardo (@leorw) |
||
232 | * @since 1.2.2 |
||
233 | * |
||
234 | * @return bool |
||
235 | */ |
||
236 | function has_menu() { |
||
239 | |||
240 | /** |
||
241 | * @author Vova Feldman (@svovaf) |
||
242 | * @since 1.1.3 |
||
243 | * |
||
244 | * @param string $id |
||
245 | * @param bool $default |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | function is_submenu_item_visible( $id, $default = true ) { |
||
261 | |||
262 | /** |
||
263 | * Calculates admin settings menu slug. |
||
264 | * If plugin's menu slug is a file (e.g. CPT), uses plugin's slug as the menu slug. |
||
265 | * |
||
266 | * @author Vova Feldman (@svovaf) |
||
267 | * @since 1.1.3 |
||
268 | * |
||
269 | * @param string $page |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | function get_slug( $page = '' ) { |
||
278 | |||
279 | /** |
||
280 | * Check if module has a menu slug set. |
||
281 | * |
||
282 | * @author Vova Feldman (@svovaf) |
||
283 | * @since 1.2.1.6 |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | function has_menu_slug() { |
||
290 | |||
291 | /** |
||
292 | * @author Vova Feldman (@svovaf) |
||
293 | * @since 1.1.3 |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | function get_parent_slug() { |
||
300 | |||
301 | /** |
||
302 | * @author Vova Feldman (@svovaf) |
||
303 | * @since 1.1.3 |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | function get_type() { |
||
310 | |||
311 | /** |
||
312 | * @author Vova Feldman (@svovaf) |
||
313 | * @since 1.1.3 |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | function is_cpt() { |
||
323 | |||
324 | /** |
||
325 | * @author Vova Feldman (@svovaf) |
||
326 | * @since 1.1.3 |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | function get_parent_type() { |
||
333 | |||
334 | /** |
||
335 | * @author Vova Feldman (@svovaf) |
||
336 | * @since 1.1.3 |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | function get_raw_slug() { |
||
343 | |||
344 | /** |
||
345 | * Get plugin's original menu slug. |
||
346 | * |
||
347 | * @author Vova Feldman (@svovaf) |
||
348 | * @since 1.1.3 |
||
349 | * |
||
350 | * @return string |
||
351 | */ |
||
352 | function get_original_menu_slug() { |
||
365 | |||
366 | /** |
||
367 | * @author Vova Feldman (@svovaf) |
||
368 | * @since 1.1.3 |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | function get_top_level_menu_slug() { |
||
377 | |||
378 | /** |
||
379 | * Is user on plugin's admin activation page. |
||
380 | * |
||
381 | * @author Vova Feldman (@svovaf) |
||
382 | * @since 1.0.8 |
||
383 | * |
||
384 | * @return bool |
||
385 | */ |
||
386 | function is_main_settings_page() { |
||
401 | |||
402 | #region Submenu Override |
||
403 | |||
404 | /** |
||
405 | * Override submenu's action. |
||
406 | * |
||
407 | * @author Vova Feldman (@svovaf) |
||
408 | * @since 1.1.0 |
||
409 | * |
||
410 | * @param string $parent_slug |
||
411 | * @param string $menu_slug |
||
412 | * @param callable $function |
||
413 | * |
||
414 | * @return false|string If submenu exist, will return the hook name. |
||
415 | */ |
||
416 | function override_submenu_action( $parent_slug, $menu_slug, $function ) { |
||
449 | |||
450 | #endregion Submenu Override |
||
451 | |||
452 | #region Top level menu Override |
||
453 | |||
454 | /** |
||
455 | * Find plugin's admin dashboard main menu item. |
||
456 | * |
||
457 | * @author Vova Feldman (@svovaf) |
||
458 | * @since 1.0.2 |
||
459 | * |
||
460 | * @return string[]|false |
||
461 | */ |
||
462 | private function find_top_level_menu() { |
||
489 | |||
490 | /** |
||
491 | * Find plugin's admin dashboard main submenu item. |
||
492 | * |
||
493 | * @author Vova Feldman (@svovaf) |
||
494 | * @since 1.2.1.6 |
||
495 | * |
||
496 | * @return array|false |
||
497 | */ |
||
498 | private function find_main_submenu() { |
||
532 | |||
533 | /** |
||
534 | * Remove all sub-menu items. |
||
535 | * |
||
536 | * @author Vova Feldman (@svovaf) |
||
537 | * @since 1.0.7 |
||
538 | * |
||
539 | * @return bool If submenu with plugin's menu slug was found. |
||
540 | */ |
||
541 | private function remove_all_submenu_items() { |
||
554 | |||
555 | /** |
||
556 | * |
||
557 | * @author Vova Feldman (@svovaf) |
||
558 | * @since 1.0.9 |
||
559 | * |
||
560 | * @return false|array[string]mixed |
||
561 | */ |
||
562 | function remove_menu_item() { |
||
580 | |||
581 | /** |
||
582 | * @author Vova Feldman (@svovaf) |
||
583 | * @since 1.1.4 |
||
584 | * |
||
585 | * @param callable $function |
||
586 | * |
||
587 | * @return false|array[string]mixed |
||
588 | */ |
||
589 | function override_menu_item( $function ) { |
||
623 | |||
624 | /** |
||
625 | * Adds a counter to the module's top level menu item. |
||
626 | * |
||
627 | * @author Vova Feldman (@svovaf) |
||
628 | * @since 1.2.1.5 |
||
629 | * |
||
630 | * @param int $counter |
||
631 | * @param string $class |
||
632 | */ |
||
633 | function add_counter_to_menu_item( $counter = 1, $class = '' ) { |
||
665 | |||
666 | #endregion Top level menu Override |
||
667 | |||
668 | /** |
||
669 | * Add a top-level menu page. |
||
670 | * |
||
671 | * Note for WordPress.org Theme/Plugin reviewer: |
||
672 | * |
||
673 | * This is a replication of `add_menu_page()` to avoid Theme Check warning. |
||
674 | * |
||
675 | * Why? |
||
676 | * ==== |
||
677 | * Freemius is an SDK for plugin and theme developers. Since the core |
||
678 | * of the SDK is relevant both for plugins and themes, for obvious reasons, |
||
679 | * we only develop and maintain one code base. |
||
680 | * |
||
681 | * This method will not run for wp.org themes (only plugins) since theme |
||
682 | * admin settings/options are now only allowed in the customizer. |
||
683 | * |
||
684 | * If you have any questions or need clarifications, please don't hesitate |
||
685 | * pinging me on slack, my username is @svovaf. |
||
686 | * |
||
687 | * @author Vova Feldman (@svovaf) |
||
688 | * @since 1.2.2 |
||
689 | * |
||
690 | * @param string $page_title The text to be displayed in the title tags of the page when the menu is |
||
691 | * selected. |
||
692 | * @param string $menu_title The text to be used for the menu. |
||
693 | * @param string $capability The capability required for this menu to be displayed to the user. |
||
694 | * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
||
695 | * @param callable|string $function The function to be called to output the content for this page. |
||
696 | * @param string $icon_url The URL to the icon to be used for this menu. |
||
697 | * * Pass a base64-encoded SVG using a data URI, which will be colored to |
||
698 | * match the color scheme. This should begin with |
||
699 | * 'data:image/svg+xml;base64,'. |
||
700 | * * Pass the name of a Dashicons helper class to use a font icon, |
||
701 | * e.g. 'dashicons-chart-pie'. |
||
702 | * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added |
||
703 | * via CSS. |
||
704 | * @param int $position The position in the menu order this one should appear. |
||
705 | * |
||
706 | * @return string The resulting page's hook_suffix. |
||
707 | */ |
||
708 | static function add_page( |
||
729 | |||
730 | /** |
||
731 | * Add a submenu page. |
||
732 | * |
||
733 | * Note for WordPress.org Theme/Plugin reviewer: |
||
734 | * |
||
735 | * This is a replication of `add_submenu_page()` to avoid Theme Check warning. |
||
736 | * |
||
737 | * Why? |
||
738 | * ==== |
||
739 | * Freemius is an SDK for plugin and theme developers. Since the core |
||
740 | * of the SDK is relevant both for plugins and themes, for obvious reasons, |
||
741 | * we only develop and maintain one code base. |
||
742 | * |
||
743 | * This method will not run for wp.org themes (only plugins) since theme |
||
744 | * admin settings/options are now only allowed in the customizer. |
||
745 | * |
||
746 | * If you have any questions or need clarifications, please don't hesitate |
||
747 | * pinging me on slack, my username is @svovaf. |
||
748 | * |
||
749 | * @author Vova Feldman (@svovaf) |
||
750 | * @since 1.2.2 |
||
751 | * |
||
752 | * @param string $parent_slug The slug name for the parent menu (or the file name of a standard |
||
753 | * WordPress admin page). |
||
754 | * @param string $page_title The text to be displayed in the title tags of the page when the menu is |
||
755 | * selected. |
||
756 | * @param string $menu_title The text to be used for the menu. |
||
757 | * @param string $capability The capability required for this menu to be displayed to the user. |
||
758 | * @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu). |
||
759 | * @param callable|string $function The function to be called to output the content for this page. |
||
760 | * |
||
761 | * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability |
||
762 | * required. |
||
763 | */ |
||
764 | static function add_subpage( |
||
782 | } |
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.