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 | * @var string |
||
15 | */ |
||
16 | private static $webpack_variables_file_location = 'themes/webpack-variables.js'; |
||
17 | |||
18 | /** |
||
19 | * we need this method because Requirements_Backend does not extend Object! |
||
20 | * @param string |
||
21 | */ |
||
22 | public static function set_webpack_variables_file_location($str) |
||
23 | { |
||
24 | self::$webpack_variables_file_location = $tr; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * IMPORTANT ... you will use this one more than others ... |
||
29 | * e.g. /mysite/javascript/test.js |
||
30 | * @var array |
||
31 | */ |
||
32 | private static $files_to_ignore = []; |
||
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() |
||
199 | |||
200 | |||
201 | /** |
||
202 | * @return string |
||
203 | */ |
||
204 | protected static function webpack_theme_folder_for_customisation() |
||
208 | |||
209 | |||
210 | /** |
||
211 | * Update the given HTML content with the appropriate include tags for the registered |
||
212 | * requirements. Needs to receive a valid HTML/XHTML template in the $content parameter, |
||
213 | * including a head and body tag. |
||
214 | * |
||
215 | * @param string $templateFile No longer used, only retained for compatibility |
||
216 | * @param string $content HTML content that has already been parsed from the $templateFile |
||
217 | * through {@link SSViewer} |
||
218 | * @return string HTML content augmented with the requirements tags |
||
219 | */ |
||
220 | public function includeInHTML($templateFile, $content) |
||
323 | |||
324 | /** |
||
325 | * Attach requirements inclusion to X-Include-JS and X-Include-CSS headers on the given |
||
326 | * HTTP Response |
||
327 | * |
||
328 | * @param SS_HTTPResponse $response |
||
329 | */ |
||
330 | public function include_in_response(SS_HTTPResponse $response) |
||
340 | |||
341 | /** |
||
342 | * |
||
343 | * |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | protected function canSaveRequirements() |
||
360 | |||
361 | /** |
||
362 | * |
||
363 | * |
||
364 | * @return bool |
||
365 | */ |
||
366 | protected function themedRequest() |
||
370 | |||
371 | /** |
||
372 | * |
||
373 | * @param string $fileLocation |
||
374 | * @param string $folderLocation |
||
375 | * |
||
376 | */ |
||
377 | protected function moveFileToRequirementsFolder($fileLocation, $folderLocation) |
||
404 | |||
405 | protected function copyIfYouCan($from, $to, $count = 0) |
||
417 | |||
418 | protected function addLinesToFile($fileLocation, $line, $count = 0) |
||
441 | |||
442 | protected function makeFolderWritable($fileLocation) |
||
451 | |||
452 | |||
453 | public static function flush() |
||
491 | } |
||
492 |
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.