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.6.2'; |
||
| 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() { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Insures we always return the same object. |
||
| 33 | * |
||
| 34 | * @return Plugin |
||
| 35 | */ |
||
| 36 | public static function get_instance() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Initialize the plugin. |
||
| 47 | */ |
||
| 48 | public function plugins_loaded() { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Check plugin requirements. |
||
| 69 | * |
||
| 70 | * @return bool True is fails requirements. False otherwise. |
||
| 71 | */ |
||
| 72 | public function maybe_self_deactivate() { |
||
| 73 | |||
| 74 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-setup.php' ); |
||
| 75 | |||
| 76 | View Code Duplication | if ( false === \HMBKP_Setup::meets_requirements() ) { |
|
| 77 | |||
| 78 | add_action( 'admin_init', array( '\HMBKP_Setup', 'self_deactivate' ) ); |
||
| 79 | |||
| 80 | add_action( 'all_admin_notices', array( '\HMBKP_Setup', 'display_admin_notices' ) ); |
||
| 81 | |||
| 82 | return true; |
||
| 83 | |||
| 84 | } |
||
| 85 | |||
| 86 | return false; |
||
| 87 | |||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Define all the constants. |
||
| 92 | */ |
||
| 93 | public function constants() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Load all BackUpWordPress functions. |
||
| 124 | */ |
||
| 125 | protected function includes() { |
||
| 126 | |||
| 127 | require_once( HMBKP_PLUGIN_PATH . 'vendor/autoload.php' ); |
||
| 128 | |||
| 129 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-notices.php' ); |
||
| 130 | |||
| 131 | // Load Whitelist HTML submodule and admin required functions. |
||
| 132 | require_once( HMBKP_PLUGIN_PATH . 'whitelist-html/whitelist-html.php' ); |
||
| 133 | require_once( HMBKP_PLUGIN_PATH . 'admin/menu.php' ); |
||
| 134 | require_once( HMBKP_PLUGIN_PATH . 'admin/actions.php' ); |
||
| 135 | |||
| 136 | // Load Backdrop if necessary. |
||
| 137 | if ( ! class_exists( 'HM_Backdrop_Task' ) ) { |
||
| 138 | require_once( HMBKP_PLUGIN_PATH . 'backdrop/hm-backdrop.php' ); |
||
| 139 | } |
||
| 140 | |||
| 141 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-requirements.php' ); |
||
| 142 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-requirement.php' ); |
||
| 143 | |||
| 144 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-path.php' ); |
||
| 145 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-excludes.php' ); |
||
| 146 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-site-size.php' ); |
||
| 147 | |||
| 148 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-utilities.php' ); |
||
| 149 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-status.php' ); |
||
| 150 | |||
| 151 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine.php' ); |
||
| 152 | |||
| 153 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database.php' ); |
||
| 154 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database-mysqldump.php' ); |
||
| 155 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-database-imysqldump.php' ); |
||
| 156 | |||
| 157 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file.php' ); |
||
| 158 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file-zip.php' ); |
||
| 159 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup-engine-file-zip-archive.php' ); |
||
| 160 | |||
| 161 | require_once( HMBKP_PLUGIN_PATH . 'classes/backup/class-backup.php' ); |
||
| 162 | |||
| 163 | // Load the backup scheduling classes |
||
| 164 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-scheduled-backup.php' ); |
||
| 165 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-schedules.php' ); |
||
| 166 | |||
| 167 | // Load the core functions |
||
| 168 | require_once( HMBKP_PLUGIN_PATH . 'functions/core.php' ); |
||
| 169 | require_once( HMBKP_PLUGIN_PATH . 'functions/interface.php' ); |
||
| 170 | |||
| 171 | // Load the services |
||
| 172 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-services.php' ); |
||
| 173 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-service.php' ); |
||
| 174 | |||
| 175 | // Load the email service |
||
| 176 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-email-service.php' ); |
||
| 177 | |||
| 178 | // Load the webhook services |
||
| 179 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-webhook-service.php' ); |
||
| 180 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-wpremote-webhook-service.php' ); |
||
| 181 | |||
| 182 | require_once( HMBKP_PLUGIN_PATH . 'classes/deprecated.php' ); |
||
| 183 | |||
| 184 | require_once( HMBKP_PLUGIN_PATH . 'classes/class-extensions.php' ); |
||
| 185 | |||
| 186 | // Load the wp cli command |
||
| 187 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
||
| 188 | include( HMBKP_PLUGIN_PATH . 'classes/class-backupwordpress-wp-cli-command.php' ); |
||
| 189 | } |
||
| 190 | |||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Hook into WordPress page lifecycle and execute BackUpWordPress functions. |
||
| 195 | */ |
||
| 196 | public function hooks() { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Load the Javascript in the admin. |
||
| 216 | * |
||
| 217 | * @param $hook The name of the admin page hook. |
||
| 218 | */ |
||
| 219 | public function scripts( $hook ) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Loads the plugin text domain for translation. |
||
| 254 | * This setup allows a user to just drop his custom translation files into the WordPress language directory |
||
| 255 | * Files will need to be in a subdirectory with the name of the textdomain 'backupwordpress' |
||
| 256 | */ |
||
| 257 | public function text_domain() { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Determine if we need to run an upgrade routine. |
||
| 278 | */ |
||
| 279 | public function upgrade() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Runs on every admin page load |
||
| 290 | */ |
||
| 291 | public function init() { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Generate a unique key. |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | protected function generate_key() { |
||
| 303 | |||
| 304 | $check = apply_filters( 'hmbkp_generate_key', null ); |
||
| 305 | |||
| 306 | if ( null !== $check ) { |
||
| 307 | return $check; |
||
| 308 | } |
||
| 309 | |||
| 310 | $key = array( ABSPATH, time() ); |
||
| 311 | $constants = array( 'AUTH_KEY', 'SECURE_AUTH_KEY', 'LOGGED_IN_KEY', 'NONCE_KEY', 'AUTH_SALT', 'SECURE_AUTH_SALT', 'LOGGED_IN_SALT', 'NONCE_SALT', 'SECRET_KEY' ); |
||
| 312 | |||
| 313 | foreach ( $constants as $constant ) { |
||
| 314 | if ( defined( $constant ) ) { |
||
| 315 | $key[] = constant( $constant ); |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | shuffle( $key ); |
||
| 320 | |||
| 321 | return md5( serialize( $key ) ); |
||
| 322 | |||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Ensure BackUpWordPress is loaded before add-ons, changes the order of the serialized values in the DB field. |
||
| 327 | */ |
||
| 328 | public function load_first() { |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Function to run when the schedule cron fires. |
||
| 350 | * |
||
| 351 | * @param $schedule_id |
||
| 352 | */ |
||
| 353 | public function schedule_hook_run( $schedule_id ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Enqueue the plugin styles. |
||
| 372 | * |
||
| 373 | * @param $hook |
||
| 374 | */ |
||
| 375 | public function styles( $hook ) { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Load Intercom and send across user information and server info. Only loaded if the user has opted in. |
||
| 393 | * |
||
| 394 | * @param $hook |
||
| 395 | */ |
||
| 396 | public function load_intercom_script() { |
||
| 397 | |||
| 398 | if ( ! get_option( 'hmbkp_enable_support' ) ) { |
||
| 399 | return; |
||
| 400 | } |
||
| 401 | |||
| 402 | $info = array(); |
||
| 403 | |||
| 404 | foreach ( Requirements::get_requirement_groups() as $group ) { |
||
| 405 | foreach ( Requirements::get_requirements( $group ) as $requirement ) { |
||
|
|
|||
| 406 | $info[ $requirement->name() ] = $requirement->result(); |
||
| 407 | } |
||
| 408 | } |
||
| 409 | |||
| 410 | foreach ( Services::get_services() as $file => $service ) { |
||
| 411 | array_merge( $info, call_user_func( array( $service, 'intercom_data' ) ) ); |
||
| 412 | } |
||
| 413 | |||
| 414 | $current_user = wp_get_current_user(); |
||
| 415 | |||
| 416 | $info['user_hash'] = hash_hmac( 'sha256', $current_user->user_email, 'fcUEt7Vi4ym5PXdcr2UNpGdgZTEvxX9NJl8YBTxK' ); |
||
| 417 | $info['email'] = $current_user->user_email; |
||
| 418 | $info['created_at'] = strtotime( $current_user->user_registered ); |
||
| 419 | $info['app_id'] = '7f1l4qyq'; |
||
| 420 | $info['name'] = $current_user->display_name; |
||
| 421 | $info['widget'] = array( 'activator' => '#intercom' ); ?> |
||
| 422 | |||
| 423 | <script id="IntercomSettingsScriptTag"> |
||
| 424 | window.intercomSettings = <?php echo json_encode( $info ); ?>; |
||
| 425 | </script> |
||
| 426 | <script>!function(){function e(){var a=c.createElement("script");a.type="text/javascript",a.async=!0,a.src="https://static.intercomcdn.com/intercom.v1.js";var b=c.getElementsByTagName("script")[0];b.parentNode.insertBefore(a,b)}var a=window,b=a.Intercom;if("function"==typeof b)b("reattach_activator"),b("update",intercomSettings);else{var c=document,d=function(){d.c(arguments)};d.q=[],d.c=function(a){d.q.push(a)},a.Intercom=d,a.attachEvent?a.attachEvent("onload",e):a.addEventListener("load",e,!1)}}();</script> |
||
| 427 | |||
| 428 | <?php } |
||
| 429 | |||
| 430 | public function display_feature_message() { |
||
| 460 | |||
| 461 | } |
||
| 462 | |||
| 463 | if ( is_multisite() && ! is_main_site() ) { |
||
| 464 | return; |
||
| 468 |
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: