Complex classes like Translator 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 Translator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Translator extends Controller |
||
20 | { |
||
21 | /** |
||
22 | * Translator model class instance |
||
23 | * @var \gplcart\modules\translator\models\Translator $translator |
||
24 | */ |
||
25 | protected $translator; |
||
26 | |||
27 | /** |
||
28 | * File transfer model class instance |
||
29 | * @var \gplcart\core\models\FileTransfer $file_transfer |
||
30 | */ |
||
31 | protected $file_transfer; |
||
32 | |||
33 | /** |
||
34 | * An array of translation strings |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $data_content; |
||
38 | |||
39 | /** |
||
40 | * The current language |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $data_language; |
||
44 | |||
45 | /** |
||
46 | * Translator constructor. |
||
47 | * @param FileTransfer $file_transfer |
||
48 | * @param TranslatorModel $translator |
||
49 | */ |
||
50 | public function __construct(FileTransfer $file_transfer, TranslatorModel $translator) |
||
57 | |||
58 | /** |
||
59 | * Page callback |
||
60 | * Displays the compiled files overview page |
||
61 | * @param $langcode |
||
62 | */ |
||
63 | public function compiledFilesTranslator($langcode) |
||
67 | |||
68 | /** |
||
69 | * Page callback |
||
70 | * Displays the file overview page |
||
71 | * @param string $langcode |
||
72 | * @param string $tab |
||
73 | */ |
||
74 | public function filesTranslator($langcode = null, $tab = '') |
||
89 | |||
90 | /** |
||
91 | * Returns a sorted array of languages |
||
92 | * @return array |
||
93 | */ |
||
94 | protected function getLanguagesTranslator() |
||
100 | |||
101 | /** |
||
102 | * Download a translation file |
||
103 | */ |
||
104 | protected function downloadFileTranslator() |
||
117 | |||
118 | /** |
||
119 | * Returns an array of file data |
||
120 | * @param string $hash |
||
121 | * @return array |
||
122 | */ |
||
123 | protected function parseFileHash($hash) |
||
139 | |||
140 | /** |
||
141 | * Parses a hash string containing the module ID and translation file |
||
142 | * @param string $hash |
||
143 | * @return array |
||
144 | */ |
||
145 | protected function parseHashTranslator($hash) |
||
165 | |||
166 | /** |
||
167 | * Applies an action to the selected translation files |
||
168 | */ |
||
169 | protected function actionFilesTranslator() |
||
185 | |||
186 | /** |
||
187 | * Deletes a translation file |
||
188 | * @param string $id |
||
189 | * @return boolean |
||
190 | */ |
||
191 | protected function deleteFileTranslator($id) |
||
201 | |||
202 | /** |
||
203 | * Returns an array of files for the language |
||
204 | * @param string $tab |
||
205 | * @return array |
||
206 | */ |
||
207 | protected function getFilesTranslator($tab) |
||
215 | |||
216 | /** |
||
217 | * Sets titles on the file overview page |
||
218 | */ |
||
219 | protected function setTitleFilesTranslator() |
||
223 | |||
224 | /** |
||
225 | * Sets breadcrumbs on the files overview page |
||
226 | */ |
||
227 | protected function setBreadcrumbFilesTranslator() |
||
236 | |||
237 | /** |
||
238 | * Page callback |
||
239 | * Displays the upload translation page |
||
240 | * @param string $langcode |
||
241 | */ |
||
242 | public function uploadTranslator($langcode) |
||
257 | |||
258 | /** |
||
259 | * Returns an array of sorted modules |
||
260 | * @return array |
||
261 | */ |
||
262 | protected function getModulesTranslator() |
||
268 | |||
269 | /** |
||
270 | * Controls permissions to upload a translation file |
||
271 | */ |
||
272 | protected function controlAccessUploadTranslator() |
||
277 | |||
278 | /** |
||
279 | * Sets titles on the upload translation page |
||
280 | */ |
||
281 | protected function setTitleUploadTranslator() |
||
285 | |||
286 | /** |
||
287 | * Sets breadcrumbs on the upload translation page |
||
288 | */ |
||
289 | protected function setBreadcrumbUploadTranslator() |
||
298 | |||
299 | /** |
||
300 | * Handles submission of translation file |
||
301 | */ |
||
302 | protected function submitUploadTranslator() |
||
309 | |||
310 | /** |
||
311 | * Validates a uploaded translation file |
||
312 | * @return boolean |
||
313 | */ |
||
314 | protected function validateUploadTranslator() |
||
322 | |||
323 | /** |
||
324 | * Validates scope of uploaded file |
||
325 | * @return boolean |
||
326 | */ |
||
327 | protected function validateUploadScopeTranslator() |
||
339 | |||
340 | /** |
||
341 | * Validates uploaded translation file |
||
342 | * @return boolean|null |
||
343 | */ |
||
344 | protected function validateUploadFileTranslator() |
||
367 | |||
368 | /** |
||
369 | * Check and try to fix malformed HTML in the uploaded file |
||
370 | */ |
||
371 | protected function normalizeHtml() |
||
390 | |||
391 | /** |
||
392 | * Copy a uploaded translation |
||
393 | */ |
||
394 | protected function copyFileTranslator() |
||
410 | |||
411 | /** |
||
412 | * Render and output the upload translation page |
||
413 | */ |
||
414 | protected function outputUploadTranslator() |
||
418 | |||
419 | /** |
||
420 | * Render and output the file overview page |
||
421 | */ |
||
422 | protected function outputFilesTranslator() |
||
426 | |||
427 | /** |
||
428 | * Returns an array of primary translations |
||
429 | * @return array |
||
430 | */ |
||
431 | protected function getOriginalFilesTranslator() |
||
449 | |||
450 | /** |
||
451 | * Returns an array of compiled translation files |
||
452 | * @return array |
||
453 | */ |
||
454 | protected function getCompiledFilesTranslator() |
||
468 | |||
469 | /** |
||
470 | * Build translation file info |
||
471 | * @param string $file |
||
472 | * @return array |
||
473 | */ |
||
474 | protected function buildFileInfoTranslator($file) |
||
500 | |||
501 | /** |
||
502 | * Sets the current language |
||
503 | * @param string $langcode |
||
504 | */ |
||
505 | protected function setLanguageTranslator($langcode) |
||
517 | |||
518 | /** |
||
519 | * Page callback |
||
520 | * Displays the translation view page |
||
521 | * @param string $langcode |
||
522 | * @param string $id |
||
523 | */ |
||
524 | public function viewTranslator($langcode, $id) |
||
536 | |||
537 | /** |
||
538 | * Read content from a translation file |
||
539 | * @param string $hash |
||
540 | */ |
||
541 | protected function setContentTranslator($hash) |
||
560 | |||
561 | /** |
||
562 | * Sets titles on the translation view page |
||
563 | */ |
||
564 | protected function setTitleViewTranslator() |
||
568 | |||
569 | /** |
||
570 | * Sets breadcrumbs on the translation view page |
||
571 | */ |
||
572 | protected function setBreadcrumbViewTranslator() |
||
581 | |||
582 | /** |
||
583 | * Render and output the translation view page |
||
584 | */ |
||
585 | protected function outputViewTranslator() |
||
589 | |||
590 | } |
||
591 |