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 BackendController |
||
| 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 | * @param FileTransferModel $file_transfer |
||
| 47 | * @param TranslatorModuleModel $translator |
||
| 48 | */ |
||
| 49 | public function __construct(FileTransferModel $file_transfer, TranslatorModuleModel $translator) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Page callback |
||
| 59 | * Displays the compiled files overview page |
||
| 60 | * @param $langcode |
||
| 61 | */ |
||
| 62 | public function compiledFilesTranslator($langcode) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Page callback |
||
| 69 | * Displays the file overview page |
||
| 70 | * @param string $langcode |
||
| 71 | * @param string $tab |
||
| 72 | */ |
||
| 73 | public function filesTranslator($langcode = null, $tab = '') |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Returns a sorted array of languages |
||
| 91 | * @return array |
||
| 92 | */ |
||
| 93 | protected function getLanguagesTranslator() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Download a translation file |
||
| 102 | */ |
||
| 103 | protected function downloadFileTranslator() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns an array of file data |
||
| 119 | * @param string $hash |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | protected function parseFileHash($hash) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Parses a hash string containing the module ID and translation file |
||
| 141 | * @param string $hash |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | protected function parseHashTranslator($hash) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Applies an action to the selected translation files |
||
| 167 | */ |
||
| 168 | protected function actionFilesTranslator() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Deletes a translation file |
||
| 187 | * @param string $id |
||
| 188 | * @return boolean |
||
| 189 | */ |
||
| 190 | protected function deleteFileTranslator($id) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Returns an array of files for the language |
||
| 203 | * @param string $tab |
||
| 204 | * @return array |
||
| 205 | */ |
||
| 206 | protected function getFilesTranslator($tab) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Sets titles on the file overview page |
||
| 217 | */ |
||
| 218 | protected function setTitleFilesTranslator() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Sets breadcrumbs on the files overview page |
||
| 225 | */ |
||
| 226 | protected function setBreadcrumbFilesTranslator() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Page callback |
||
| 238 | * Displays the upload translation page |
||
| 239 | * @param string $langcode |
||
| 240 | */ |
||
| 241 | public function uploadTranslator($langcode) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Returns an array of sorted modules |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | protected function getModulesTranslator() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Controls permissions to upload a translation file |
||
| 270 | */ |
||
| 271 | protected function controlAccessUploadTranslator() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Sets titles on the upload translation page |
||
| 279 | */ |
||
| 280 | protected function setTitleUploadTranslator() |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Sets breadcrumbs on the upload translation page |
||
| 287 | */ |
||
| 288 | protected function setBreadcrumbUploadTranslator() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Handles submission of translation file |
||
| 300 | */ |
||
| 301 | protected function submitUploadTranslator() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Validates a uploaded translation file |
||
| 311 | * @return boolean |
||
| 312 | */ |
||
| 313 | protected function validateUploadTranslator() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Validates scope of uploaded file |
||
| 324 | * @return boolean |
||
| 325 | */ |
||
| 326 | protected function validateUploadScopeTranslator() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Validates uploaded translation file |
||
| 341 | * @return boolean|null |
||
| 342 | */ |
||
| 343 | protected function validateUploadFileTranslator() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Check and try to fix malformed HTML in the uploaded file |
||
| 369 | */ |
||
| 370 | protected function normalizeHtml() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Copy a uploaded translation |
||
| 392 | */ |
||
| 393 | protected function copyFileTranslator() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Render and output the upload translation page |
||
| 412 | */ |
||
| 413 | protected function outputUploadTranslator() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Render and output the file overview page |
||
| 420 | */ |
||
| 421 | protected function outputFilesTranslator() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Returns an array of primary translations |
||
| 428 | * @return array |
||
| 429 | */ |
||
| 430 | protected function getOriginalFilesTranslator() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Returns an array of compiled translation files |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | protected function getCompiledFilesTranslator() |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Build translation file info |
||
| 470 | * @param string $file |
||
| 471 | * @return array |
||
| 472 | */ |
||
| 473 | protected function buildFileInfoTranslator($file) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Sets the current language |
||
| 502 | * @param string $langcode |
||
| 503 | */ |
||
| 504 | protected function setLanguageTranslator($langcode) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Page callback |
||
| 519 | * Displays the translation view page |
||
| 520 | * @param string $langcode |
||
| 521 | * @param string $id |
||
| 522 | */ |
||
| 523 | public function viewTranslator($langcode, $id) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Read content from a translation file |
||
| 538 | * @param string $hash |
||
| 539 | */ |
||
| 540 | protected function setContentTranslator($hash) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Sets titles on the translation view page |
||
| 562 | */ |
||
| 563 | protected function setTitleViewTranslator() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Sets breadcrumbs on the translation view page |
||
| 570 | */ |
||
| 571 | protected function setBreadcrumbViewTranslator() |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Render and output the translation view page |
||
| 583 | */ |
||
| 584 | protected function outputViewTranslator() |
||
| 588 | |||
| 589 | } |
||
| 590 |