Complex classes like Pages 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 Pages, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Pages { |
||
24 | |||
25 | /** |
||
26 | * Current settings page. |
||
27 | * |
||
28 | * @access private |
||
29 | * @var string |
||
30 | */ |
||
31 | private $page = ''; |
||
32 | |||
33 | /** |
||
34 | * Default tab. |
||
35 | * |
||
36 | * @access private |
||
37 | * @var string |
||
38 | */ |
||
39 | private $tab = ''; |
||
40 | |||
41 | /** |
||
42 | * Settings pages. |
||
43 | * |
||
44 | * @access private |
||
45 | * @var array |
||
46 | */ |
||
47 | private $settings = array(); |
||
48 | |||
49 | /** |
||
50 | * Constructor. |
||
51 | * |
||
52 | * @since 3.0.0 |
||
53 | * |
||
54 | * @param string $page |
||
55 | */ |
||
56 | public function __construct( $page = 'settings' ) { |
||
57 | |||
58 | $this->page = $page; |
||
59 | $settings_pages = ! is_null( \SimpleCalendar\plugin()->objects ) ? simcal_get_admin_pages() : ''; |
||
60 | $settings_page_tabs = array(); |
||
61 | $tabs = isset( $settings_pages[ $page ] ) ? $settings_pages[ $page ] : false; |
||
62 | |||
63 | if ( $tabs && is_array( $tabs ) ) { |
||
64 | foreach ( $tabs as $tab ) { |
||
65 | |||
66 | $settings_page = simcal_get_admin_page( $tab ); |
||
67 | |||
68 | if ( $settings_page instanceof Admin_Page ) { |
||
69 | $settings_page_tabs[ $settings_page->id ] = $settings_page; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | $this->settings = $settings_page_tabs; |
||
74 | } |
||
75 | |||
76 | // The first tab is the default tab when opening a page. |
||
77 | $this->tab = isset( $tabs[0] ) ? $tabs[0] : ''; |
||
78 | |||
79 | do_action( 'simcal_admin_pages', $page ); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Get settings pages. |
||
84 | * |
||
85 | * @since 3.0.0 |
||
86 | * @access private |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | public function get_settings() { |
||
111 | |||
112 | /** |
||
113 | * Register settings. |
||
114 | * |
||
115 | * Adds settings sections and fields to settings pages. |
||
116 | * |
||
117 | * @since 3.0.0 |
||
118 | * |
||
119 | * @param array $settings |
||
120 | */ |
||
121 | public function register_settings( $settings = array() ) { |
||
195 | |||
196 | /** |
||
197 | * Print Settings Pages. |
||
198 | * |
||
199 | * @since 3.0.0 |
||
200 | */ |
||
201 | public function html() { |
||
286 | |||
287 | } |
||
288 |