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 |
||
| 21 | class Translator extends Model |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * File model class instance |
||
| 26 | * @var \gplcart\core\models\File $file |
||
| 27 | */ |
||
| 28 | protected $file; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Zip helper class instance |
||
| 32 | * @var \gplcart\core\helpers\Zip $zip |
||
| 33 | */ |
||
| 34 | protected $zip; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Cache class instance |
||
| 38 | * @var \gplcart\core\Cache $cache |
||
| 39 | */ |
||
| 40 | protected $cache; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Language model class instance |
||
| 44 | * @var \gplcart\core\models\Language $language |
||
| 45 | */ |
||
| 46 | protected $language; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param ZipHelper $zip |
||
| 50 | * @param FileModel $file |
||
| 51 | * @param LanguageModel $language |
||
| 52 | * @param Cache $cache |
||
| 53 | */ |
||
| 54 | public function __construct(ZipHelper $zip, FileModel $file, |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Returns an array of information about the translation file |
||
| 67 | * @param string $file |
||
| 68 | * @return array |
||
| 69 | */ |
||
| 70 | public function getFileInfo($file) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Copy a translation file |
||
| 95 | * @param string $source |
||
| 96 | * @param string $destination |
||
| 97 | * @return boolean |
||
| 98 | */ |
||
| 99 | public function copy($source, $destination) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Deletes a translation file |
||
| 119 | * @param string $file |
||
| 120 | * @param string $langcode |
||
| 121 | * @return boolean |
||
| 122 | */ |
||
| 123 | public function delete($file, $langcode) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Whether the translation file can be deleted |
||
| 143 | * @param string $file |
||
| 144 | * @param string $langcode |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function canDelete($file, $langcode) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Whether the file is a translation file |
||
| 154 | * @param string $file |
||
| 155 | * @param string $langcode |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function isTranslationFile($file, $langcode) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Returns a module ID from the translation file path |
||
| 168 | * @param string $file |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | public function getModuleIdFromPath($file) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns an array of scanned and prepared translations |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | public function getImportList($langcode = null) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Delete both cache and ZIP file |
||
| 206 | * @return boolean |
||
| 207 | */ |
||
| 208 | public function clearImport() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Copy translation from the source ZIP file |
||
| 217 | * @param string $module_id |
||
| 218 | * @param string $file |
||
| 219 | * @param string $langcode |
||
| 220 | * @return boolean |
||
| 221 | */ |
||
| 222 | public function importContent($module_id, $file, $langcode) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Ensure that directory exists and contains no the same file |
||
| 254 | * @param string $file |
||
| 255 | * @return boolean |
||
| 256 | */ |
||
| 257 | protected function prepareDirectory($file) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns an array of scanned translations |
||
| 273 | * @param string|bool $file |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function scanImportFile($file) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Build translation data |
||
| 301 | * @param array $items |
||
| 302 | * @param string $file |
||
| 303 | * @param null|string $langcode |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function buildImportList(array $items, $file, $langcode) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Prepare an array of translations |
||
| 334 | * @param array $data |
||
| 335 | * @param string $file |
||
| 336 | * @return array |
||
| 337 | */ |
||
| 338 | protected function prepareImportTranslations(array $data, $file, $langcode) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Read CSV from ZIP file |
||
| 383 | * @param string $module_id |
||
| 384 | * @param string $file |
||
| 385 | * @param string $langcode |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | public function readZip($module_id, $file, $langcode) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Returns a total number of translated strings |
||
| 409 | * @param array $lines |
||
| 410 | * @return integer |
||
| 411 | */ |
||
| 412 | protected function countTranslated(array $lines) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Downloads a remote ZIP file |
||
| 426 | * @param string $destination |
||
| 427 | * @return boolean |
||
| 428 | */ |
||
| 429 | public function downloadImportFile($destination) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Returns URL of source ZIP file |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getImportDownloadUrl() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Returns the path of a downloaded ZIP file |
||
| 456 | * @return bool|string |
||
| 457 | */ |
||
| 458 | public function getImportFile() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Returns the absolute path of downloaded ZIP file |
||
| 471 | * @return string|bool |
||
| 472 | */ |
||
| 473 | public function getImportFilePath() |
||
| 477 | |||
| 478 | } |
||
| 479 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: