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 | * System environment |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $environment; |
||
34 | |||
35 | /** |
||
36 | * The instance of the cache. |
||
37 | * |
||
38 | * @var \Illuminate\Cache\CacheManager |
||
39 | */ |
||
40 | protected $cache; |
||
41 | |||
42 | /** |
||
43 | * Config. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $config; |
||
48 | |||
49 | /** |
||
50 | * The instance of the database. |
||
51 | * |
||
52 | * @var \Illuminate\Database\DatabaseManager |
||
53 | */ |
||
54 | protected $db; |
||
55 | |||
56 | /** |
||
57 | * Name of the cache. |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $cache_name; |
||
62 | |||
63 | /** |
||
64 | * Texts collection. |
||
65 | * |
||
66 | * @var \Illuminate\Support\Collection |
||
67 | */ |
||
68 | protected $texts; |
||
69 | |||
70 | /** |
||
71 | * Missing texts. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $new_texts; |
||
76 | |||
77 | /** |
||
78 | * Create a new MultiLang instance. |
||
79 | * |
||
80 | * @param string $environment |
||
81 | * @param array $config |
||
82 | * @param \Illuminate\Cache\CacheManager $cache |
||
83 | * @param \Illuminate\Database\DatabaseManager $db |
||
84 | */ |
||
85 | 24 | public function __construct($environment, array $config, Cache $cache, Database $db) |
|
93 | |||
94 | 24 | public function setConfig(array $config) |
|
99 | |||
100 | 21 | public function getConfig($key = null, $default = null) |
|
122 | |||
123 | /** |
||
124 | * Get a cache driver instance. |
||
125 | * |
||
126 | * @return \Illuminate\Contracts\Cache\Repository |
||
127 | */ |
||
128 | 18 | public function getCache() |
|
139 | |||
140 | /** |
||
141 | * Get a database connection instance. |
||
142 | * |
||
143 | * @return \Illuminate\Database\Connection |
||
144 | */ |
||
145 | 18 | public function getDb() |
|
153 | |||
154 | /** |
||
155 | * Set locale and load texts |
||
156 | * |
||
157 | * @param string $lang |
||
158 | * @param array $texts |
||
159 | * @return void |
||
160 | */ |
||
161 | 22 | public function setLocale($lang, $texts = null) |
|
178 | |||
179 | /** |
||
180 | * Load texts |
||
181 | * |
||
182 | * @param string $lang |
||
183 | * @return array |
||
184 | */ |
||
185 | 18 | public function loadTexts($lang = null) |
|
201 | |||
202 | /** |
||
203 | * Get translated text |
||
204 | * |
||
205 | * @param string $key |
||
206 | * @return string |
||
207 | */ |
||
208 | 8 | public function get($key) |
|
228 | |||
229 | /** |
||
230 | * Get texts |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | 4 | public function getRedirectUrl(Request $request) |
|
263 | |||
264 | 1 | public function detectLocale(Request $request) |
|
278 | |||
279 | /** |
||
280 | * Get texts |
||
281 | * |
||
282 | * @return array |
||
283 | */ |
||
284 | 4 | public function getTexts() |
|
289 | |||
290 | /** |
||
291 | * Set texts manually |
||
292 | * |
||
293 | * @param array $texts_array |
||
294 | * @return \Longman\LaravelMultiLang\MultiLang |
||
295 | */ |
||
296 | 3 | public function setTexts(array $texts_array) |
|
307 | |||
308 | /** |
||
309 | * Queue missing texts |
||
310 | * |
||
311 | * @param string $key |
||
312 | * @return void |
||
313 | */ |
||
314 | 4 | protected function queueToSave($key) |
|
318 | |||
319 | /** |
||
320 | * Check if we must load texts from cache |
||
321 | * |
||
322 | * @return bool |
||
323 | */ |
||
324 | 4 | public function mustLoadFromCache() |
|
328 | |||
329 | 4 | protected function storeTextsInCache(array $texts) |
|
334 | |||
335 | 18 | public function loadTextsFromDatabase($lang) |
|
347 | |||
348 | 1 | public function loadTextsFromCache() |
|
354 | |||
355 | 21 | public function setCacheName($lang) |
|
359 | |||
360 | 4 | public function getCacheName() |
|
364 | |||
365 | 2 | public function getUrl($path) |
|
373 | |||
374 | 4 | public function autoSaveIsAllowed() |
|
381 | |||
382 | 19 | public function getLocale() |
|
386 | |||
387 | 3 | public function saveTexts() |
|
416 | |||
417 | 18 | protected function getTableName() |
|
422 | } |
||
423 |