Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 7 | class Translation |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Setup public vars. |
||
| 11 | */ |
||
| 12 | public $translator; |
||
| 13 | public $translation; |
||
| 14 | public $string; |
||
| 15 | public $debug; |
||
| 16 | public $from; |
||
| 17 | public $to; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Setup default values. |
||
| 21 | * |
||
| 22 | * @param string $string |
||
| 23 | */ |
||
| 24 | public function __construct($string) |
||
| 25 | { |
||
| 26 | $this->translator = config('laralang.default.translator'); |
||
| 27 | $this->debug = config('laralang.default.debug'); |
||
| 28 | $this->from = config('laralang.default.from_lang'); |
||
| 29 | $this->to = config('laralang.default.to_lang'); |
||
| 30 | $this->string = $string; |
||
| 31 | $this->translation = $string; |
||
| 32 | |||
| 33 | |||
| 34 | // Checking whether from_lang or to_lang are set as app_locale. |
||
| 35 | |||
| 36 | if ($this->from == 'app_locale') {$this->from = App::getLocale();} |
||
| 37 | |||
| 38 | if ($this->to == 'app_locale') {$this->to = App::getLocale();} |
||
| 39 | } |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * Setup debug value |
||
| 44 | * |
||
| 45 | * @param boolean $debug |
||
| 46 | */ |
||
| 47 | |||
| 48 | public function setDebug($debug) |
||
| 49 | { |
||
| 50 | $this->debug = $debug; |
||
| 51 | |||
| 52 | return $this; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Setup fromLang value |
||
| 57 | * |
||
| 58 | * @param string $lang |
||
| 59 | */ |
||
| 60 | |||
| 61 | public function setFromLang($lang) |
||
| 62 | { |
||
| 63 | $this->from = $lang; |
||
| 64 | |||
| 65 | return $this; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Setup tolang value |
||
| 70 | * |
||
| 71 | * @param string $lang |
||
| 72 | */ |
||
| 73 | |||
| 74 | public function setToLang($lang) |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * Setup translator |
||
| 84 | * |
||
| 85 | * @param string $translator |
||
| 86 | */ |
||
| 87 | |||
| 88 | public function setTranslator($translator) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Main function of the class |
||
| 97 | * |
||
| 98 | * Check what translator must select |
||
| 99 | * |
||
| 100 | */ |
||
| 101 | |||
| 102 | private function run() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get translation from mymemory API. |
||
| 140 | */ |
||
| 141 | private function mymemoryTrans() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get translation from apertium API. |
||
| 193 | */ |
||
| 194 | |||
| 195 | private function apertiumTrans() |
||
| 253 | |||
| 254 | /* |
||
| 255 | * This fuction is called when host is down, and it would set translation if debug is true |
||
| 256 | * |
||
| 257 | */ |
||
| 258 | private function hostDown() { |
||
| 264 | |||
| 265 | /* |
||
| 266 | * This fuction is called by trans() function of Fadade Laralang |
||
| 267 | * It would call run() function of this class and returns the translation |
||
| 268 | * |
||
| 269 | */ |
||
| 270 | public function __toString() |
||
| 276 | } |
||
| 277 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.