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 SubLanguageManager 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 SubLanguageManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class SubLanguageManager |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Constructor |
||
| 12 | */ |
||
| 13 | public function __construct() |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Get all the languages |
||
| 19 | * @return Array All information about sub-language |
||
| 20 | */ |
||
| 21 | public static function getAllLanguages() |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Get all files of lang folder (forum.inc.php,gradebook.inc.php,notebook.inc.php) |
||
| 37 | * @param String The lang path folder (/var/www/my_lms/main/lang/spanish) |
||
| 38 | * @param bool true if we only want the "subname" trad4all instead of trad4all.inc.php |
||
| 39 | * |
||
| 40 | * @return Array All file of lang folder |
||
| 41 | */ |
||
| 42 | public static function get_lang_folder_files_list($path, $only_main_name = false) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get all information of sub-language |
||
| 66 | * @param Integer The parent id(Language father id) |
||
| 67 | * @param Integer The sub language id |
||
| 68 | * @return Array All information about sub-language |
||
| 69 | */ |
||
| 70 | View Code Duplication | public static function get_all_information_of_sub_language($parent_id, $sub_language_id) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Get all information of language |
||
| 88 | * @param Integer The parent id(Language father id) |
||
| 89 | * @return Array All information about language |
||
| 90 | */ |
||
| 91 | View Code Duplication | public static function get_all_information_of_language($parent_id) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get all information of chamilo file |
||
| 106 | * @param String The chamilo path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php) |
||
| 107 | * @patam Bool Whether we want to remove the '$' prefix in the results or not |
||
| 108 | * @return Array Contains all information of chamilo file |
||
| 109 | */ |
||
| 110 | public static function get_all_language_variable_in_file($system_path_file, $get_as_string_index = false) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Add file in sub-language directory and add header(tag php) |
||
| 135 | * @param String The chamilo path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php) |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | public static function add_file_in_language_directory($system_path_file) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Write in file of sub-language |
||
| 147 | * @param String The path file (/var/www/chamilo/main/lang/spanish/gradebook.inc.php) |
||
| 148 | * @param String The new sub-language |
||
| 149 | * @param String The language variable |
||
| 150 | * @return Boolean True on success, False on error |
||
| 151 | */ |
||
| 152 | public static function write_data_in_file($path_file, $new_term, $new_variable) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Add directory for sub-language |
||
| 172 | * @param string $sub_language_dir The sub-language directory ( e.g. 'spanish_corporate' ) |
||
| 173 | * @return boolean True on success, false on failure |
||
| 174 | */ |
||
| 175 | public static function add_language_directory($sub_language_dir) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Delete sub-language. |
||
| 190 | * In order to avoid deletion of main laguages, we check the existence of a parent |
||
| 191 | * @param int The parent id |
||
| 192 | * @return bool True on success, false on error |
||
| 193 | */ |
||
| 194 | public static function remove_sub_language($parent_id, $sub_language_id) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Remove directory for sub-language |
||
| 224 | * @param String The sub-language path directory ( e.g. 'spanish_corporate'' ) |
||
| 225 | * @return boolean True on success, false on failure |
||
| 226 | */ |
||
| 227 | public static function remove_language_directory($sub_language_dir) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * check if language exist by id |
||
| 251 | * @param int $language_id |
||
| 252 | * @return bool |
||
| 253 | */ |
||
| 254 | View Code Duplication | public static function check_if_exist_language_by_id($language_id) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Get name of language by id |
||
| 274 | * @param Integer The language id |
||
| 275 | * @return String The original name of language |
||
| 276 | */ |
||
| 277 | public static function get_name_of_language_by_id($language_id) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Verified if language is sub-language |
||
| 293 | * @param int $language_id |
||
| 294 | * |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | View Code Duplication | public static function check_if_language_is_sub_language($language_id) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param int $language_id |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | public static function check_if_language_is_used($language_id) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Verified if language is father of an sub-language |
||
| 331 | * @param Integer The language id |
||
| 332 | * @return Boolean |
||
| 333 | */ |
||
| 334 | View Code Duplication | public static function check_if_language_is_father($language_id) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Make unavailable the language |
||
| 350 | * @param Integer The language id |
||
| 351 | * @return void() |
||
| 352 | */ |
||
| 353 | View Code Duplication | public static function make_unavailable_language($language_id) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Make available the language |
||
| 365 | * @param Integer The language id |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | View Code Duplication | public static function make_available_language($language_id) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Set platform language |
||
| 380 | * @param Integer The language id |
||
| 381 | * @return bool |
||
| 382 | */ |
||
| 383 | public static function set_platform_language($language_id) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get platform language ID |
||
| 408 | * @return int The platform language ID |
||
| 409 | */ |
||
| 410 | View Code Duplication | public static function get_platform_language_id() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Get parent language path (or null if no parent) |
||
| 426 | * @param string Children language path |
||
| 427 | * @return string Parent language path or null |
||
| 428 | */ |
||
| 429 | View Code Duplication | public static function get_parent_language_path($language_path) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Get language matching isocode |
||
| 449 | * @param string $isocode The language isocode (en, es, fr, zh-TW, etc) |
||
| 450 | * @return mixed English name of the matching language, or false if no active language could be found |
||
| 451 | */ |
||
| 452 | View Code Duplication | public static function getLanguageFromIsocode($isocode) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Get best language in browser preferences |
||
| 474 | * @param string $preferences The browser-configured language preferences (e.g. "en,es;q=0.7;en-us;q=0.3", etc) |
||
| 475 | * @return mixed English name of the matching language, or false if no active language could be found |
||
| 476 | */ |
||
| 477 | public static function getLanguageFromBrowserPreference($preferences) |
||
| 503 | } |
||
| 504 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.