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 JS assets |
||
53 | */ |
||
54 | const FOLDER_JS = 'js'; |
||
55 | |||
56 | /** |
||
57 | * Name of folder that contains CSS assets |
||
58 | */ |
||
59 | const FOLDER_CSS = 'css'; |
||
60 | |||
61 | /** |
||
62 | * Name of folder that contains images |
||
63 | */ |
||
64 | const FOLDER_IMAGE = 'image'; |
||
65 | |||
66 | /** |
||
67 | * Zip class instance |
||
68 | * @var \gplcart\core\helpers\Zip $zip |
||
69 | */ |
||
70 | protected $zip; |
||
71 | |||
72 | /** |
||
73 | * Full path to a ZIP file containing generated module |
||
74 | * @var string |
||
75 | */ |
||
76 | protected $file; |
||
77 | |||
78 | /** |
||
79 | * Constructor |
||
80 | * @param ZipHelper $zip |
||
81 | */ |
||
82 | public function __construct(ZipHelper $zip) |
||
88 | |||
89 | /** |
||
90 | * Returns an array of licenses and their URLs |
||
91 | * @return array |
||
92 | */ |
||
93 | public function getLicenses() |
||
103 | |||
104 | /** |
||
105 | * Generates module files and folders |
||
106 | * @param array $data |
||
107 | */ |
||
108 | public function generate(array $data) |
||
121 | |||
122 | /** |
||
123 | * Creates various structure elements |
||
124 | * @param string $folder |
||
125 | * @param array $data |
||
126 | */ |
||
127 | protected function createStructure($folder, array $data) |
||
155 | |||
156 | /** |
||
157 | * Creates asset structure |
||
158 | * @param string $folder |
||
159 | * @param array $data |
||
160 | */ |
||
161 | protected function createStructureAsset($folder, array $data) |
||
171 | |||
172 | /** |
||
173 | * Creates a controller class |
||
174 | * @param string $folder |
||
175 | * @param array $data |
||
176 | */ |
||
177 | protected function createStructureController($folder, array $data) |
||
184 | |||
185 | /** |
||
186 | * Cretates module main class |
||
187 | * @param string $folder |
||
188 | * @param array $data |
||
189 | * @return boolean |
||
190 | */ |
||
191 | protected function createMainClass($folder, array $data) |
||
197 | |||
198 | /** |
||
199 | * Creates a model class |
||
200 | * @param string $folder |
||
201 | * @param array $data |
||
202 | */ |
||
203 | protected function createStructureModel($folder, array $data) |
||
210 | |||
211 | /** |
||
212 | * Creates a helper class |
||
213 | * @param string $folder |
||
214 | * @param array $data |
||
215 | */ |
||
216 | protected function createStructureHelper($folder, array $data) |
||
223 | |||
224 | /** |
||
225 | * Creates a handler class |
||
226 | * @param string $folder |
||
227 | * @param array $data |
||
228 | */ |
||
229 | protected function createStructureHandler($folder, array $data) |
||
236 | |||
237 | /** |
||
238 | * Creates module overrides |
||
239 | * @param string $folder |
||
240 | * @param array $data |
||
241 | */ |
||
242 | protected function createStructureOverride($folder, array $data) |
||
249 | |||
250 | /** |
||
251 | * Creates a template sample |
||
252 | * @param string $folder |
||
253 | * @param array $data |
||
254 | */ |
||
255 | protected function createStructureTemplate($folder, array $data) |
||
262 | |||
263 | /** |
||
264 | * Pack module folder into zip file |
||
265 | * @param string $folder |
||
266 | * @param array $data |
||
267 | * @return bool |
||
268 | */ |
||
269 | protected function createZip($folder, array $data) |
||
276 | |||
277 | /** |
||
278 | * Returns a path to zip file |
||
279 | * @return string |
||
280 | */ |
||
281 | public function getZip() |
||
285 | |||
286 | /** |
||
287 | * Returns a full path to the module folder |
||
288 | * @param integer $module_id |
||
289 | * @return string |
||
290 | */ |
||
291 | protected function getModuleFolder($module_id) |
||
295 | |||
296 | /** |
||
297 | * Recursively creates folders |
||
298 | * @param string $folder |
||
299 | * @return boolean |
||
300 | */ |
||
301 | protected function prepareFolder($folder) |
||
305 | |||
306 | /** |
||
307 | * Prepares an array of data before rendering |
||
308 | * @param array $data |
||
309 | * @return string |
||
310 | */ |
||
311 | protected function prepareData(array &$data) |
||
320 | |||
321 | /** |
||
322 | * Renders a template |
||
323 | * @param string $template |
||
324 | * @param array $data |
||
325 | * @return string|null |
||
326 | */ |
||
327 | protected function render($template, array $data) |
||
340 | |||
341 | /** |
||
342 | * Writes to a file using a template as an source |
||
343 | * @param string $file |
||
344 | * @param string $template |
||
345 | * @param array $data |
||
346 | * @param bool $php |
||
347 | * @return null |
||
348 | */ |
||
349 | protected function write($file, $template, array $data, $php = true) |
||
363 | |||
364 | } |
||
365 |
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.