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) |
|
90 | |||
91 | /** |
||
92 | * Set multilang config |
||
93 | * |
||
94 | * @param array $config |
||
95 | * @return $this |
||
96 | */ |
||
97 | 33 | public function setConfig(array $config) |
|
103 | |||
104 | /** |
||
105 | * Set repository object |
||
106 | * |
||
107 | * @param \Longman\LaravelMultiLang\Repository $repository |
||
108 | * @return $this |
||
109 | */ |
||
110 | 33 | public function setRepository(Repository $repository) |
|
116 | |||
117 | /** |
||
118 | * Get repository object |
||
119 | * |
||
120 | * @return \Longman\LaravelMultiLang\Repository |
||
121 | */ |
||
122 | 3 | public function getRepository() |
|
126 | |||
127 | /** |
||
128 | * Set application scope |
||
129 | * |
||
130 | * @param $scope |
||
131 | * @return $this |
||
132 | */ |
||
133 | 1 | public function setScope($scope) |
|
134 | { |
||
135 | 1 | $this->scope = $scope; |
|
136 | |||
137 | 1 | return $this; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Get application scope |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | 1 | public function getScope() |
|
146 | { |
||
147 | 1 | return $this->scope; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Set locale and load texts |
||
152 | * |
||
153 | * @param string $lang |
||
154 | * @param array $texts |
||
155 | * @return void |
||
156 | */ |
||
157 | 27 | public function setLocale($lang, array $texts = null) |
|
170 | |||
171 | /** |
||
172 | * Load texts |
||
173 | * |
||
174 | * @param string $lang |
||
175 | * @param string $scope |
||
176 | * @return array |
||
177 | */ |
||
178 | 23 | public function loadTexts($lang, $scope = null) |
|
179 | { |
||
180 | 23 | if ($this->environment != 'production' || $this->config->get('cache.enabled', true) === false) { |
|
181 | 20 | $texts = $this->repository->loadFromDatabase($lang, $scope); |
|
182 | |||
183 | 20 | return $texts; |
|
184 | } |
||
185 | |||
186 | 4 | if ($this->repository->existsInCache($lang, $scope)) { |
|
187 | 1 | $texts = $this->repository->loadFromCache($lang, $scope); |
|
188 | } else { |
||
189 | 4 | $texts = $this->repository->loadFromDatabase($lang, $scope); |
|
190 | 4 | $this->repository->storeInCache($lang, $texts, $scope); |
|
191 | } |
||
192 | |||
193 | 4 | return $texts; |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Get translated text |
||
198 | * |
||
199 | * @param string $key |
||
200 | * @param array $replace |
||
201 | * @return string |
||
202 | */ |
||
203 | 9 | public function get($key, array $replace = []) |
|
224 | |||
225 | /** |
||
226 | * Replace markers in text |
||
227 | * |
||
228 | * @param string $text |
||
229 | * @param array $replace |
||
230 | * @return string |
||
231 | */ |
||
232 | 7 | protected function replaceMarkers($text, array $replace = []) |
|
240 | |||
241 | /** |
||
242 | * Make the place-holder replacements on a line. |
||
243 | * |
||
244 | * @param string $text |
||
245 | * @param array $replace |
||
246 | * @return string |
||
247 | */ |
||
248 | 1 | protected function makeReplacements($text, array $replace) |
|
262 | |||
263 | /** |
||
264 | * Sort the replacements array. |
||
265 | * |
||
266 | * @param array $replace |
||
267 | * @return \Illuminate\Support\Collection |
||
268 | */ |
||
269 | protected function sortReplacements(array $replace) |
||
275 | |||
276 | /** |
||
277 | * Get redirect url in middleware |
||
278 | * |
||
279 | * @param \Illuminate\Http\Request $request |
||
280 | * @return null|string |
||
281 | */ |
||
282 | 5 | public function getRedirectUrl(Request $request) |
|
316 | |||
317 | /** |
||
318 | * Detect locale based on url segment |
||
319 | * |
||
320 | * @param \Illuminate\Http\Request $request |
||
321 | * @return string |
||
322 | */ |
||
323 | 2 | public function detectLocale(Request $request) |
|
324 | { |
||
325 | 2 | $locale = $request->segment(1); |
|
326 | 2 | $locales = $this->config->get('locales'); |
|
327 | |||
328 | 2 | if (isset($locales[$locale])) { |
|
329 | 1 | return isset($locales[$locale]['locale']) ? $locales[$locale]['locale'] : $locale; |
|
330 | } |
||
331 | |||
332 | 1 | return $this->config->get('default_locale', 'en'); |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Wrap routes to available languages group |
||
337 | * |
||
338 | * @param \Closure $callback |
||
339 | */ |
||
340 | public function routeGroup(Closure $callback) |
||
353 | |||
354 | /** |
||
355 | * Manage texts |
||
356 | */ |
||
357 | public function manageTextsRoutes() |
||
375 | |||
376 | /** |
||
377 | * Get texts |
||
378 | * |
||
379 | * @return array |
||
380 | */ |
||
381 | 4 | public function getTexts() |
|
386 | |||
387 | /** |
||
388 | * Get all texts |
||
389 | * |
||
390 | * @param string $lang |
||
391 | * @param string $scope |
||
392 | * @return array |
||
393 | */ |
||
394 | 1 | public function getAllTexts($lang = null, $scope = null) |
|
395 | { |
||
396 | 1 | return $this->repository->loadAllFromDatabase($lang, $scope); |
|
397 | } |
||
398 | |||
399 | /** |
||
400 | * Set texts manually |
||
401 | * |
||
402 | * @param array $texts_array |
||
403 | * @return \Longman\LaravelMultiLang\MultiLang |
||
404 | */ |
||
405 | 4 | public function setTexts(array $texts_array) |
|
416 | |||
417 | /** |
||
418 | * Queue missing texts |
||
419 | * |
||
420 | * @param string $key |
||
421 | * @return void |
||
422 | */ |
||
423 | 4 | protected function queueToSave($key) |
|
427 | |||
428 | /** |
||
429 | * Get language prefixed url |
||
430 | * |
||
431 | * @param string $path |
||
432 | * @param string $lang |
||
433 | * @return string |
||
434 | */ |
||
435 | 4 | public function getUrl($path, $lang = null) |
|
436 | { |
||
437 | 4 | $locale = $lang ? $lang : $this->getLocale(); |
|
438 | 4 | if ($locale) { |
|
439 | 4 | $path = $locale . '/' . $this->removeLocaleFromPath($path); |
|
440 | } |
||
441 | |||
442 | 4 | return $path; |
|
443 | } |
||
444 | |||
445 | /** |
||
446 | * Remove locale from the path |
||
447 | * |
||
448 | * @param string $path |
||
449 | * @return string |
||
450 | */ |
||
451 | 4 | private function removeLocaleFromPath($path) |
|
461 | |||
462 | /** |
||
463 | * Get language prefixed route |
||
464 | * |
||
465 | * @param string $name |
||
466 | * @return string |
||
467 | */ |
||
468 | 2 | public function getRoute($name) |
|
469 | { |
||
470 | 2 | $locale = $this->getLocale(); |
|
471 | 2 | if ($locale) { |
|
472 | 1 | $name = $locale . '.' . $name; |
|
473 | } |
||
474 | |||
475 | 2 | return $name; |
|
476 | } |
||
477 | |||
478 | /** |
||
479 | * Check if autosave allowed |
||
480 | * |
||
481 | * @return bool |
||
482 | */ |
||
483 | 7 | public function autoSaveIsAllowed() |
|
491 | |||
492 | /** |
||
493 | * Get locale |
||
494 | * |
||
495 | * @return string |
||
496 | */ |
||
497 | 25 | public function getLocale() |
|
501 | |||
502 | /** |
||
503 | * Get available locales |
||
504 | * |
||
505 | * @return array |
||
506 | */ |
||
507 | 1 | public function getLocales() |
|
511 | |||
512 | /** |
||
513 | * Save missing texts |
||
514 | * |
||
515 | * @return bool |
||
516 | */ |
||
517 | 3 | public function saveTexts() |
|
527 | } |
||
528 |
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: