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 |
||
27 | class Translator |
||
28 | { |
||
29 | |||
30 | /** |
||
31 | * Hook class instance |
||
32 | * @var \gplcart\core\Hook $hook |
||
33 | */ |
||
34 | protected $hook; |
||
35 | |||
36 | /** |
||
37 | * Config class instance |
||
38 | * @var \gplcart\core\Config $config |
||
39 | */ |
||
40 | protected $config; |
||
41 | |||
42 | /** |
||
43 | * Module class instance |
||
44 | * @var \gplcart\core\Module $module |
||
45 | */ |
||
46 | protected $module; |
||
47 | |||
48 | /** |
||
49 | * Cache class instance |
||
50 | * @var \gplcart\core\Cache $cache |
||
51 | */ |
||
52 | protected $cache; |
||
53 | |||
54 | /** |
||
55 | * File transfer model class instance |
||
56 | * @var \gplcart\core\models\FileTransfer $file_transfer |
||
57 | */ |
||
58 | protected $file_transfer; |
||
59 | |||
60 | /** |
||
61 | * Zip helper class instance |
||
62 | * @var \gplcart\core\helpers\Zip $zip |
||
63 | */ |
||
64 | protected $zip; |
||
65 | |||
66 | /** |
||
67 | * Translation UI model class instance |
||
68 | * @var \gplcart\core\models\Translation $translation |
||
69 | */ |
||
70 | protected $translation; |
||
71 | |||
72 | /** |
||
73 | * Language model class instance |
||
74 | * @var \gplcart\core\models\Language $language |
||
75 | */ |
||
76 | protected $language; |
||
77 | |||
78 | /** |
||
79 | * Translator constructor. |
||
80 | * @param Hook $hook |
||
81 | * @param Config $config |
||
82 | * @param Cache $cache |
||
83 | * @param Module $module |
||
84 | * @param ZipHelper $zip |
||
85 | * @param FileTransferModel $file_transfer |
||
86 | * @param LanguageModel $language |
||
87 | * @param TranslationModel $translation |
||
88 | */ |
||
89 | public function __construct(Hook $hook, Config $config, Cache $cache, Module $module, |
||
103 | |||
104 | /** |
||
105 | * Returns an array of information about the translation file |
||
106 | * @param string $file |
||
107 | * @return array |
||
108 | */ |
||
109 | public function getFileInfo($file) |
||
131 | |||
132 | /** |
||
133 | * Copy a translation file |
||
134 | * @param string $source |
||
135 | * @param string $destination |
||
136 | * @return boolean |
||
137 | */ |
||
138 | public function copy($source, $destination) |
||
155 | |||
156 | /** |
||
157 | * Deletes a translation file |
||
158 | * @param string $file |
||
159 | * @param string $langcode |
||
160 | * @return boolean |
||
161 | */ |
||
162 | public function delete($file, $langcode) |
||
179 | |||
180 | /** |
||
181 | * Whether the translation file can be deleted |
||
182 | * @param string $file |
||
183 | * @param string $langcode |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function canDelete($file, $langcode) |
||
190 | |||
191 | /** |
||
192 | * Whether the file is a translation file |
||
193 | * @param string $file |
||
194 | * @param string $langcode |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function isTranslationFile($file, $langcode) |
||
204 | |||
205 | /** |
||
206 | * Returns a module ID from the translation file path |
||
207 | * @param string $file |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getModuleIdFromPath($file) |
||
221 | |||
222 | /** |
||
223 | * Returns an array of scanned and prepared translations |
||
224 | * @param string|null $langcode |
||
225 | * @return array |
||
226 | */ |
||
227 | public function getImportList($langcode = null) |
||
243 | |||
244 | /** |
||
245 | * Delete both cache and ZIP file |
||
246 | * @return boolean |
||
247 | */ |
||
248 | public function clearImport() |
||
254 | |||
255 | /** |
||
256 | * Copy translation from the source ZIP file |
||
257 | * @param string $module_id |
||
258 | * @param string $file |
||
259 | * @param string $langcode |
||
260 | * @return boolean |
||
261 | */ |
||
262 | public function importContent($module_id, $file, $langcode) |
||
291 | |||
292 | /** |
||
293 | * Ensure that directory exists and contains no the same file |
||
294 | * @param string $file |
||
295 | * @return boolean |
||
296 | */ |
||
297 | protected function prepareDirectory($file) |
||
310 | |||
311 | /** |
||
312 | * Returns an array of scanned translations |
||
313 | * @param string|bool $file |
||
314 | * @return array |
||
315 | */ |
||
316 | public function scanImportFile($file) |
||
338 | |||
339 | /** |
||
340 | * Build translation data |
||
341 | * @param array $items |
||
342 | * @param string $file |
||
343 | * @param null|string $langcode |
||
344 | * @return array |
||
345 | */ |
||
346 | public function buildImportList(array $items, $file, $langcode) |
||
371 | |||
372 | /** |
||
373 | * Prepare an array of translations |
||
374 | * @param array $data |
||
375 | * @param string $file |
||
376 | * @param string $langcode |
||
377 | * @return array |
||
378 | */ |
||
379 | protected function prepareImportTranslations(array $data, $file, $langcode) |
||
421 | |||
422 | /** |
||
423 | * Read CSV from ZIP file |
||
424 | * @param string $module_id |
||
425 | * @param string $file |
||
426 | * @param string $langcode |
||
427 | * @return string |
||
428 | */ |
||
429 | public function readZip($module_id, $file, $langcode) |
||
447 | |||
448 | /** |
||
449 | * Returns a total number of translated strings |
||
450 | * @param array $lines |
||
451 | * @return integer |
||
452 | */ |
||
453 | protected function countTranslated(array $lines) |
||
464 | |||
465 | /** |
||
466 | * Downloads a remote ZIP file |
||
467 | * @param string $destination |
||
468 | * @return boolean |
||
469 | */ |
||
470 | public function downloadImportFile($destination) |
||
485 | |||
486 | /** |
||
487 | * Returns URL of source ZIP file |
||
488 | * @return string |
||
489 | */ |
||
490 | public function getImportDownloadUrl() |
||
494 | |||
495 | /** |
||
496 | * Returns the path of a downloaded ZIP file |
||
497 | * @return bool|string |
||
498 | */ |
||
499 | public function getImportFile() |
||
509 | |||
510 | /** |
||
511 | * Returns the absolute path of downloaded ZIP file |
||
512 | * @return string|bool |
||
513 | */ |
||
514 | public function getImportFilePath() |
||
518 | |||
519 | /** |
||
520 | * Detect invalid HTML |
||
521 | * @param string $string |
||
522 | * @return boolean |
||
523 | */ |
||
524 | public function isInvalidHtml($string) |
||
544 | |||
545 | } |
||
546 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.