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 |
||
| 10 | class Requirements_Backend_For_Webpack extends Requirements_Backend implements flushable |
||
|
|
|||
| 11 | { |
||
| 12 | |||
| 13 | |||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private static $webpack_variables_file_location = 'themes/webpack-variables.js'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * we need this method because Requirements_Backend does not extend Object! |
||
| 21 | * @param string |
||
| 22 | */ |
||
| 23 | public static function set_webpack_variables_file_location($str) |
||
| 27 | |||
| 28 | /** |
||
| 29 | * e.g. /mysite/javascript/test.js |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private static $files_to_ignore = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * we need this method because Requirements_Backend does not extend Object! |
||
| 36 | * @var array $array |
||
| 37 | */ |
||
| 38 | public static function set_files_to_ignore($array) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @return array |
||
| 45 | */ |
||
| 46 | public static function get_files_to_ignore() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private static $working_theme_folder_extension = "mysite"; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * we need this method because Requirements_Backend does not extend Object! |
||
| 58 | * @var string $string |
||
| 59 | */ |
||
| 60 | public static function set_working_theme_folder_extension($string) |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * we need this method because Requirements_Backend does not extend Object! |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public static function get_working_theme_folder_extension() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | private static $copy_css_to_folder = "src/raw_requirements/css"; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * we need this method because Requirements_Backend does not extend Object! |
||
| 82 | * @var string $string |
||
| 83 | */ |
||
| 84 | public static function set_copy_css_to_folder($string) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | private static $copy_js_to_folder = "src/raw_requirements/js"; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * we need this method because Requirements_Backend does not extend Object! |
||
| 96 | * @param string $string |
||
| 97 | */ |
||
| 98 | public static function set_copy_js_to_folder($string) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private static $urls_to_exclude = array(); |
||
| 107 | |||
| 108 | /** |
||
| 109 | * we need this method because Requirements_Backend does not extend Object! |
||
| 110 | * @param array $array |
||
| 111 | */ |
||
| 112 | public static function set_urls_to_exclude($a) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * |
||
| 119 | * @return array |
||
| 120 | */ |
||
| 121 | public static function get_urls_to_exclude() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var bool |
||
| 128 | */ |
||
| 129 | private static $force_update = true; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * |
||
| 133 | * @param bool |
||
| 134 | */ |
||
| 135 | public static function set_force_update($bool) |
||
| 139 | |||
| 140 | |||
| 141 | /** |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public static function get_force_update($bool) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Whether to add caching query params to the requests for file-based requirements. |
||
| 152 | * Eg: themes/myTheme/js/main.js?m=123456789. The parameter is a timestamp generated by |
||
| 153 | * filemtime. This has the benefit of allowing the browser to cache the URL infinitely, |
||
| 154 | * while automatically busting this cache every time the file is changed. |
||
| 155 | * |
||
| 156 | * @var bool |
||
| 157 | */ |
||
| 158 | protected $suffix_requirements = false; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Whether to combine CSS and JavaScript files |
||
| 162 | * |
||
| 163 | * @var bool |
||
| 164 | */ |
||
| 165 | protected $combined_files_enabled = false; |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * Force the JavaScript to the bottom of the page, even if there's a script tag in the body already |
||
| 170 | * |
||
| 171 | * @var boolean |
||
| 172 | */ |
||
| 173 | protected $force_js_to_bottom = true; |
||
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | protected static function webpack_current_theme_as_set_in_db() |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | protected static function webpack_theme_folder_for_customisation() |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * Update the given HTML content with the appropriate include tags for the registered |
||
| 204 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
| 205 | * including a head and body tag. |
||
| 206 | * |
||
| 207 | * @param string $templateFile No longer used, only retained for compatibility |
||
| 208 | * @param string $content HTML content that has already been parsed from the $templateFile |
||
| 209 | * through {@link SSViewer} |
||
| 210 | * @return string HTML content augmented with the requirements tags |
||
| 211 | */ |
||
| 212 | public function includeInHTML($templateFile, $content) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
||
| 318 | * HTTP Response |
||
| 319 | * |
||
| 320 | * @param SS_HTTPResponse $response |
||
| 321 | */ |
||
| 322 | public function include_in_response(SS_HTTPResponse $response) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * |
||
| 335 | * |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | protected function canSaveRequirements() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | protected function themedRequest() |
||
| 360 | |||
| 361 | protected function moveFileToRequirementsFolder($fileLocation, $folderLocation) |
||
| 402 | |||
| 403 | protected $addLinesToFileCount = 0; |
||
| 404 | |||
| 405 | protected function copyIfYouCan($from, $to) |
||
| 415 | |||
| 416 | protected function addLinesToFile($fileLocation, $line) |
||
| 427 | |||
| 428 | protected function makeFolderWritable($fileLocation) |
||
| 440 | |||
| 441 | |||
| 442 | public static function flush() |
||
| 475 | |||
| 476 | |||
| 477 | } |
||
| 478 |
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.