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 Plugin 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 Plugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | final class Plugin { |
||
9 | const PLUGIN_VERSION = '3.5'; |
||
10 | |||
11 | /** |
||
12 | * @var Plugin The singleton instance. |
||
13 | */ |
||
14 | private static $instance; |
||
15 | |||
16 | /** |
||
17 | * Instantiates a new Plugin object. |
||
18 | */ |
||
19 | private function __construct() { |
||
22 | |||
23 | /** |
||
24 | * Insures we always return the same object. |
||
25 | * |
||
26 | * @return Plugin |
||
27 | */ |
||
28 | public static function get_instance() { |
||
36 | |||
37 | /** |
||
38 | * Initialize the plugin. |
||
39 | */ |
||
40 | public function plugins_loaded() { |
||
58 | |||
59 | /** |
||
60 | * Check plugin requirements. |
||
61 | * |
||
62 | * @return bool True is fails requirements. False otherwise. |
||
63 | */ |
||
64 | public function maybe_self_deactivate() { |
||
81 | |||
82 | /** |
||
83 | * Define all the constants. |
||
84 | */ |
||
85 | public function constants() { |
||
113 | |||
114 | /** |
||
115 | * Load all BackUpWordPress functions. |
||
116 | */ |
||
117 | protected function includes() { |
||
118 | |||
119 | require_once( HMBKP_PLUGIN_PATH . 'vendor/autoload.php' ); |
||
120 | |||
121 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-notices.php' ); |
||
122 | |||
123 | // Load the admin menu |
||
124 | require_once( HMBKP_PLUGIN_PATH . 'admin/menu.php' ); |
||
125 | require_once( HMBKP_PLUGIN_PATH . 'admin/actions.php' ); |
||
126 | |||
127 | // Load Backdrop if necessary. |
||
128 | if ( ! class_exists( 'HM_Backdrop_Task' ) ) { |
||
129 | require_once( HMBKP_PLUGIN_PATH . 'backdrop/hm-backdrop.php' ); |
||
130 | } |
||
131 | |||
132 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-requirements.php' ); |
||
133 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-requirement.php' ); |
||
134 | |||
135 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-path.php' ); |
||
136 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-excludes.php' ); |
||
137 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-site-size.php' ); |
||
138 | |||
139 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-utilities.php' ); |
||
140 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-status.php' ); |
||
141 | |||
142 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine.php' ); |
||
143 | |||
144 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database.php' ); |
||
145 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database-mysqldump.php' ); |
||
146 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database-imysqldump.php' ); |
||
147 | |||
148 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file.php' ); |
||
149 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file-zip.php' ); |
||
150 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file-zip-archive.php' ); |
||
151 | |||
152 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup.php' ); |
||
153 | |||
154 | // Load the backup scheduling classes |
||
155 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-scheduled-backup.php' ); |
||
156 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-schedules.php' ); |
||
157 | |||
158 | // Load the core functions |
||
159 | require_once( HMBKP_PLUGIN_PATH . 'functions/core.php' ); |
||
160 | require_once( HMBKP_PLUGIN_PATH . 'functions/interface.php' ); |
||
161 | |||
162 | // Load the services |
||
163 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-services.php' ); |
||
164 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-service.php' ); |
||
165 | |||
166 | // Load the email service |
||
167 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-email-service.php' ); |
||
168 | |||
169 | // Load the webhook services |
||
170 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-webhook-service.php' ); |
||
171 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-wpremote-webhook-service.php' ); |
||
172 | |||
173 | require_once( HMBKP_PLUGIN_PATH . 'classes/deprecated.php' ); |
||
174 | |||
175 | // Load the wp cli command |
||
176 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
177 | include( HMBKP_PLUGIN_PATH . 'classes/class-backupwordpress-wp-cli-command.php' ); |
||
178 | } |
||
179 | |||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Hook into WordPress page lifecycle and execute BackUpWordPress functions. |
||
184 | */ |
||
185 | public function hooks() { |
||
202 | |||
203 | /** |
||
204 | * Load the Javascript in the admin. |
||
205 | * |
||
206 | * @param $hook The name of the admin page hook. |
||
207 | */ |
||
208 | public function scripts( $hook ) { |
||
240 | |||
241 | /** |
||
242 | * Loads the plugin text domain for translation. |
||
243 | * This setup allows a user to just drop his custom translation files into the WordPress language directory |
||
244 | * Files will need to be in a subdirectory with the name of the textdomain 'backupwordpress' |
||
245 | */ |
||
246 | public function text_domain() { |
||
264 | |||
265 | /** |
||
266 | * Determine if we need to run an upgrade routine. |
||
267 | */ |
||
268 | public function upgrade() { |
||
276 | |||
277 | /** |
||
278 | * Runs on every admin page load |
||
279 | */ |
||
280 | public function init() { |
||
285 | |||
286 | /** |
||
287 | * Generate a unique key. |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | protected function generate_key() { |
||
292 | |||
293 | $check = apply_filters( 'hmbkp_generate_key', null ); |
||
294 | |||
295 | if ( null !== $check ) { |
||
296 | return $check; |
||
297 | } |
||
298 | |||
299 | $key = array( ABSPATH, time() ); |
||
300 | $constants = array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT', 'SECRET_KEY' ); |
||
301 | |||
302 | foreach ( $constants as $constant ) { |
||
303 | if ( defined( $constant ) ) { |
||
304 | $key[] = constant( $constant ); |
||
305 | } |
||
306 | } |
||
307 | |||
308 | shuffle( $key ); |
||
309 | |||
310 | return md5( serialize( $key ) ); |
||
311 | |||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Ensure BackUpWordPress is loaded before add-ons, changes the order of the serialized values in the DB field. |
||
316 | */ |
||
317 | public function load_first() { |
||
336 | |||
337 | /** |
||
338 | * Function to run when the schedule cron fires. |
||
339 | * |
||
340 | * @param $schedule_id |
||
341 | */ |
||
342 | public function schedule_hook_run( $schedule_id ) { |
||
358 | |||
359 | /** |
||
360 | * Enqueue the plugin styles. |
||
361 | * |
||
362 | * @param $hook |
||
363 | */ |
||
364 | public function styles( $hook ) { |
||
379 | |||
380 | /** |
||
381 | * Load Intercom and send across user information and server info. Only loaded if the user has opted in. |
||
382 | * |
||
383 | * @param $hook |
||
384 | */ |
||
385 | public function load_intercom_script() { |
||
418 | } |
||
419 | |||
425 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: