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\Repository |
||
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 | */ |
||
92 | 23 | public function __construct($environment, array $config, CacheContract $cache, DatabaseContract $db) |
|
100 | |||
101 | 23 | public function setConfig(array $config) |
|
109 | |||
110 | 23 | public function getDefaultConfig() |
|
128 | |||
129 | 21 | public function getConfig($key = null) |
|
137 | |||
138 | /** |
||
139 | * Set locale and load texts |
||
140 | * |
||
141 | * @param string $lang |
||
142 | * @param string $default_lang |
||
143 | * @param array $texts |
||
144 | * @return void |
||
145 | */ |
||
146 | 22 | public function setLocale($lang, $default_lang = null, $texts = null) |
|
169 | |||
170 | /** |
||
171 | * Load texts |
||
172 | * |
||
173 | * @param string $lang |
||
174 | * @return array |
||
175 | */ |
||
176 | 20 | public function loadTexts($lang = null) |
|
194 | |||
195 | /** |
||
196 | * Get translated text |
||
197 | * |
||
198 | * @param string $key |
||
199 | * @return string |
||
200 | */ |
||
201 | 7 | public function get($key) |
|
221 | |||
222 | /** |
||
223 | * Get texts |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | 4 | public function getRedirectUrl(Request $request) |
|
256 | |||
257 | |||
258 | |||
259 | /** |
||
260 | * Get texts |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | 4 | public function getTexts() |
|
269 | |||
270 | /** |
||
271 | * Set texts manually |
||
272 | * |
||
273 | * @param array $texts_array |
||
274 | * @return \Longman\LaravelMultiLang\MultiLang |
||
275 | */ |
||
276 | 3 | public function setTexts(array $texts_array) |
|
287 | |||
288 | /** |
||
289 | * Queue missing texts |
||
290 | * |
||
291 | * @param string $key |
||
292 | * @return void |
||
293 | */ |
||
294 | 4 | protected function queueToSave($key) |
|
298 | |||
299 | /** |
||
300 | * Check if we must load texts from cache |
||
301 | * |
||
302 | * @return bool |
||
303 | */ |
||
304 | 4 | public function mustLoadFromCache() |
|
308 | |||
309 | 4 | protected function storeTextsInCache(array $texts) |
|
315 | |||
316 | 20 | public function loadTextsFromDatabase($lang) |
|
328 | |||
329 | 1 | public function loadTextsFromCache() |
|
335 | |||
336 | 21 | public function setCacheName($lang) |
|
340 | |||
341 | 4 | public function getCacheName() |
|
345 | |||
346 | 1 | public function getUrl($path) |
|
354 | |||
355 | 2 | public function autoSaveIsAllowed() |
|
362 | |||
363 | 20 | public function getLocale() |
|
367 | |||
368 | 3 | public function saveTexts() |
|
396 | |||
397 | 20 | protected function getTableName() |
|
402 | } |
||
403 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..