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 |
||
19 | class MultiLang |
||
20 | { |
||
21 | /** |
||
22 | * Language/Locale. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $lang; |
||
27 | |||
28 | /** |
||
29 | * Default Language/Locale. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $default_lang; |
||
34 | |||
35 | /** |
||
36 | * System environment |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $environment; |
||
41 | |||
42 | /** |
||
43 | * The instance of the cache. |
||
44 | * |
||
45 | * @var \Illuminate\Cache\CacheManager |
||
46 | */ |
||
47 | protected $cache; |
||
48 | |||
49 | /** |
||
50 | * Config. |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $config; |
||
55 | |||
56 | /** |
||
57 | * The instance of the database. |
||
58 | * |
||
59 | * @var \Illuminate\Database\DatabaseManager |
||
60 | */ |
||
61 | protected $db; |
||
62 | |||
63 | /** |
||
64 | * Name of the cache. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $cache_name; |
||
69 | |||
70 | /** |
||
71 | * Texts collection. |
||
72 | * |
||
73 | * @var \Illuminate\Support\Collection |
||
74 | */ |
||
75 | protected $texts; |
||
76 | |||
77 | /** |
||
78 | * Missing texts. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | protected $new_texts; |
||
83 | |||
84 | /** |
||
85 | * Create a new MultiLang instance. |
||
86 | * |
||
87 | * @param string $environment |
||
88 | * @param array $config |
||
89 | * @param \Illuminate\Cache\CacheManager $cache |
||
90 | * @param \Illuminate\Database\DatabaseManager $db |
||
91 | * @return void |
||
|
|||
92 | */ |
||
93 | 23 | public function __construct($environment, array $config, CacheContract $cache, DatabaseContract $db) |
|
101 | |||
102 | 23 | public function setConfig(array $config) |
|
110 | |||
111 | 23 | public function getDefaultConfig() |
|
129 | |||
130 | 21 | public function getConfig($key = null) |
|
138 | |||
139 | /** |
||
140 | * Set locale and load texts |
||
141 | * |
||
142 | * @param string $lang |
||
143 | * @param string $default_lang |
||
144 | * @param array $text |
||
145 | * @return void |
||
146 | */ |
||
147 | 22 | public function setLocale($lang, $default_lang = null, $texts = null) |
|
172 | |||
173 | /** |
||
174 | * Load texts |
||
175 | * |
||
176 | * @param string $lang |
||
177 | * @return array |
||
178 | */ |
||
179 | 20 | public function loadTexts($lang = null) |
|
197 | |||
198 | /** |
||
199 | * Get translated text |
||
200 | * |
||
201 | * @param string $key |
||
202 | * @param string $default |
||
203 | * @return string |
||
204 | */ |
||
205 | 7 | public function get($key) |
|
225 | |||
226 | /** |
||
227 | * Get texts |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | 4 | public function getRedirectUrl(Request $request) |
|
260 | |||
261 | |||
262 | |||
263 | /** |
||
264 | * Get texts |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | 4 | public function getTexts() |
|
273 | |||
274 | /** |
||
275 | * Set texts manually |
||
276 | * |
||
277 | * @param array $texts_array |
||
278 | * @return \Longman\LaravelMultiLang\MultiLang |
||
279 | */ |
||
280 | 3 | public function setTexts(array $texts_array) |
|
291 | |||
292 | /** |
||
293 | * Queue missing texts |
||
294 | * |
||
295 | * @param string $key |
||
296 | * @return void |
||
297 | */ |
||
298 | 4 | protected function queueToSave($key) |
|
302 | |||
303 | /** |
||
304 | * Check if we must load texts from cache |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | 4 | public function mustLoadFromCache() |
|
312 | |||
313 | 4 | protected function storeTextsInCache(array $texts) |
|
319 | |||
320 | 20 | public function loadTextsFromDatabase($lang) |
|
332 | |||
333 | 1 | public function loadTextsFromCache() |
|
339 | |||
340 | 21 | public function setCacheName($lang) |
|
344 | |||
345 | 4 | public function getCacheName() |
|
349 | |||
350 | 1 | public function getUrl($path) |
|
358 | |||
359 | 2 | public function autoSaveIsAllowed() |
|
366 | |||
367 | 20 | public function getLocale() |
|
371 | |||
372 | 3 | public function saveTexts() |
|
402 | |||
403 | 20 | protected function getTableName() |
|
408 | } |
||
409 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.