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 Jetpack_Admin_Page 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 Jetpack_Admin_Page, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | abstract class Jetpack_Admin_Page { |
||
5 | // Add page specific actions given the page hook |
||
6 | abstract function add_page_actions( $hook ); |
||
7 | |||
8 | // Create a menu item for the page and returns the hook |
||
9 | abstract function get_page_hook(); |
||
10 | |||
11 | // Enqueue and localize page specific scripts |
||
12 | abstract function page_admin_scripts(); |
||
13 | |||
14 | // Render page specific HTML |
||
15 | abstract function page_render(); |
||
16 | |||
17 | /** |
||
18 | * Should we block the page rendering because the site is in IDC? |
||
19 | * @var bool |
||
20 | */ |
||
21 | static $block_page_rendering_for_idc; |
||
|
|||
22 | |||
23 | /** |
||
24 | * Flag to know if we already checked the plan. |
||
25 | * |
||
26 | * @since 4.4.0 |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | static $plan_checked = false; |
||
31 | |||
32 | /** |
||
33 | * Function called after admin_styles to load any additional needed styles. |
||
34 | * |
||
35 | * @since 4.3.0 |
||
36 | */ |
||
37 | function additional_styles() {} |
||
38 | |||
39 | function __construct() { |
||
40 | $this->jetpack = Jetpack::init(); |
||
41 | self::$block_page_rendering_for_idc = ( |
||
42 | Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' ) |
||
43 | ); |
||
44 | } |
||
45 | |||
46 | function add_actions() { |
||
47 | |||
48 | // If user is not an admin and site is in Dev Mode, don't do anything |
||
49 | if ( ! current_user_can( 'manage_options' ) && Jetpack::is_development_mode() ) { |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | // Don't add in the modules page unless modules are available! |
||
54 | if ( $this->dont_show_if_not_active && ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { |
||
55 | return; |
||
56 | } |
||
57 | |||
58 | // Initialize menu item for the page in the admin |
||
59 | $hook = $this->get_page_hook(); |
||
60 | |||
61 | // Attach hooks common to all Jetpack admin pages based on the created |
||
62 | // hook |
||
63 | add_action( "load-$hook", array( $this, 'admin_help' ) ); |
||
64 | add_action( "load-$hook", array( $this, 'admin_page_load' ) ); |
||
65 | add_action( "admin_head-$hook", array( $this, 'admin_head' ) ); |
||
66 | |||
67 | add_action( "admin_print_styles-$hook", array( $this, 'admin_styles' ) ); |
||
68 | add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts' ) ); |
||
69 | |||
70 | if ( ! self::$block_page_rendering_for_idc ) { |
||
71 | add_action( "admin_print_styles-$hook", array( $this, 'additional_styles' ) ); |
||
72 | } |
||
73 | |||
74 | // Check if the site plan changed and deactivate modules accordingly. |
||
75 | add_action( 'current_screen', array( $this, 'check_plan_deactivate_modules' ) ); |
||
76 | |||
77 | // Attach page specific actions in addition to the above |
||
78 | $this->add_page_actions( $hook ); |
||
79 | } |
||
80 | |||
81 | function admin_head() { |
||
96 | |||
97 | // Render the page with a common top and bottom part, and page specific content |
||
98 | function render() { |
||
106 | |||
107 | function admin_help() { |
||
110 | |||
111 | function admin_page_load() { |
||
115 | |||
116 | function admin_page_top() { |
||
119 | |||
120 | function admin_page_bottom() { |
||
123 | |||
124 | // Add page specific scripts and jetpack stats for all menu pages |
||
125 | function admin_scripts() { |
||
129 | |||
130 | // Enqueue the Jetpack admin stylesheet |
||
131 | function admin_styles() { |
||
138 | |||
139 | /** |
||
140 | * Checks if WordPress version is too old to have REST API. |
||
141 | * |
||
142 | * @since 4.3 |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | function is_wp_version_too_old() { |
||
150 | |||
151 | /** |
||
152 | * Checks if REST API is enabled. |
||
153 | * |
||
154 | * @since 4.4.2 |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | function is_rest_api_enabled() { |
||
167 | |||
168 | /** |
||
169 | * Checks the site plan and deactivates modules that were active but are no longer included in the plan. |
||
170 | * |
||
171 | * @since 4.4.0 |
||
172 | * |
||
173 | * @param $page |
||
174 | * |
||
175 | * @return bool|array |
||
176 | */ |
||
177 | function check_plan_deactivate_modules( $page ) { |
||
242 | } |
||
243 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.