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 | 25 | 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 | 25 | public function setConfig(array $config) |
|
103 | |||
104 | /** |
||
105 | * Set repository object |
||
106 | * |
||
107 | * @param \Longman\LaravelMultiLang\Repository $repository |
||
108 | * @return $this |
||
109 | */ |
||
110 | 25 | public function setRepository(Repository $repository) |
|
116 | |||
117 | /** |
||
118 | * Get repository object |
||
119 | * |
||
120 | * @return \Longman\LaravelMultiLang\Repository |
||
121 | */ |
||
122 | 2 | public function getRepository() |
|
126 | |||
127 | /** |
||
128 | * Set application scope |
||
129 | * |
||
130 | * @param $scope |
||
131 | * @return $this |
||
132 | */ |
||
133 | public function setScope($scope) |
||
139 | |||
140 | /** |
||
141 | * Get application scope |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getScope() |
||
149 | |||
150 | /** |
||
151 | * Set locale and load texts |
||
152 | * |
||
153 | * @param string $lang |
||
154 | * @param array $texts |
||
155 | * @return void |
||
156 | */ |
||
157 | 23 | 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 | 19 | public function loadTexts($lang, $scope = null) |
|
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 | 4 | 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 | 1 | public function detectLocale(Request $request) |
|
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 | public function getAllTexts($lang = null, $scope = null) |
||
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 $path |
||
432 | * @return string |
||
433 | */ |
||
434 | 2 | public function getUrl($path) |
|
443 | |||
444 | /** |
||
445 | * Get language prefixed route |
||
446 | * |
||
447 | * @param string $name |
||
448 | * @return string |
||
449 | */ |
||
450 | 1 | public function getRoute($name) |
|
459 | |||
460 | /** |
||
461 | * Check if autosave allowed |
||
462 | * |
||
463 | * @return bool |
||
464 | */ |
||
465 | 5 | public function autoSaveIsAllowed() |
|
473 | |||
474 | /** |
||
475 | * Get locale |
||
476 | * |
||
477 | * @return string |
||
478 | */ |
||
479 | 20 | public function getLocale() |
|
483 | |||
484 | /** |
||
485 | * Get available locales |
||
486 | * |
||
487 | * @return array |
||
488 | */ |
||
489 | 1 | public function getLocales() |
|
493 | |||
494 | /** |
||
495 | * Save missing texts |
||
496 | * |
||
497 | * @return bool |
||
498 | */ |
||
499 | 3 | public function saveTexts() |
|
509 | } |
||
510 |
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: