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 |
||
18 | class MultiLang |
||
19 | { |
||
20 | /** |
||
21 | * Language/Locale. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $lang; |
||
26 | |||
27 | /** |
||
28 | * System environment |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $environment; |
||
33 | |||
34 | /** |
||
35 | * The instance of the cache. |
||
36 | * |
||
37 | * @var \Illuminate\Cache\CacheManager |
||
38 | */ |
||
39 | protected $cache; |
||
40 | |||
41 | /** |
||
42 | * Config. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $config; |
||
47 | |||
48 | /** |
||
49 | * The instance of the database. |
||
50 | * |
||
51 | * @var \Illuminate\Database\DatabaseManager |
||
52 | */ |
||
53 | protected $db; |
||
54 | |||
55 | /** |
||
56 | * Name of the cache. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $cache_name; |
||
61 | |||
62 | /** |
||
63 | * Texts. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $texts; |
||
68 | |||
69 | /** |
||
70 | * Missing texts. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $new_texts; |
||
75 | |||
76 | /** |
||
77 | * Create a new MultiLang instance. |
||
78 | * |
||
79 | * @param string $environment |
||
80 | * @param array $config |
||
81 | * @param \Illuminate\Cache\CacheManager $cache |
||
82 | * @param \Illuminate\Database\DatabaseManager $db |
||
83 | */ |
||
84 | public function __construct($environment, array $config, Cache $cache, Database $db) |
||
92 | |||
93 | public function setConfig(array $config) |
||
98 | |||
99 | public function getConfig($key = null, $default = null) |
||
121 | |||
122 | /** |
||
123 | * Get a cache driver instance. |
||
124 | * |
||
125 | * @return \Illuminate\Contracts\Cache\Repository |
||
126 | */ |
||
127 | public function getCache() |
||
138 | |||
139 | /** |
||
140 | * Get a database connection instance. |
||
141 | * |
||
142 | * @return \Illuminate\Database\Connection |
||
143 | */ |
||
144 | public function getDb() |
||
152 | |||
153 | /** |
||
154 | * Set locale and load texts |
||
155 | * |
||
156 | * @param string $lang |
||
157 | * @param array $texts |
||
158 | * @return void |
||
159 | */ |
||
160 | public function setLocale($lang, array $texts = null) |
||
175 | |||
176 | /** |
||
177 | * Load texts |
||
178 | * |
||
179 | * @param string $lang |
||
180 | * @return array |
||
181 | */ |
||
182 | public function loadTexts($lang = null) |
||
198 | |||
199 | /** |
||
200 | * Get translated text |
||
201 | * |
||
202 | * @param string $key |
||
203 | * @return string |
||
204 | */ |
||
205 | public function get($key) |
||
225 | |||
226 | /** |
||
227 | * Get texts |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | public function getRedirectUrl(Request $request) |
||
260 | |||
261 | public function detectLocale(Request $request) |
||
272 | |||
273 | /** |
||
274 | * Get texts |
||
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | public function getTexts() |
||
283 | |||
284 | /** |
||
285 | * Set texts manually |
||
286 | * |
||
287 | * @param array $texts_array |
||
288 | * @return \Longman\LaravelMultiLang\MultiLang |
||
289 | */ |
||
290 | public function setTexts(array $texts_array) |
||
301 | |||
302 | /** |
||
303 | * Queue missing texts |
||
304 | * |
||
305 | * @param string $key |
||
306 | * @return void |
||
307 | */ |
||
308 | protected function queueToSave($key) |
||
312 | |||
313 | /** |
||
314 | * Check if we must load texts from cache |
||
315 | * |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function mustLoadFromCache() |
||
322 | |||
323 | protected function storeTextsInCache(array $texts) |
||
328 | |||
329 | public function loadTextsFromDatabase($lang) |
||
341 | |||
342 | public function loadTextsFromCache() |
||
348 | |||
349 | public function setCacheName($lang) |
||
353 | |||
354 | public function getCacheName() |
||
358 | |||
359 | public function getUrl($path) |
||
367 | |||
368 | public function autoSaveIsAllowed() |
||
375 | |||
376 | public function getLocale() |
||
380 | |||
381 | public function saveTexts() |
||
410 | |||
411 | protected function getTableName() |
||
416 | } |
||
417 |