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 laravel app. |
||
36 | * |
||
37 | * @var \Illuminate\Foundation\Application |
||
38 | */ |
||
39 | protected $app; |
||
40 | |||
41 | /** |
||
42 | * The instance of the cache. |
||
43 | * |
||
44 | * @var \Illuminate\Cache\CacheManager |
||
45 | */ |
||
46 | protected $cache; |
||
47 | |||
48 | /** |
||
49 | * Config. |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $config; |
||
54 | |||
55 | /** |
||
56 | * The instance of the database. |
||
57 | * |
||
58 | * @var \Illuminate\Database\DatabaseManager |
||
59 | */ |
||
60 | protected $db; |
||
61 | |||
62 | /** |
||
63 | * Name of the cache. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $cache_name; |
||
68 | |||
69 | /** |
||
70 | * Texts collection. |
||
71 | * |
||
72 | * @var \Illuminate\Support\Collection |
||
73 | */ |
||
74 | protected $texts; |
||
75 | |||
76 | /** |
||
77 | * Missing texts. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $new_texts; |
||
82 | |||
83 | /** |
||
84 | * Create a new MultiLang instance. |
||
85 | * |
||
86 | * @param string $environment |
||
87 | * @param array $config |
||
88 | * @param \Illuminate\Cache\CacheManager $cache |
||
89 | * @param \Illuminate\Database\DatabaseManager $db |
||
90 | * @return void |
||
|
|||
91 | */ |
||
92 | public function __construct($environment, array $config, CacheContract $cache, DatabaseContract $db) |
||
100 | |||
101 | public function setConfig(array $config) |
||
122 | |||
123 | public function getConfig($key = null) |
||
131 | |||
132 | /** |
||
133 | * Set locale and load texts |
||
134 | * |
||
135 | * @param string $lang |
||
136 | * @param \Illuminate\Support\Collection|array $text |
||
137 | * @return void |
||
138 | */ |
||
139 | public function setLocale($lang, $texts = null) |
||
154 | |||
155 | /** |
||
156 | * Load texts |
||
157 | * |
||
158 | * @param string $lang |
||
159 | * @return \Illuminate\Support\Collection |
||
160 | */ |
||
161 | public function loadTexts($lang = null) |
||
181 | |||
182 | /** |
||
183 | * Get translated text |
||
184 | * |
||
185 | * @param string $key |
||
186 | * @param string $default |
||
187 | * @return string |
||
188 | */ |
||
189 | public function get($key) |
||
209 | |||
210 | /** |
||
211 | * Get texts |
||
212 | * |
||
213 | * @param string $lang |
||
214 | * @return string |
||
215 | */ |
||
216 | public function getTexts($lang = null) |
||
221 | |||
222 | /** |
||
223 | * Set texts manually |
||
224 | * |
||
225 | * @param array $texts_array |
||
226 | * @return \Longman\LaravelMultiLang\MultiLang |
||
227 | */ |
||
228 | public function setTexts(array $texts_array) |
||
239 | |||
240 | /** |
||
241 | * Queue missing texts |
||
242 | * |
||
243 | * @param string $key |
||
244 | * @return void |
||
245 | */ |
||
246 | protected function queueToSave($key) |
||
250 | |||
251 | /** |
||
252 | * Check if we must load texts from cache |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | protected function mustLoadFromCache() |
||
260 | |||
261 | protected function storeTextsInCache(array $texts) |
||
267 | |||
268 | protected function loadTextsFromDatabase($lang) |
||
280 | |||
281 | protected function loadTextsFromCache() |
||
286 | |||
287 | protected function setCacheName() |
||
291 | |||
292 | public function getUrl($path) |
||
300 | |||
301 | public function autoSaveIsAllowed() |
||
308 | |||
309 | public function getLocale() |
||
313 | |||
314 | public function saveTexts() |
||
345 | |||
346 | protected function getTableName($with_prefix = false) |
||
355 | } |
||
356 |
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.