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 | 25 | public function __construct($environment, array $config, Cache $cache, Database $db) |
|
93 | |||
94 | 25 | public function setConfig(array $config) |
|
95 | { |
||
96 | 25 | $this->config = $this->getDefaultConfig(); |
|
97 | |||
98 | 25 | foreach ($config as $k => $v) { |
|
99 | 5 | $this->config[$k] = $v; |
|
100 | 25 | } |
|
101 | 25 | } |
|
102 | |||
103 | 25 | public function getDefaultConfig() |
|
104 | { |
||
105 | $config = [ |
||
106 | 'locales' => [ |
||
107 | 'en' => [ |
||
108 | 25 | 'name' => 'English', |
|
109 | 25 | 'native_name' => 'English', |
|
110 | 25 | 'flag' => 'gb.svg', |
|
111 | 25 | 'locale' => 'en', |
|
112 | 25 | ], |
|
113 | 25 | ], |
|
114 | 25 | 'default_locale' => 'en', |
|
115 | 25 | 'autosave' => true, |
|
116 | 25 | 'cache' => true, |
|
117 | 25 | 'cache_lifetime' => 1440, |
|
118 | 25 | 'texts_table' => 'texts', |
|
119 | 25 | ]; |
|
120 | 25 | return $config; |
|
121 | } |
||
122 | |||
123 | 22 | public function getConfig($key = null) |
|
124 | { |
||
125 | 22 | if ($key === null) { |
|
126 | 1 | return $this->config; |
|
127 | } |
||
128 | |||
129 | 22 | return isset($this->config[$key]) ? $this->config[$key] : null; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Set locale and load texts |
||
134 | * |
||
135 | * @param string $lang |
||
136 | * @param string $default_lang |
||
|
|||
137 | * @param array $texts |
||
138 | * @return void |
||
139 | */ |
||
140 | 23 | public function setLocale($lang, $texts = null) |
|
141 | { |
||
142 | 23 | if (!$lang) { |
|
143 | 1 | throw new InvalidArgumentException('Locale is empty'); |
|
144 | } |
||
145 | 22 | $this->lang = $lang; |
|
146 | |||
147 | 22 | $this->setCacheName($lang); |
|
148 | |||
149 | 22 | if (is_array($texts)) { |
|
150 | 3 | $texts = new Collection($texts); |
|
151 | 3 | } else { |
|
152 | 19 | $texts = $this->loadTexts($this->getLocale()); |
|
153 | } |
||
154 | |||
155 | 22 | $this->texts = new Collection($texts); |
|
156 | 22 | } |
|
157 | |||
158 | /** |
||
159 | * Load texts |
||
160 | * |
||
161 | * @param string $lang |
||
162 | * @return array |
||
163 | */ |
||
164 | 19 | public function loadTexts($lang = null) |
|
165 | { |
||
166 | 19 | $cache = $this->getConfig('cache'); |
|
167 | |||
168 | 19 | if (!$cache || $this->cache === null || $this->environment != 'production') { |
|
169 | 16 | $texts = $this->loadTextsFromDatabase($lang); |
|
170 | 16 | return $texts; |
|
171 | } |
||
172 | |||
173 | 4 | if ($this->mustLoadFromCache()) { |
|
174 | $texts = $this->loadTextsFromCache(); |
||
175 | } else { |
||
176 | 4 | $texts = $this->loadTextsFromDatabase($lang); |
|
177 | 4 | $this->storeTextsInCache($texts); |
|
178 | } |
||
179 | |||
180 | 4 | return $texts; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get translated text |
||
185 | * |
||
186 | * @param string $key |
||
187 | * @return string |
||
188 | */ |
||
189 | 8 | public function get($key) |
|
209 | |||
210 | /** |
||
211 | * Get texts |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | 4 | public function getRedirectUrl(Request $request) |
|
244 | |||
245 | 1 | public function detectLocale(Request $request) |
|
259 | |||
260 | /** |
||
261 | * Get texts |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | 4 | public function getTexts() |
|
270 | |||
271 | /** |
||
272 | * Set texts manually |
||
273 | * |
||
274 | * @param array $texts_array |
||
275 | * @return \Longman\LaravelMultiLang\MultiLang |
||
276 | */ |
||
277 | 3 | public function setTexts(array $texts_array) |
|
288 | |||
289 | /** |
||
290 | * Queue missing texts |
||
291 | * |
||
292 | * @param string $key |
||
293 | * @return void |
||
294 | */ |
||
295 | 4 | protected function queueToSave($key) |
|
299 | |||
300 | /** |
||
301 | * Check if we must load texts from cache |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | 4 | public function mustLoadFromCache() |
|
309 | |||
310 | 4 | protected function storeTextsInCache(array $texts) |
|
316 | |||
317 | 19 | public function loadTextsFromDatabase($lang) |
|
329 | |||
330 | 1 | public function loadTextsFromCache() |
|
336 | |||
337 | 22 | public function setCacheName($lang) |
|
341 | |||
342 | 4 | public function getCacheName() |
|
346 | |||
347 | 2 | public function getUrl($path) |
|
355 | |||
356 | 4 | public function autoSaveIsAllowed() |
|
363 | |||
364 | 20 | public function getLocale() |
|
368 | |||
369 | 3 | public function saveTexts() |
|
397 | |||
398 | 19 | protected function getTableName() |
|
403 | } |
||
404 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.