Complex classes like MultiLang 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 MultiLang, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class MultiLang |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Language/Locale. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $lang; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * System environment |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $environment; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Config. |
||
| 40 | * |
||
| 41 | * @var \Longman\LaravelMultiLang\Config |
||
| 42 | */ |
||
| 43 | protected $config; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Repository |
||
| 47 | * |
||
| 48 | * @var \Longman\LaravelMultiLang\Repository |
||
| 49 | */ |
||
| 50 | protected $repository; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Texts. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $texts; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Missing texts. |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $new_texts; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Application scope. |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $scope; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Create a new MultiLang instance. |
||
| 75 | * |
||
| 76 | * @param string $environment |
||
| 77 | * @param array $config |
||
| 78 | * @param \Illuminate\Cache\CacheManager $cache |
||
| 79 | * @param \Illuminate\Database\DatabaseManager $db |
||
| 80 | */ |
||
| 81 | 23 | public function __construct($environment, array $config, Cache $cache, Database $db) |
|
| 82 | { |
||
| 83 | 23 | $this->environment = $environment; |
|
| 84 | 23 | $this->cache = $cache; |
|
|
|
|||
| 85 | 23 | $this->db = $db; |
|
| 86 | |||
| 87 | 23 | $this->setConfig($config); |
|
| 88 | |||
| 89 | 23 | $this->setRepository(new Repository($this->config, $cache, $db)); |
|
| 90 | 23 | } |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Set multilang config |
||
| 94 | * |
||
| 95 | * @param array $config |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | 23 | public function setConfig(array $config) |
|
| 99 | { |
||
| 100 | 23 | $this->config = new Config($config); |
|
| 101 | 23 | return $this; |
|
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Set repository object |
||
| 106 | * |
||
| 107 | * @param \Longman\LaravelMultiLang\Repository $repository |
||
| 108 | * @return $this |
||
| 109 | */ |
||
| 110 | 23 | public function setRepository(Repository $repository) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Get repository object |
||
| 118 | * |
||
| 119 | * @return \Longman\LaravelMultiLang\Repository |
||
| 120 | */ |
||
| 121 | 2 | public function getRepository() |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Set application scope |
||
| 128 | * |
||
| 129 | * @param $scope |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | public function setScope($scope) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get application scope |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getScope() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set locale and load texts |
||
| 150 | * |
||
| 151 | * @param string $lang |
||
| 152 | * @param array $texts |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | 21 | public function setLocale($lang, array $texts = null) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Load texts |
||
| 171 | * |
||
| 172 | * @param string $lang |
||
| 173 | * @param string $scope |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | 17 | public function loadTexts($lang, $scope = null) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Get translated text |
||
| 195 | * |
||
| 196 | * @param string $key |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 8 | public function get($key) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Get redirect url in middleware |
||
| 222 | * |
||
| 223 | * @param \Illuminate\Http\Request $request |
||
| 224 | * @return null|string |
||
| 225 | */ |
||
| 226 | 4 | public function getRedirectUrl(Request $request) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Detect locale based on url segment |
||
| 262 | * |
||
| 263 | * @param \Illuminate\Http\Request $request |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 1 | public function detectLocale(Request $request) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Wrap routes to available languages group |
||
| 280 | * |
||
| 281 | * @param \Closure $callback |
||
| 282 | */ |
||
| 283 | public function routeGroup(Closure $callback) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Manage texts |
||
| 300 | */ |
||
| 301 | public function manageTextsRoutes() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Get texts |
||
| 322 | * |
||
| 323 | * @return array |
||
| 324 | */ |
||
| 325 | 4 | public function getTexts() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Set texts manually |
||
| 333 | * |
||
| 334 | * @param array $texts_array |
||
| 335 | * @return \Longman\LaravelMultiLang\MultiLang |
||
| 336 | */ |
||
| 337 | 3 | public function setTexts(array $texts_array) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Queue missing texts |
||
| 351 | * |
||
| 352 | * @param string $key |
||
| 353 | * @return void |
||
| 354 | */ |
||
| 355 | 4 | protected function queueToSave($key) |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Get language prefixed url |
||
| 362 | * |
||
| 363 | * @param $path |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | 2 | public function getUrl($path) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Check if autosave allowed |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | 4 | public function autoSaveIsAllowed() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Get locale |
||
| 390 | * |
||
| 391 | * @return string |
||
| 392 | */ |
||
| 393 | 18 | public function getLocale() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Get available locales |
||
| 400 | * |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | 1 | public function getLocales() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Save missing texts |
||
| 410 | * |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | 3 | public function saveTexts() |
|
| 422 | } |
||
| 423 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: