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 |
||
| 20 | class Plugin |
||
| 21 | { |
||
| 22 | protected $version = ''; |
||
| 23 | protected $author = ''; |
||
| 24 | protected $fields = []; |
||
| 25 | private $settings = []; |
||
| 26 | // Translation strings. |
||
| 27 | private $strings = null; |
||
| 28 | public $isCoursePlugin = false; |
||
| 29 | public $isAdminPlugin = false; |
||
| 30 | public $isMailPlugin = false; |
||
| 31 | // Adds icon in the course home |
||
| 32 | public $addCourseTool = true; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * When creating a new course, these settings are added to the course, in |
||
| 36 | * the course_info/infocours.php |
||
| 37 | * To show the plugin course icons you need to add these icons: |
||
| 38 | * main/img/icons/22/plugin_name.png |
||
| 39 | * main/img/icons/64/plugin_name.png |
||
| 40 | * main/img/icons/64/plugin_name_na.png |
||
| 41 | * @example |
||
| 42 | * $course_settings = array( |
||
| 43 | array('name' => 'big_blue_button_welcome_message', 'type' => 'text'), |
||
| 44 | array('name' => 'big_blue_button_record_and_store', 'type' => 'checkbox') |
||
| 45 | ); |
||
| 46 | */ |
||
| 47 | public $course_settings = array(); |
||
| 48 | /** |
||
| 49 | * This indicates whether changing the setting should execute the callback |
||
| 50 | * function. |
||
| 51 | */ |
||
| 52 | public $course_settings_callback = false; |
||
| 53 | |||
| 54 | const TAB_FILTER_NO_STUDENT = '::no-student'; |
||
| 55 | const TAB_FILTER_ONLY_STUDENT = '::only-student'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Default constructor for the plugin class. By default, it only sets |
||
| 59 | * a few attributes of the object |
||
| 60 | * @param string $version of this plugin |
||
| 61 | * @param string $author of this plugin |
||
| 62 | * @param array $settings settings to be proposed to configure the plugin |
||
| 63 | */ |
||
| 64 | protected function __construct($version, $author, $settings = array()) |
||
| 65 | { |
||
| 66 | $this->version = $version; |
||
| 67 | $this->author = $author; |
||
| 68 | $this->fields = $settings; |
||
| 69 | |||
| 70 | global $language_files; |
||
| 71 | $language_files[] = 'plugin_'.$this->get_name(); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Gets an array of information about this plugin (name, version, ...) |
||
| 76 | * @return array Array of information elements about this plugin |
||
| 77 | */ |
||
| 78 | public function get_info() |
||
| 79 | { |
||
| 80 | $result = array(); |
||
| 81 | $result['obj'] = $this; |
||
| 82 | $result['title'] = $this->get_title(); |
||
| 83 | $result['comment'] = $this->get_comment(); |
||
| 84 | $result['version'] = $this->get_version(); |
||
| 85 | $result['author'] = $this->get_author(); |
||
| 86 | $result['plugin_class'] = get_class($this); |
||
| 87 | $result['is_course_plugin'] = $this->isCoursePlugin; |
||
| 88 | $result['is_admin_plugin'] = $this->isAdminPlugin; |
||
| 89 | $result['is_mail_plugin'] = $this->isMailPlugin; |
||
| 90 | |||
| 91 | if ($form = $this->get_settings_form()) { |
||
| 92 | $result['settings_form'] = $form; |
||
| 93 | |||
| 94 | foreach ($this->fields as $name => $type) { |
||
| 95 | $value = $this->get($name); |
||
| 96 | |||
| 97 | if (is_array($type)) { |
||
| 98 | $value = $type['options']; |
||
| 99 | } |
||
| 100 | $result[$name] = $value; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | return $result; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Returns the "system" name of the plugin in lowercase letters |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function get_name() |
||
| 112 | { |
||
| 113 | $result = get_class($this); |
||
| 114 | $result = str_replace('Plugin', '', $result); |
||
| 115 | $result = strtolower($result); |
||
| 116 | |||
| 117 | return $result; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return string |
||
| 122 | */ |
||
| 123 | public function getCamelCaseName() |
||
| 124 | { |
||
| 125 | $result = get_class($this); |
||
| 126 | return str_replace('Plugin', '', $result); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns the title of the plugin |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | public function get_title() |
||
| 134 | { |
||
| 135 | return $this->get_lang('plugin_title'); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the description of the plugin |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | public function get_comment() |
||
| 143 | { |
||
| 144 | return $this->get_lang('plugin_comment'); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns the version of the plugin |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function get_version() |
||
| 152 | { |
||
| 153 | return $this->version; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the author of the plugin |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function get_author() |
||
| 161 | { |
||
| 162 | return $this->author; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns the contents of the CSS defined by the plugin |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function get_css() |
||
| 170 | { |
||
| 171 | $name = $this->get_name(); |
||
| 172 | $path = api_get_path(SYS_PLUGIN_PATH)."$name/resources/$name.css"; |
||
| 173 | if (!is_readable($path)) { |
||
| 174 | return ''; |
||
| 175 | } |
||
| 176 | $css = array(); |
||
| 177 | $css[] = file_get_contents($path); |
||
| 178 | $result = implode($css); |
||
| 179 | |||
| 180 | return $result; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns an HTML form (generated by FormValidator) of the plugin settings |
||
| 185 | * @return FormValidator FormValidator-generated form |
||
| 186 | */ |
||
| 187 | public function get_settings_form() |
||
| 188 | { |
||
| 189 | $result = new FormValidator($this->get_name()); |
||
| 190 | |||
| 191 | $defaults = array(); |
||
| 192 | $checkboxGroup = array(); |
||
| 193 | $checkboxCollection = array(); |
||
| 194 | |||
| 195 | if ($checkboxNames = array_keys($this->fields, 'checkbox')) { |
||
| 196 | $pluginInfoCollection = api_get_settings('Plugins'); |
||
| 197 | foreach ($pluginInfoCollection as $pluginInfo) { |
||
| 198 | if (array_search($pluginInfo['title'], $checkboxNames) !== false) { |
||
| 199 | $checkboxCollection[$pluginInfo['title']] = $pluginInfo; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | foreach ($this->fields as $name => $type) { |
||
| 205 | $options = null; |
||
| 206 | View Code Duplication | if (is_array($type) && isset($type['type']) && $type['type'] === 'select') { |
|
| 207 | $options = $type['options']; |
||
| 208 | $type = $type['type']; |
||
| 209 | } |
||
| 210 | |||
| 211 | $value = $this->get($name); |
||
| 212 | $defaults[$name] = $value; |
||
| 213 | $type = isset($type) ? $type : 'text'; |
||
| 214 | |||
| 215 | $help = null; |
||
| 216 | if ($this->get_lang_plugin_exists($name.'_help')) { |
||
| 217 | $help = $this->get_lang($name.'_help'); |
||
| 218 | if ($name === "show_main_menu_tab") { |
||
| 219 | $pluginName = strtolower(str_replace('Plugin', '', get_class($this))); |
||
| 220 | $pluginUrl = api_get_path(WEB_PATH)."plugin/$pluginName/index.php"; |
||
| 221 | $pluginUrl = "<a href=$pluginUrl>$pluginUrl</a>"; |
||
| 222 | $help = sprintf($help, $pluginUrl); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | switch ($type) { |
||
| 227 | case 'html': |
||
| 228 | $result->addElement('html', $this->get_lang($name)); |
||
| 229 | break; |
||
| 230 | case 'wysiwyg': |
||
| 231 | $result->addHtmlEditor($name, $this->get_lang($name), false); |
||
| 232 | break; |
||
| 233 | case 'text': |
||
| 234 | $result->addElement($type, $name, array($this->get_lang($name), $help)); |
||
| 235 | break; |
||
| 236 | case 'boolean': |
||
| 237 | $group = array(); |
||
| 238 | $group[] = $result->createElement('radio', $name, '', get_lang('Yes'), 'true'); |
||
| 239 | $group[] = $result->createElement('radio', $name, '', get_lang('No'), 'false'); |
||
| 240 | $result->addGroup($group, null, array($this->get_lang($name), $help)); |
||
| 241 | break; |
||
| 242 | case 'checkbox': |
||
| 243 | $selectedValue = null; |
||
| 244 | if (isset($checkboxCollection[$name])) { |
||
| 245 | if ($checkboxCollection[$name]['selected_value'] === 'true') { |
||
| 246 | $selectedValue = 'checked'; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | $element = $result->createElement( |
||
| 250 | $type, |
||
| 251 | $name, |
||
| 252 | '', |
||
| 253 | $this->get_lang($name), |
||
| 254 | $selectedValue |
||
| 255 | ); |
||
| 256 | $element->_attributes['value'] = 'true'; |
||
| 257 | $checkboxGroup[] = $element; |
||
| 258 | break; |
||
| 259 | case 'select': |
||
| 260 | $result->addElement( |
||
| 261 | $type, |
||
| 262 | $name, |
||
| 263 | array($this->get_lang($name), $help), |
||
| 264 | $options |
||
| 265 | ); |
||
| 266 | break; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | |||
| 270 | if (!empty($checkboxGroup)) { |
||
| 271 | $result->addGroup($checkboxGroup, null, array($this->get_lang('sms_types'), $help)); |
||
| 272 | } |
||
| 273 | $result->setDefaults($defaults); |
||
| 274 | $result->addButtonSave($this->get_lang('Save'), 'submit_button'); |
||
| 275 | |||
| 276 | return $result; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Returns the value of a given plugin global setting |
||
| 281 | * @param string $name of the plugin |
||
| 282 | * |
||
| 283 | * @return string Value of the plugin |
||
| 284 | */ |
||
| 285 | public function get($name) |
||
| 286 | { |
||
| 287 | $settings = $this->get_settings(); |
||
| 288 | foreach ($settings as $setting) { |
||
| 289 | if ($setting['variable'] == $this->get_name().'_'.$name) { |
||
| 290 | return $setting['selected_value']; |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | return false; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns an array with the global settings for this plugin |
||
| 299 | * @param bool $forceFromDB Optional. Force get settings from the database |
||
| 300 | * @return array Plugin settings as an array |
||
| 301 | */ |
||
| 302 | public function get_settings($forceFromDB = false) |
||
| 303 | { |
||
| 304 | if (empty($this->settings) || $forceFromDB) { |
||
| 305 | $settings = api_get_settings_params( |
||
| 306 | array( |
||
| 307 | "subkey = ? AND category = ? AND type = ? " => array($this->get_name(), 'Plugins', 'setting') |
||
| 308 | ) |
||
| 309 | ); |
||
| 310 | $this->settings = $settings; |
||
| 311 | } |
||
| 312 | |||
| 313 | return $this->settings; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Tells whether language variables are defined for this plugin or not |
||
| 318 | * @param string $name System name of the plugin |
||
| 319 | * |
||
| 320 | * @return bool True if the plugin has language variables defined, false otherwise |
||
| 321 | */ |
||
| 322 | public function get_lang_plugin_exists($name) |
||
| 323 | { |
||
| 324 | return isset($this->strings[$name]); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Hook for the get_lang() function to check for plugin-defined language terms |
||
| 329 | * @param string $name of the language variable we are looking for |
||
| 330 | * |
||
| 331 | * @return string The translated language term of the plugin |
||
| 332 | */ |
||
| 333 | public function get_lang($name) |
||
| 334 | { |
||
| 335 | // Check whether the language strings for the plugin have already been |
||
| 336 | // loaded. If so, no need to load them again. |
||
| 337 | |||
| 338 | if (is_null($this->strings)) { |
||
| 339 | $root = api_get_path(SYS_PLUGIN_PATH); |
||
| 340 | $plugin_name = $this->get_name(); |
||
| 341 | |||
| 342 | $language_interface = api_get_language_isocode(); |
||
| 343 | |||
| 344 | //1. Loading english if exists |
||
| 345 | $english_path = $root.$plugin_name."/lang/en.php"; |
||
| 346 | |||
| 347 | if (is_readable($english_path)) { |
||
| 348 | $strings = array(); |
||
| 349 | include $english_path; |
||
| 350 | $this->strings = $strings; |
||
| 351 | } |
||
| 352 | |||
| 353 | $path = $root.$plugin_name."/lang/$language_interface.php"; |
||
| 354 | // 2. Loading the system language |
||
| 355 | if (is_readable($path)) { |
||
| 356 | include $path; |
||
| 357 | if (!empty($strings)) { |
||
| 358 | foreach ($strings as $key => $string) { |
||
| 359 | $this->strings[$key] = $string; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | if (isset($this->strings[$name])) { |
||
| 366 | return $this->strings[$name]; |
||
| 367 | } |
||
| 368 | |||
| 369 | return get_lang($name); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Caller for the install_course_fields() function |
||
| 374 | * @param int $courseId |
||
| 375 | * |
||
| 376 | * @param boolean $addToolLink Whether to add a tool link on the course homepage |
||
| 377 | * |
||
| 378 | * @return void |
||
| 379 | */ |
||
| 380 | public function course_install($courseId, $addToolLink = true) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Add course settings and, if not asked otherwise, add a tool link on the course homepage |
||
| 387 | * @param int $courseId Course integer ID |
||
| 388 | * @param boolean $add_tool_link Whether to add a tool link or not |
||
| 389 | * (some tools might just offer a configuration section and act on the backend) |
||
| 390 | * |
||
| 391 | * @return boolean|null False on error, null otherwise |
||
| 392 | */ |
||
| 393 | public function install_course_fields($courseId, $add_tool_link = true) |
||
| 394 | { |
||
| 395 | $plugin_name = $this->get_name(); |
||
| 396 | $t_course = Database::get_course_table(TABLE_COURSE_SETTING); |
||
| 397 | $courseId = (int) $courseId; |
||
| 398 | |||
| 467 | |||
| 468 | /** |
||
| 469 | * Add an link for a course tool |
||
| 470 | * @param string $name The tool name |
||
| 471 | * @param int $courseId The course ID |
||
| 472 | * @param string $iconName Optional. Icon file name |
||
| 473 | * @param string $link Optional. Link URL |
||
| 474 | * @return \Chamilo\CourseBundle\Entity\CTool|null |
||
| 475 | */ |
||
| 476 | protected function createLinkToCourseTool($name, $courseId, $iconName = null, $link = null) |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Delete the fields added to the course settings page and the link to the |
||
| 520 | * tool on the course's homepage |
||
| 521 | * @param int $courseId |
||
| 522 | * |
||
| 523 | * @return false|null |
||
| 524 | */ |
||
| 525 | public function uninstall_course_fields($courseId) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Install the course fields and tool link of this plugin in all courses |
||
| 560 | * @param boolean $add_tool_link Whether we want to add a plugin link on the course homepage |
||
| 561 | * |
||
| 562 | * @return void |
||
| 563 | */ |
||
| 564 | View Code Duplication | public function install_course_fields_in_all_courses($add_tool_link = true) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Uninstall the plugin settings fields from all courses |
||
| 577 | * @return void |
||
| 578 | */ |
||
| 579 | View Code Duplication | public function uninstall_course_fields_in_all_courses() |
|
| 590 | |||
| 591 | /** |
||
| 592 | * @return array |
||
| 593 | */ |
||
| 594 | public function getCourseSettings() |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Method to be extended when changing the setting in the course |
||
| 614 | * configuration should trigger the use of a callback method |
||
| 615 | * @param array $values sent back from the course configuration script |
||
| 616 | * |
||
| 617 | * @return void |
||
| 618 | */ |
||
| 619 | public function course_settings_updated($values = array()) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Add a tab to platform |
||
| 626 | * @param string $tabName |
||
| 627 | * @param string $url |
||
| 628 | * @param string $userFilter Optional. Filter tab type |
||
| 629 | * @return false|string |
||
| 630 | */ |
||
| 631 | public function addTab($tabName, $url, $userFilter = null) |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Delete a tab to chamilo's platform |
||
| 704 | * @param string $key |
||
| 705 | * @return boolean $resp Transaction response |
||
| 706 | */ |
||
| 707 | public function deleteTab($key) |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Update the tabs attributes |
||
| 752 | * @param string $key |
||
| 753 | * @param array $attributes |
||
| 754 | * |
||
| 755 | * @return boolean |
||
| 756 | */ |
||
| 757 | public function updateTab($key, $attributes) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * This method shows or hides plugin's tab |
||
| 769 | * @param boolean $showTab Shows or hides the main menu plugin tab |
||
| 770 | * @param string $filePath Plugin starter file path |
||
| 771 | */ |
||
| 772 | public function manageTab($showTab, $filePath = 'index.php') |
||
| 800 | |||
| 801 | /** |
||
| 802 | * @param string $variable |
||
| 803 | * @return bool |
||
| 804 | */ |
||
| 805 | public function validateCourseSetting($variable) |
||
| 809 | |||
| 810 | /** |
||
| 811 | * @param string $region |
||
| 812 | * @return string |
||
| 813 | */ |
||
| 814 | public function renderRegion($region) |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Returns true if the plugin is installed, false otherwise |
||
| 821 | * @return bool True if plugin is installed/enabled, false otherwise |
||
| 822 | */ |
||
| 823 | public function isEnabled() |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Allow make some actions after configure the plugin parameters |
||
| 838 | * This function is called from main/admin/configure_plugin.php page |
||
| 839 | * when saving the plugin parameters |
||
| 840 | * @return \Plugin |
||
| 841 | */ |
||
| 842 | public function performActionsAfterConfigure() |
||
| 846 | } |
||
| 847 |