Complex classes like Translation 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 Translation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Translation |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Setup public vars. |
||
| 13 | */ |
||
| 14 | public $translation; |
||
| 15 | public $translator; |
||
| 16 | public $string; |
||
| 17 | public $debug; |
||
| 18 | public $from; |
||
| 19 | public $to; |
||
| 20 | public $save; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Setup default values. |
||
| 24 | * |
||
| 25 | * @param string $string |
||
| 26 | */ |
||
| 27 | public function __construct($string) |
||
| 28 | { |
||
| 29 | $this->translator = config('laralang.default.translator'); |
||
| 30 | $this->debug = config('laralang.default.debug'); |
||
| 31 | $this->save = config('laralang.default.autosave'); |
||
| 32 | $this->from = config('laralang.default.from_lang'); |
||
| 33 | $this->to = config('laralang.default.to_lang'); |
||
| 34 | $this->string = $string; |
||
| 35 | $this->translation = $string; |
||
| 36 | |||
| 37 | |||
| 38 | // Checking whether from_lang or to_lang are set as app_locale. |
||
| 39 | |||
| 40 | if ($this->from == 'app_locale') { |
||
| 41 | $this->from = App::getLocale(); |
||
| 42 | } |
||
| 43 | |||
| 44 | if ($this->to == 'app_locale') { |
||
| 45 | $this->to = App::getLocale(); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Setup debug value. |
||
| 51 | * |
||
| 52 | * @param bool $debug |
||
| 53 | */ |
||
| 54 | public function setDebug($debug) |
||
| 55 | { |
||
| 56 | $this->debug = $debug; |
||
| 57 | |||
| 58 | return $this; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Setup fromLang value. |
||
| 63 | * |
||
| 64 | * @param string $lang |
||
| 65 | */ |
||
| 66 | public function setFrom($lang) |
||
| 67 | { |
||
| 68 | $this->from = $lang; |
||
| 69 | |||
| 70 | return $this; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Setup tolang value. |
||
| 75 | * |
||
| 76 | * @param string $lang |
||
| 77 | */ |
||
| 78 | public function setTo($lang) |
||
| 79 | { |
||
| 80 | $this->to = $lang; |
||
| 81 | |||
| 82 | return $this; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Setup translator. |
||
| 87 | * |
||
| 88 | * @param string $translator |
||
| 89 | */ |
||
| 90 | public function setTranslator($translator) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Setup save option. |
||
| 99 | * |
||
| 100 | * @param bool $save |
||
| 101 | */ |
||
| 102 | public function Save($save) |
||
| 108 | |||
| 109 | |||
| 110 | private function exists() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Function to save translations to DB. |
||
| 131 | */ |
||
| 132 | private function checkSave() |
||
| 154 | |||
| 155 | /* |
||
| 156 | * This fuction is called when host is down, and it would set translation if debug is true |
||
| 157 | * |
||
| 158 | */ |
||
| 159 | private function hostDown() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Main function of the class. |
||
| 168 | * |
||
| 169 | * Check what translator must select. |
||
| 170 | */ |
||
| 171 | private function run() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get translation from mymemory API. |
||
| 210 | */ |
||
| 211 | private function mymemoryTrans() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get translation from apertium API. |
||
| 257 | */ |
||
| 258 | private function apertiumTrans() |
||
| 318 | |||
| 319 | /* |
||
| 320 | * This fuction is called by trans() function of Fadade Laralang |
||
| 321 | * It would call run() function of this class and returns the translation |
||
| 322 | * |
||
| 323 | */ |
||
| 324 | public function __toString() |
||
| 330 | } |
||
| 331 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.