Complex classes like Collecting 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 Collecting, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | trait Collecting { |
||
14 | /** |
||
15 | * Getting of HTML, JS and CSS files list to be included |
||
16 | * |
||
17 | * @param Config $Config |
||
18 | * |
||
19 | * @return string[][] |
||
20 | */ |
||
21 | 4 | protected function get_includes_list ($Config) { |
|
40 | /** |
||
41 | * @param string $base_dir |
||
42 | * @param string[][] $includes |
||
43 | */ |
||
44 | 4 | protected function fill_includes ($base_dir, &$includes) { |
|
49 | /** |
||
50 | * @param string $base_dir |
||
51 | * @param string $ext |
||
52 | * |
||
53 | * @return array |
||
54 | */ |
||
55 | 4 | protected function fill_includes_internal ($base_dir, $ext) { |
|
58 | /** |
||
59 | * Get dependencies of components between each other (only that contains some HTML, JS and CSS files) and mapping HTML, JS and CSS files to URL paths |
||
60 | * |
||
61 | * @param Config $Config |
||
62 | * |
||
63 | * @return array[] [$dependencies, $includes_map] |
||
64 | */ |
||
65 | 4 | protected function get_includes_dependencies_and_map ($Config) { |
|
106 | /** |
||
107 | * Process meta information and corresponding entries to dependencies and functionalities |
||
108 | * |
||
109 | * @param string $base_dir |
||
110 | * @param array $dependencies |
||
111 | * @param array $functionalities |
||
112 | * @param bool $skip_functionalities |
||
113 | */ |
||
114 | 4 | protected function process_meta ($base_dir, &$dependencies, &$functionalities, $skip_functionalities = false) { |
|
158 | /** |
||
159 | * Process map structure, fill includes map and remove files from list of all includes (remaining files will be included on all pages) |
||
160 | * |
||
161 | * @param string $base_dir |
||
162 | * @param array $includes_map |
||
163 | * @param array $all_includes |
||
164 | */ |
||
165 | 4 | protected function process_map ($base_dir, &$includes_map, &$all_includes) { |
|
171 | /** |
||
172 | * Process map structure, fill includes map and remove files from list of all includes (remaining files will be included on all pages) |
||
173 | * |
||
174 | * @param array $map |
||
175 | * @param string $includes_dir |
||
176 | * @param array $includes_map |
||
177 | * @param array $all_includes |
||
178 | */ |
||
179 | 4 | protected function process_map_internal ($map, $includes_dir, &$includes_map, &$all_includes) { |
|
180 | 4 | foreach ($map as $path => $files) { |
|
181 | 4 | foreach ((array)$files as $file) { |
|
182 | 4 | $extension = file_extension($file); |
|
183 | 4 | if (in_array($extension, ['css', 'js', 'html'])) { |
|
184 | 4 | $file = "$includes_dir/$extension/$file"; |
|
185 | 4 | $includes_map[$path][$extension][] = $file; |
|
186 | 4 | $all_includes[$extension] = array_diff($all_includes[$extension], [$file]); |
|
187 | } else { |
||
188 | 4 | $file = rtrim($file, '*'); |
|
189 | /** |
||
190 | * Wildcard support, it is possible to specify just path prefix and all files with this prefix will be included |
||
191 | */ |
||
192 | 4 | $found_files = array_filter( |
|
193 | 4 | get_files_list($includes_dir, '/.*\.(css|js|html)$/i', 'f', '', true, 'name', '!include') ?: [], |
|
194 | 4 | function ($f) use ($file) { |
|
195 | // We need only files with specified mask and only those located in directory that corresponds to file's extension |
||
196 | return preg_match("#^(css|js|html)/$file.*\\1$#i", $f); |
||
197 | 4 | } |
|
198 | ); |
||
199 | // Drop first level directory |
||
200 | 4 | $found_files = _preg_replace('#^[^/]+/(.*)#', '$1', $found_files); |
|
201 | 4 | $this->process_map_internal([$path => $found_files], $includes_dir, $includes_map, $all_includes); |
|
202 | } |
||
203 | } |
||
204 | } |
||
205 | 4 | } |
|
206 | /** |
||
207 | * Replace functionalities by real packages names, take into account recursive dependencies |
||
208 | * |
||
209 | * @param array $dependencies |
||
210 | * @param array $functionalities |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | 4 | protected function normalize_dependencies ($dependencies, $functionalities) { |
|
265 | /** |
||
266 | * Convert array of arbitrary nested structure into flat array |
||
267 | * |
||
268 | * @param array $array |
||
269 | * |
||
270 | * @return string[] |
||
271 | */ |
||
272 | 4 | protected function array_flatten ($array) { |
|
280 | /** |
||
281 | * Includes array is composed from dependencies and sometimes dependencies doesn't have any files, so we'll clean that |
||
282 | * |
||
283 | * @param array $dependencies |
||
284 | * @param array $includes_map |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | 4 | protected function clean_includes_arrays_without_files ($dependencies, $includes_map) { |
|
299 | } |
||
300 |