Complex classes like Generator 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 Generator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Generator extends Model |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Name of folder that contains module controllers |
||
23 | */ |
||
24 | const FOLDER_CONTROLLER = 'controllers'; |
||
25 | |||
26 | /** |
||
27 | * Name of folder that contains module helpers |
||
28 | */ |
||
29 | const FOLDER_HELPER = 'helpers'; |
||
30 | |||
31 | /** |
||
32 | * Name of folder that contains module models |
||
33 | */ |
||
34 | const FOLDER_MODEL = 'models'; |
||
35 | |||
36 | /** |
||
37 | * Name of folder that contains module handlers |
||
38 | */ |
||
39 | const FOLDER_HANDLER = 'handlers'; |
||
40 | |||
41 | /** |
||
42 | * Name of folder that contains module templates |
||
43 | */ |
||
44 | const FOLDER_TEMPLATE = 'templates'; |
||
45 | |||
46 | /** |
||
47 | * Name of folder that contains module overrides |
||
48 | */ |
||
49 | const FOLDER_OVERRIDE = 'override'; |
||
50 | |||
51 | /** |
||
52 | * Name of folder that contains module translations |
||
53 | */ |
||
54 | const FOLDER_LOCALE = 'locale'; |
||
55 | |||
56 | /** |
||
57 | * Name of folder that contains JS assets |
||
58 | */ |
||
59 | const FOLDER_JS = 'js'; |
||
60 | |||
61 | /** |
||
62 | * Name of folder that contains CSS assets |
||
63 | */ |
||
64 | const FOLDER_CSS = 'css'; |
||
65 | |||
66 | /** |
||
67 | * Name of folder that contains images |
||
68 | */ |
||
69 | const FOLDER_IMAGE = 'image'; |
||
70 | |||
71 | /** |
||
72 | * Zip class instance |
||
73 | * @var \gplcart\core\helpers\Zip $zip |
||
74 | */ |
||
75 | protected $zip; |
||
76 | |||
77 | /** |
||
78 | * Full path to a ZIP file containing generated module |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $file; |
||
82 | |||
83 | /** |
||
84 | * The current module folder |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $folder; |
||
88 | |||
89 | /** |
||
90 | * An array of module data |
||
91 | * @var array |
||
92 | */ |
||
93 | protected $data = array(); |
||
94 | |||
95 | /** |
||
96 | * @param ZipHelper $zip |
||
97 | */ |
||
98 | public function __construct(ZipHelper $zip) |
||
104 | |||
105 | /** |
||
106 | * Returns an array of licenses and their URLs |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getLicenses() |
||
118 | |||
119 | /** |
||
120 | * Generates module files and folders |
||
121 | * @param array $data |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function generate(array $data) |
||
134 | |||
135 | /** |
||
136 | * Create module manifest file |
||
137 | */ |
||
138 | protected function createManifest() |
||
157 | |||
158 | /** |
||
159 | * Creates various structure elements |
||
160 | */ |
||
161 | protected function createStructure() |
||
199 | |||
200 | /** |
||
201 | * Creates asset structure |
||
202 | */ |
||
203 | protected function createStructureAsset() |
||
214 | |||
215 | /** |
||
216 | * Creates locale structure |
||
217 | */ |
||
218 | protected function createStructureLocale() |
||
227 | |||
228 | /** |
||
229 | * Creates a controller class |
||
230 | */ |
||
231 | protected function createStructureController() |
||
239 | |||
240 | /** |
||
241 | * Creates module main class |
||
242 | */ |
||
243 | protected function createMainClass() |
||
249 | |||
250 | /** |
||
251 | * Creates a model class |
||
252 | */ |
||
253 | protected function createStructureModel() |
||
261 | |||
262 | /** |
||
263 | * Creates a helper class |
||
264 | */ |
||
265 | protected function createStructureHelper() |
||
273 | |||
274 | /** |
||
275 | * Creates a handler class |
||
276 | */ |
||
277 | protected function createStructureHandler() |
||
285 | |||
286 | /** |
||
287 | * Creates module overrides |
||
288 | */ |
||
289 | protected function createStructureOverride() |
||
297 | |||
298 | /** |
||
299 | * Creates a template sample |
||
300 | */ |
||
301 | protected function createStructureTemplate() |
||
309 | |||
310 | /** |
||
311 | * Pack module folder into zip file |
||
312 | * @return bool |
||
313 | */ |
||
314 | protected function createZip() |
||
326 | |||
327 | /** |
||
328 | * Returns a path to zip file |
||
329 | * @return string |
||
330 | */ |
||
331 | public function getZip() |
||
335 | |||
336 | /** |
||
337 | * Recursively creates folders |
||
338 | * @param string $folder |
||
339 | * @return bool |
||
340 | */ |
||
341 | protected function prepareFolder($folder) |
||
345 | |||
346 | /** |
||
347 | * Prepares an array of data before rendering |
||
348 | * @param array $data |
||
349 | * @return string |
||
350 | */ |
||
351 | protected function setData(array &$data) |
||
362 | |||
363 | /** |
||
364 | * Renders a template |
||
365 | * @param string $template |
||
366 | * @return string|null |
||
367 | */ |
||
368 | protected function render($template) |
||
381 | |||
382 | /** |
||
383 | * Writes to a file using a template as an source |
||
384 | * @param string $file |
||
385 | * @param string $template |
||
386 | * @return null |
||
387 | */ |
||
388 | protected function write($file, $template) |
||
402 | |||
403 | } |
||
404 |