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 |
||
| 21 | class MultiLang |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Language/Locale. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $lang; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * System environment |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $environment; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Config. |
||
| 39 | * |
||
| 40 | * @var \Longman\LaravelMultiLang\Config |
||
| 41 | */ |
||
| 42 | protected $config; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Repository |
||
| 46 | * |
||
| 47 | * @var \Longman\LaravelMultiLang\Repository |
||
| 48 | */ |
||
| 49 | protected $repository; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Texts. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $texts; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Missing texts. |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $new_texts; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Application scope. |
||
| 67 | * |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | protected $scope = 'global'; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Create a new MultiLang instance. |
||
| 74 | * |
||
| 75 | * @param string $environment |
||
| 76 | * @param array $config |
||
| 77 | * @param \Illuminate\Cache\CacheManager $cache |
||
| 78 | * @param \Illuminate\Database\DatabaseManager $db |
||
| 79 | */ |
||
| 80 | 33 | public function __construct($environment, array $config, Cache $cache, Database $db) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Set multilang config |
||
| 91 | * |
||
| 92 | * @param array $config |
||
| 93 | * @return $this |
||
| 94 | */ |
||
| 95 | 33 | public function setConfig(array $config) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Get multilang config |
||
| 104 | * |
||
| 105 | * @return \Longman\LaravelMultiLang\Config |
||
| 106 | */ |
||
| 107 | 1 | public function getConfig(): Config |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Set repository object |
||
| 115 | * |
||
| 116 | * @param \Longman\LaravelMultiLang\Repository $repository |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | 33 | public function setRepository(Repository $repository) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Get repository object |
||
| 128 | * |
||
| 129 | * @return \Longman\LaravelMultiLang\Repository |
||
| 130 | */ |
||
| 131 | 3 | public function getRepository() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Set application scope |
||
| 138 | * |
||
| 139 | * @param $scope |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | 1 | public function setScope($scope) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Get application scope |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 1 | public function getScope() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Set locale and load texts |
||
| 161 | * |
||
| 162 | * @param string $lang |
||
| 163 | * @param array $texts |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | 27 | public function setLocale($lang, array $texts = null) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Load texts |
||
| 182 | * |
||
| 183 | * @param string $lang |
||
| 184 | * @param string $scope |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | 23 | public function loadTexts($lang, $scope = null) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Get translated text |
||
| 207 | * |
||
| 208 | * @param string $key |
||
| 209 | * @param array $replace |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | 9 | public function get($key, array $replace = []) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Replace markers in text |
||
| 236 | * |
||
| 237 | * @param string $text |
||
| 238 | * @param array $replace |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | 7 | protected function replaceMarkers($text, array $replace = []) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Make the place-holder replacements on a line. |
||
| 252 | * |
||
| 253 | * @param string $text |
||
| 254 | * @param array $replace |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 1 | protected function makeReplacements($text, array $replace) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Sort the replacements array. |
||
| 274 | * |
||
| 275 | * @param array $replace |
||
| 276 | * @return \Illuminate\Support\Collection |
||
| 277 | */ |
||
| 278 | protected function sortReplacements(array $replace) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get redirect url in middleware |
||
| 287 | * |
||
| 288 | * @param \Illuminate\Http\Request $request |
||
| 289 | * @return null|string |
||
| 290 | */ |
||
| 291 | 5 | public function getRedirectUrl(Request $request) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Detect locale based on url segment |
||
| 328 | * |
||
| 329 | * @param \Illuminate\Http\Request $request |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | 2 | public function detectLocale(Request $request) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Wrap routes to available languages group |
||
| 346 | * |
||
| 347 | * @param \Closure $callback |
||
| 348 | */ |
||
| 349 | public function routeGroup(Closure $callback) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Manage texts |
||
| 365 | */ |
||
| 366 | public function manageTextsRoutes() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get texts |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | 4 | public function getTexts() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Get all texts |
||
| 398 | * |
||
| 399 | * @param string $lang |
||
| 400 | * @param string $scope |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | 1 | public function getAllTexts($lang = null, $scope = null) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Set texts manually |
||
| 410 | * |
||
| 411 | * @param array $texts_array |
||
| 412 | * @return \Longman\LaravelMultiLang\MultiLang |
||
| 413 | */ |
||
| 414 | 4 | public function setTexts(array $texts_array) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Queue missing texts |
||
| 428 | * |
||
| 429 | * @param string $key |
||
| 430 | * @return void |
||
| 431 | */ |
||
| 432 | 4 | protected function queueToSave($key) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Get language prefixed url |
||
| 439 | * |
||
| 440 | * @param string $path |
||
| 441 | * @param string $lang |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | 4 | public function getUrl($path, $lang = null) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Remove locale from the path |
||
| 456 | * |
||
| 457 | * @param string $path |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | 4 | private function removeLocaleFromPath($path) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get language prefixed route |
||
| 473 | * |
||
| 474 | * @param string $name |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | 2 | public function getRoute($name) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Check if autosave allowed |
||
| 489 | * |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | 7 | public function autoSaveIsAllowed() |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Get locale |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | 25 | public function getLocale() |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Get available locales |
||
| 513 | * |
||
| 514 | * @return array |
||
| 515 | */ |
||
| 516 | 1 | public function getLocales() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Save missing texts |
||
| 523 | * |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | 3 | public function saveTexts() |
|
| 536 | } |
||
| 537 |