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 |
||
| 15 | class MultiLang |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Language/Locale. |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $lang; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * System environment |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $environment; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Config. |
||
| 33 | * |
||
| 34 | * @var \Longman\LaravelMultiLang\Config |
||
| 35 | */ |
||
| 36 | protected $config; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Repository |
||
| 40 | * |
||
| 41 | * @var \Longman\LaravelMultiLang\Repository |
||
| 42 | */ |
||
| 43 | protected $repository; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Texts. |
||
| 47 | * |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $texts; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Missing texts. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $new_texts; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Application scope. |
||
| 61 | * |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $scope = 'global'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Translator instance |
||
| 68 | * |
||
| 69 | * @var \Symfony\Component\Translation\Translator |
||
| 70 | */ |
||
| 71 | protected $translator; |
||
| 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 | 33 | */ |
|
| 81 | public function __construct(string $environment, array $config, Cache $cache, Database $db) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Set multilang config |
||
| 92 | * |
||
| 93 | * @param array $config |
||
| 94 | * @return $this |
||
| 95 | 33 | */ |
|
| 96 | public function setConfig(array $config): MultiLang |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get multilang config |
||
| 105 | * |
||
| 106 | * @return \Longman\LaravelMultiLang\Config |
||
| 107 | 1 | */ |
|
| 108 | public function getConfig(): Config |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set repository object |
||
| 116 | * |
||
| 117 | * @param \Longman\LaravelMultiLang\Repository $repository |
||
| 118 | * @return $this |
||
| 119 | 33 | */ |
|
| 120 | public function setRepository(Repository $repository): MultiLang |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get repository object |
||
| 129 | * |
||
| 130 | * @return \Longman\LaravelMultiLang\Repository |
||
| 131 | 3 | */ |
|
| 132 | public function getRepository(): Repository |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Set application scope |
||
| 139 | * |
||
| 140 | * @param $scope |
||
| 141 | * @return $this |
||
| 142 | 1 | */ |
|
| 143 | public function setScope($scope): MultiLang |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get application scope |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | 1 | */ |
|
| 155 | public function getScope(): string |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set locale |
||
| 162 | * |
||
| 163 | * @param string $lang |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | 27 | public function setLocale(string $lang) |
|
| 173 | 26 | ||
| 174 | 23 | public function loadTexts(?string $locale = null, ?string $scope = null): array |
|
| 201 | |||
| 202 | 4 | protected function createTranslator(string $locale, string $scope, array $texts): Translator |
|
| 210 | |||
| 211 | /** |
||
| 212 | 9 | * Get translated text |
|
| 213 | * |
||
| 214 | * @param string $key |
||
| 215 | 9 | * @param array $replacements |
|
| 216 | 1 | * @return string |
|
| 217 | */ |
||
| 218 | public function get(string $key, array $replacements = []): string |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get redirect url in middleware |
||
| 254 | * |
||
| 255 | * @param \Illuminate\Http\Request $request |
||
| 256 | * @return string |
||
| 257 | 1 | */ |
|
| 258 | public function getRedirectUrl(Request $request): string |
||
| 294 | 5 | ||
| 295 | 5 | /** |
|
| 296 | 5 | * Detect locale based on url segment |
|
| 297 | * |
||
| 298 | * @param \Illuminate\Http\Request $request |
||
| 299 | * @return string |
||
| 300 | 5 | */ |
|
| 301 | 4 | public function detectLocale(Request $request): string |
|
| 312 | |||
| 313 | /** |
||
| 314 | 1 | * Wrap routes to available languages group |
|
| 315 | 1 | * |
|
| 316 | 1 | * @param \Closure $callback |
|
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | public function routeGroup(Closure $callback) |
||
| 332 | 2 | ||
| 333 | /** |
||
| 334 | 2 | * Get texts |
|
| 335 | 2 | * |
|
| 336 | * @return array |
||
| 337 | 2 | */ |
|
| 338 | 1 | public function getTexts(): array |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Get all texts |
||
| 346 | * |
||
| 347 | * @param string $lang |
||
| 348 | * @param string $scope |
||
| 349 | * @return array |
||
| 350 | */ |
||
| 351 | public function getAllTexts(?string $lang = null, ?string $scope = null): array |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Set texts manually |
||
| 358 | * |
||
| 359 | * @param array $texts_array |
||
| 360 | * @return \Longman\LaravelMultiLang\MultiLang |
||
| 361 | */ |
||
| 362 | public function setTexts(array $texts_array): MultiLang |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Queue missing texts |
||
| 378 | * |
||
| 379 | * @param string $key |
||
| 380 | * @return void |
||
| 381 | */ |
||
| 382 | protected function queueToSave(string $key) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get language prefixed url |
||
| 389 | * |
||
| 390 | 4 | * @param string $path |
|
| 391 | * @param string $lang |
||
| 392 | * @return string |
||
| 393 | 4 | */ |
|
| 394 | public function getUrl(string $path, ?string $lang = null): string |
||
| 403 | 1 | ||
| 404 | /** |
||
| 405 | 1 | * Remove locale from the path |
|
| 406 | * |
||
| 407 | * @param string $path |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | private function removeLocaleFromPath(string $path): string |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Get language prefixed route |
||
| 431 | * |
||
| 432 | 4 | * @param string $name |
|
| 433 | * @return string |
||
| 434 | 4 | */ |
|
| 435 | 4 | public function getRoute(string $name): string |
|
| 444 | 4 | ||
| 445 | /** |
||
| 446 | 4 | * Check if autosave allowed |
|
| 447 | 4 | * |
|
| 448 | 4 | * @return bool |
|
| 449 | */ |
||
| 450 | public function autoSaveIsAllowed() |
||
| 458 | |||
| 459 | /** |
||
| 460 | 4 | * Get locale |
|
| 461 | * |
||
| 462 | 4 | * @return string |
|
| 463 | 4 | */ |
|
| 464 | 4 | public function getLocale() |
|
| 468 | 4 | ||
| 469 | /** |
||
| 470 | * Get available locales |
||
| 471 | * |
||
| 472 | * @return array |
||
| 473 | */ |
||
| 474 | public function getLocales(): array |
||
| 478 | |||
| 479 | 2 | /** |
|
| 480 | 2 | * Save missing texts |
|
| 481 | 1 | * |
|
| 482 | * @return bool |
||
| 483 | */ |
||
| 484 | 2 | public function saveTexts(): bool |
|
| 492 | } |
||
| 493 |