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 Requirements_Backend_For_Webpack 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 Requirements_Backend_For_Webpack, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Requirements_Backend_For_Webpack extends Requirements_Backend |
||
|
|
|||
| 9 | { |
||
| 10 | |||
| 11 | |||
| 12 | /** |
||
| 13 | * e.g. /mysite/javascript/test.js |
||
| 14 | * @ var array |
||
| 15 | */ |
||
| 16 | private static $files_to_ignore = array(); |
||
| 17 | |||
| 18 | /** |
||
| 19 | * we need this method because Requirements_Backend does not extend Object! |
||
| 20 | * @param array $array |
||
| 21 | */ |
||
| 22 | public static function set_files_to_ignore($array) |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @ var string |
||
| 29 | */ |
||
| 30 | private static $copy_css_to_folder = "source/css/requirements"; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * we need this method because Requirements_Backend does not extend Object! |
||
| 34 | * @param string $string |
||
| 35 | */ |
||
| 36 | public static function set_copy_css_to_folder($string) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @ var string |
||
| 43 | */ |
||
| 44 | private static $copy_js_to_folder = "source/js/requirements"; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * we need this method because Requirements_Backend does not extend Object! |
||
| 48 | * @param string $string |
||
| 49 | */ |
||
| 50 | public static function set_copy_js_to_folder($string) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @ var string |
||
| 57 | */ |
||
| 58 | private static $urls_to_exclude = array(); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * we need this method because Requirements_Backend does not extend Object! |
||
| 62 | * @param array $array |
||
| 63 | */ |
||
| 64 | public static function set_urls_to_exclude($a) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @ var bool |
||
| 75 | */ |
||
| 76 | private static $force_update = true; |
||
| 77 | public static function set_force_update($bool) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Whether to add caching query params to the requests for file-based requirements. |
||
| 88 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 89 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 90 | * while automatically busting this cache every time the file is changed. |
||
| 91 | * |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | protected $suffix_requirements = false; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Whether to combine CSS and JavaScript files |
||
| 98 | * |
||
| 99 | * @var bool |
||
| 100 | */ |
||
| 101 | protected $combined_files_enabled = false; |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * Force the JavaScript to the bottom of the page, even if there's a script tag in the body already |
||
| 106 | * |
||
| 107 | * @var boolean |
||
| 108 | */ |
||
| 109 | protected $force_js_to_bottom = true; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Update the given HTML content with the appropriate include tags for the registered |
||
| 113 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
| 114 | * including a head and body tag. |
||
| 115 | * |
||
| 116 | * @param string $templateFile No longer used, only retained for compatibility |
||
| 117 | * @param string $content HTML content that has already been parsed from the $templateFile |
||
| 118 | * through {@link SSViewer} |
||
| 119 | * @return string HTML content augmented with the requirements tags |
||
| 120 | */ |
||
| 121 | public function includeInHTML($templateFile, $content) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
||
| 227 | * HTTP Response |
||
| 228 | * |
||
| 229 | * @param SS_HTTPResponse $response |
||
| 230 | */ |
||
| 231 | public function include_in_response(SS_HTTPResponse $response) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * |
||
| 244 | * |
||
| 245 | * |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | protected function canSaveRequirements() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | protected function themedRequest() |
||
| 269 | |||
| 270 | protected function moveFileToRequirementsFolder($fileLocation, $folderLocation) |
||
| 313 | } |
||
| 314 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.