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 Sensei_Main 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 Sensei_Main, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Sensei_Main { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var string |
||
| 15 | * Reference to the main plugin file |
||
| 16 | */ |
||
| 17 | private $file; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var Sensei_Main $_instance to the the main and only instance of the Sensei class. |
||
| 21 | * @since 1.8.0 |
||
| 22 | */ |
||
| 23 | protected static $_instance = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Main reference to the plugins current version |
||
| 27 | */ |
||
| 28 | public $version; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Public token, referencing for the text domain. |
||
| 32 | */ |
||
| 33 | public $token = 'woothemes-sensei'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Plugin url and path for use when access resources. |
||
| 37 | */ |
||
| 38 | public $plugin_url; |
||
| 39 | public $plugin_path; |
||
| 40 | public $template_url; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var Sensei_PostTypes |
||
| 44 | * All Sensei sub classes. Currently used to access functionality contained within |
||
| 45 | * within Sensei sub classes e.g. Sensei()->course->all_courses() |
||
| 46 | */ |
||
| 47 | public $post_types; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var WooThemes_Sensei_Settings |
||
| 51 | */ |
||
| 52 | public $settings; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var WooThemes_Sensei_Course_Results |
||
| 56 | */ |
||
| 57 | public $course_results; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var Sensei_Updates |
||
| 61 | */ |
||
| 62 | public $updates; |
||
| 63 | /** |
||
| 64 | * @var WooThemes_Sensei_Course |
||
| 65 | */ |
||
| 66 | public $course; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var WooThemes_Sensei_Lesson |
||
| 70 | */ |
||
| 71 | public $lesson; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var WooThemes_Sensei_Quiz |
||
| 75 | */ |
||
| 76 | public $quiz; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var WooThemes_Sensei_Question |
||
| 80 | */ |
||
| 81 | public $question; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var WooThemes_Sensei_Admin |
||
| 85 | */ |
||
| 86 | public $admin; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var WooThemes_Sensei_Frontend |
||
| 90 | */ |
||
| 91 | public $frontend; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var Sensei_Notices |
||
| 95 | */ |
||
| 96 | public $notices; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var WooThemes_Sensei_Grading |
||
| 100 | */ |
||
| 101 | public $grading; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var WooThemes_Sensei_Emails |
||
| 105 | */ |
||
| 106 | public $emails; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var WooThemes_Sensei_Learner_Profiles |
||
| 110 | */ |
||
| 111 | public $learner_profiles; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var Sensei_Teacher |
||
| 115 | */ |
||
| 116 | public $teacher; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var WooThemes_Sensei_Learners |
||
| 120 | */ |
||
| 121 | public $learners; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var array |
||
| 125 | * Global instance for access to the permissions message shown |
||
| 126 | * when users do not have the right privileges to access resources. |
||
| 127 | */ |
||
| 128 | public $permissions_message; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var Sensei_Core_Modules Sensei Modules functionality |
||
| 132 | */ |
||
| 133 | public $modules; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var Sensei_Analysis |
||
| 137 | */ |
||
| 138 | public $analysis; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Constructor method. |
||
| 142 | * @param string $file The base file of the plugin. |
||
| 143 | * @since 1.0.0 |
||
| 144 | */ |
||
| 145 | public function __construct ( $file ) { |
||
| 146 | |||
| 147 | // Setup object data |
||
| 148 | $this->file = $file; |
||
| 149 | $this->plugin_url = trailingslashit( plugins_url( '', $plugin = $file ) ); |
||
| 150 | $this->plugin_path = trailingslashit( dirname( $file ) ); |
||
| 151 | $this->template_url = apply_filters( 'sensei_template_url', 'sensei/' ); |
||
| 152 | $this->permissions_message = array( 'title' => __( 'Permission Denied', 'woothemes-sensei' ), 'message' => __( 'Unfortunately you do not have permissions to access this page.', 'woothemes-sensei' ) ); |
||
| 153 | |||
| 154 | // Initialize the core Sensei functionality |
||
| 155 | $this->init(); |
||
| 156 | |||
| 157 | // Installation |
||
| 158 | if ( is_admin() && ! defined( 'DOING_AJAX' ) ) $this->install(); |
||
| 159 | |||
| 160 | // Run this on activation. |
||
| 161 | register_activation_hook( $this->file, array( $this, 'activation' ) ); |
||
| 162 | |||
| 163 | // Image Sizes |
||
| 164 | $this->init_image_sizes(); |
||
| 165 | |||
| 166 | // load all hooks |
||
| 167 | $this->load_hooks(); |
||
| 168 | |||
| 169 | } // End __construct() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Load the foundations of Sensei. |
||
| 173 | * @since 1.9.0 |
||
| 174 | */ |
||
| 175 | protected function init(){ |
||
| 176 | |||
| 177 | // Localisation |
||
| 178 | $this->load_plugin_textdomain(); |
||
| 179 | add_action( 'init', array( $this, 'load_localisation' ), 0 ); |
||
| 180 | |||
| 181 | // Setup settings |
||
| 182 | $this->settings = new Sensei_Settings(); |
||
| 183 | |||
| 184 | // load the shortcode loader into memory, so as to listen to all for |
||
| 185 | // all shortcodes on the front end |
||
| 186 | new Sensei_Shortcode_Loader(); |
||
| 187 | |||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Global Sensei Instance |
||
| 192 | * |
||
| 193 | * Ensure that only one instance of the main Sensei class can be loaded. |
||
| 194 | * |
||
| 195 | * @since 1.8.0 |
||
| 196 | * @static |
||
| 197 | * @see WC() |
||
| 198 | * @return WooThemes_Sensei Instance. |
||
| 199 | */ |
||
| 200 | public static function instance() { |
||
| 201 | |||
| 202 | if ( is_null( self::$_instance ) ) { |
||
| 203 | |||
| 204 | //Sensei requires a reference to the main Sensei plugin file |
||
| 205 | $sensei_main_plugin_file = dirname ( dirname( __FILE__ ) ) . '/woothemes-sensei.php'; |
||
| 206 | |||
| 207 | self::$_instance = new self( $sensei_main_plugin_file ); |
||
| 208 | |||
| 209 | // load the global class objects needed throughout Sensei |
||
| 210 | self::$_instance->initialize_global_objects(); |
||
| 211 | |||
| 212 | } |
||
| 213 | |||
| 214 | return self::$_instance; |
||
| 215 | |||
| 216 | } // end instance() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * This function is linked into the activation |
||
| 220 | * hook to reset flush the urls to ensure Sensei post types show up. |
||
| 221 | * |
||
| 222 | * @since 1.9.0 |
||
| 223 | * |
||
| 224 | * @param $plugin |
||
| 225 | */ |
||
| 226 | public static function activation_flush_rules( $plugin ){ |
||
| 227 | |||
| 228 | if( strpos( $plugin, '/woothemes-sensei.php' ) > 0 ){ |
||
| 229 | |||
| 230 | flush_rewrite_rules(true); |
||
| 231 | |||
| 232 | } |
||
| 233 | |||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Cloning is forbidden. |
||
| 238 | * @since 1.8.0 |
||
| 239 | */ |
||
| 240 | public function __clone() { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Unserializing instances of this class is forbidden. |
||
| 246 | * @since 1.8.0 |
||
| 247 | */ |
||
| 248 | public function __wakeup() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Load the properties for the main Sensei object |
||
| 254 | * |
||
| 255 | * @since 1.9.0 |
||
| 256 | */ |
||
| 257 | public function initialize_global_objects(){ |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Initialize all Sensei hooks |
||
| 328 | * |
||
| 329 | * @since 1.9.0 |
||
| 330 | */ |
||
| 331 | public function load_hooks(){ |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Run Sensei updates. |
||
| 351 | * @access public |
||
| 352 | * @since 1.1.0 |
||
| 353 | * @return void |
||
| 354 | */ |
||
| 355 | public function run_updates() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Register the widgets. |
||
| 366 | * @access public |
||
| 367 | * @since 1.0.0 |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | public function register_widgets () { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Load the plugin's localisation file. |
||
| 390 | * @access public |
||
| 391 | * @since 1.0.0 |
||
| 392 | * @return void |
||
| 393 | */ |
||
| 394 | public function load_localisation () { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Load the plugin textdomain from the main WordPress "languages" folder. |
||
| 402 | * @access public |
||
| 403 | * @since 1.0.0 |
||
| 404 | * @return void |
||
| 405 | */ |
||
| 406 | public function load_plugin_textdomain () { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Run on activation. |
||
| 418 | * @access public |
||
| 419 | * @since 1.0.0 |
||
| 420 | * @return void |
||
| 421 | */ |
||
| 422 | public function activation () { |
||
| 427 | |||
| 428 | |||
| 429 | /** |
||
| 430 | * Register activation hooks. |
||
| 431 | * @access public |
||
| 432 | * @since 1.0.0 |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | public function install () { |
||
| 441 | |||
| 442 | |||
| 443 | /** |
||
| 444 | * Run on activation of the plugin. |
||
| 445 | * @access public |
||
| 446 | * @since 1.0.0 |
||
| 447 | * @return void |
||
| 448 | */ |
||
| 449 | public function activate_sensei () { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Register the plugin's version. |
||
| 458 | * @access public |
||
| 459 | * @since 1.0.0 |
||
| 460 | * @return void |
||
| 461 | */ |
||
| 462 | private function register_plugin_version () { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Ensure that "post-thumbnails" support is available for those themes that don't register it. |
||
| 472 | * @access public |
||
| 473 | * @since 1.0.1 |
||
| 474 | * @return void |
||
| 475 | */ |
||
| 476 | public function ensure_post_thumbnails_support () { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * template_loader function. |
||
| 484 | * |
||
| 485 | * @access public |
||
| 486 | * @param mixed $template |
||
| 487 | * @return void |
||
| 488 | * @deprecated |
||
| 489 | */ |
||
| 490 | public function template_loader ( $template = '' ) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Determine the relative path to the plugin's directory. |
||
| 499 | * @access public |
||
| 500 | * @since 1.0.0 |
||
| 501 | * @return string $sensei_plugin_path |
||
| 502 | */ |
||
| 503 | public function plugin_path () { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Retrieve the ID of a specified page setting. |
||
| 521 | * @access public |
||
| 522 | * @since 1.0.0 |
||
| 523 | * @param string $page |
||
| 524 | * @return int |
||
| 525 | */ |
||
| 526 | public function get_page_id ( $page ) { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * check_user_permissions function. |
||
| 533 | * |
||
| 534 | * @access public |
||
| 535 | * @param string $page (default: '') |
||
| 536 | * |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | public function check_user_permissions ( $page = '' ) { |
||
| 703 | |||
| 704 | |||
| 705 | /** |
||
| 706 | * Check if visitors have access permission. If the "access_permission" setting is active, do a log in check. |
||
| 707 | * @since 1.0.0 |
||
| 708 | * @access public |
||
| 709 | * @return bool |
||
| 710 | */ |
||
| 711 | public function access_settings () { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * load_class loads in class files |
||
| 728 | * @since 1.2.0 |
||
| 729 | * @access public |
||
| 730 | * @return void |
||
| 731 | */ |
||
| 732 | public function load_class ( $class_name = '' ) { |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Filtering wp_count_comments to ensure that Sensei comments are ignored |
||
| 740 | * @since 1.4.0 |
||
| 741 | * @access public |
||
| 742 | * @param array $comments |
||
| 743 | * @param integer $post_id |
||
| 744 | * @return array |
||
| 745 | */ |
||
| 746 | public function sensei_count_comments( $comments, $post_id ) { |
||
| 792 | |||
| 793 | /** |
||
| 794 | * Init images. |
||
| 795 | * |
||
| 796 | * @since 1.4.5 |
||
| 797 | * @access public |
||
| 798 | * @return void |
||
| 799 | */ |
||
| 800 | public function init_image_sizes() { |
||
| 811 | |||
| 812 | /** |
||
| 813 | * Get an image size. |
||
| 814 | * |
||
| 815 | * Variable is filtered by sensei_get_image_size_{image_size} |
||
| 816 | * |
||
| 817 | * @since 1.4.5 |
||
| 818 | * @access public |
||
| 819 | * @param mixed $image_size |
||
| 820 | * @return string |
||
| 821 | */ |
||
| 822 | public function get_image_size( $image_size ) { |
||
| 850 | |||
| 851 | public function body_class( $classes ) { |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Checks that the Jetpack Beautiful Maths module has been activated to support LaTeX within question titles and answers |
||
| 860 | * |
||
| 861 | * @return null |
||
| 862 | * @since 1.7.0 |
||
| 863 | */ |
||
| 864 | public function jetpack_latex_support() { |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Load the module functionality. |
||
| 873 | * |
||
| 874 | * This function is hooked into plugins_loaded to avoid conflicts with |
||
| 875 | * the retired modules extension. |
||
| 876 | * |
||
| 877 | * @since 1.8.0 |
||
| 878 | */ |
||
| 879 | public function load_modules_class(){ |
||
| 896 | |||
| 897 | /** |
||
| 898 | * Tell the user to that the modules extension is no longer needed. |
||
| 899 | * |
||
| 900 | * @since 1.8.0 |
||
| 901 | */ |
||
| 902 | public function disable_sensei_modules_extension(){ ?> |
||
| 915 | |||
| 916 | /** |
||
| 917 | * Sensei wide rewrite flush call. |
||
| 918 | * |
||
| 919 | * To use this simply update the option 'sensei_flush_rewrite_rules' to 1 |
||
| 920 | * |
||
| 921 | * After the option is one the Rules will be flushed. |
||
| 922 | * |
||
| 923 | * @since 1.9.0 |
||
| 924 | */ |
||
| 925 | public function flush_rewrite_rules(){ |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Calling this function will tell Sensei to flush rewrite |
||
| 947 | * rules on the next load. |
||
| 948 | * |
||
| 949 | * @since 1.9.0 |
||
| 950 | */ |
||
| 951 | public function initiate_rewrite_rules_flush(){ |
||
| 956 | |||
| 957 | /** |
||
| 958 | * sensei_woocommerce_email_course_details adds detail to email |
||
| 959 | * |
||
| 960 | * @deprecated since 1.9.0 use Sensei_WC::email_course_details |
||
| 961 | * |
||
| 962 | * @since 1.4.5 |
||
| 963 | * @access public |
||
| 964 | * @param WC_Order $order |
||
| 965 | * |
||
| 966 | * @return void |
||
| 967 | */ |
||
| 968 | public function sensei_woocommerce_email_course_details( $order ) { |
||
| 973 | |||
| 974 | /** |
||
| 975 | * @deprecated since 1.9.0, movde to the Sensei_WC class |
||
| 976 | * @param $user_id |
||
| 977 | * @param $subscription_key |
||
| 978 | */ |
||
| 979 | public function sensei_woocommerce_reactivate_subscription( $user_id, $subscription_key ){ |
||
| 983 | |||
| 984 | /** |
||
| 985 | * @deprecated since 1.9.0, movde to the Sensei_WC class |
||
| 986 | * @param $user_id |
||
| 987 | * @param $subscription_key |
||
| 988 | */ |
||
| 989 | public function sensei_woocommerce_subscription_ended( $user_id, $subscription_key ){ |
||
| 993 | |||
| 994 | /** |
||
| 995 | * sensei_woocommerce_complete_order description |
||
| 996 | * |
||
| 997 | * @deprecated since 1.9.0 use Sensei_WC::complete_order( $order_id ); |
||
| 998 | * @since 1.0.3 |
||
| 999 | * @access public |
||
| 1000 | * @param int $order_id WC order ID |
||
| 1001 | * |
||
| 1002 | * @return void |
||
| 1003 | */ |
||
| 1004 | public function sensei_woocommerce_complete_order ( $order_id = 0 ) { |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * Runs when an order is cancelled. |
||
| 1012 | * |
||
| 1013 | * @deprecated since 1.9.0 |
||
| 1014 | * |
||
| 1015 | * @since 1.2.0 |
||
| 1016 | * @param integer $order_id order ID |
||
| 1017 | * @return void |
||
| 1018 | */ |
||
| 1019 | public function sensei_woocommerce_cancel_order ( $order_id ) { |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * sensei_activate_subscription runs when a subscription product is purchased |
||
| 1027 | * @deprecated since 1.9.0 |
||
| 1028 | * @since 1.2.0 |
||
| 1029 | * @access public |
||
| 1030 | * @param integer $order_id order ID |
||
| 1031 | * @return void |
||
| 1032 | */ |
||
| 1033 | public function sensei_activate_subscription( $order_id = 0 ) { |
||
| 1038 | |||
| 1039 | /** |
||
| 1040 | * If WooCommerce is activated and the customer has purchased the course, update Sensei to indicate that they are taking the course. |
||
| 1041 | * @deprecated since 1.9.0 |
||
| 1042 | * @since 1.0.0 |
||
| 1043 | * @param int $course_id (default: 0) |
||
| 1044 | * @param array/Object $order_user (default: array()) Specific user's data. |
||
| 1045 | * @return bool|int |
||
| 1046 | */ |
||
| 1047 | public function woocommerce_course_update ( $course_id = 0, $order_user = array() ) { |
||
| 1052 | |||
| 1053 | /** |
||
| 1054 | * Returns the WooCommerce Product Object |
||
| 1055 | * |
||
| 1056 | * The code caters for pre and post WooCommerce 2.2 installations. |
||
| 1057 | * |
||
| 1058 | * @deprecated since 1.9.0 |
||
| 1059 | * @since 1.1.1 |
||
| 1060 | * |
||
| 1061 | * @param integer $wc_product_id Product ID or Variation ID |
||
| 1062 | * @param string $product_type '' or 'variation' |
||
| 1063 | * |
||
| 1064 | * @return WC_Product $wc_product_object |
||
| 1065 | */ |
||
| 1066 | public function sensei_get_woocommerce_product_object ( $wc_product_id = 0, $product_type = '' ) { |
||
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Setup required WooCommerce settings. |
||
| 1074 | * @access public |
||
| 1075 | * @since 1.1.0 |
||
| 1076 | * @return void |
||
| 1077 | */ |
||
| 1078 | public function set_woocommerce_functionality() { |
||
| 1083 | |||
| 1084 | /** |
||
| 1085 | * Disable guest checkout if a course product is in the cart |
||
| 1086 | * @deprecated since 1.9.0 |
||
| 1087 | * @param boolean $guest_checkout Current guest checkout setting |
||
| 1088 | * @return boolean Modified guest checkout setting |
||
| 1089 | */ |
||
| 1090 | public function disable_guest_checkout( $guest_checkout ) { |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Change order status with virtual products to completed |
||
| 1098 | * |
||
| 1099 | * @deprecated since 1.9.0 use Sensei_WC::virtual_order_payment_complete( $order_status, $order_id ) |
||
| 1100 | * |
||
| 1101 | * @since 1.1.0 |
||
| 1102 | * @param string $order_status |
||
| 1103 | * @param int $order_id |
||
| 1104 | * @return string |
||
| 1105 | **/ |
||
| 1106 | public function virtual_order_payment_complete( $order_status, $order_id ) { |
||
| 1110 | |||
| 1111 | } // End Class |
||
| 1112 | |||
| 1119 |
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: