Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
20 | class MultiLang |
||
21 | { |
||
22 | /** |
||
23 | * Language/Locale. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $lang; |
||
28 | |||
29 | /** |
||
30 | * System environment |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $environment; |
||
35 | |||
36 | /** |
||
37 | * The instance of the cache. |
||
38 | * |
||
39 | * @var \Illuminate\Cache\CacheManager |
||
40 | */ |
||
41 | protected $cache; |
||
42 | |||
43 | /** |
||
44 | * Config. |
||
45 | * |
||
46 | * @var \Longman\LaravelMultiLang\Config |
||
47 | */ |
||
48 | protected $config; |
||
49 | |||
50 | /** |
||
51 | * The instance of the database. |
||
52 | * |
||
53 | * @var \Illuminate\Database\DatabaseManager |
||
54 | */ |
||
55 | protected $db; |
||
56 | |||
57 | /** |
||
58 | * Name of the cache. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $cache_name; |
||
63 | |||
64 | /** |
||
65 | * Texts. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $texts; |
||
70 | |||
71 | /** |
||
72 | * Missing texts. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $new_texts; |
||
77 | |||
78 | /** |
||
79 | * Create a new MultiLang instance. |
||
80 | * |
||
81 | * @param string $environment |
||
82 | * @param array $config |
||
83 | * @param \Illuminate\Cache\CacheManager $cache |
||
84 | * @param \Illuminate\Database\DatabaseManager $db |
||
85 | */ |
||
86 | public function __construct($environment, array $config, Cache $cache, Database $db) |
||
97 | |||
98 | public function setConfig(array $config) |
||
103 | |||
104 | public function setRepository(Repository $repository) |
||
109 | |||
110 | /** |
||
111 | * Get a cache driver instance. |
||
112 | * |
||
113 | * @return \Illuminate\Contracts\Cache\Repository |
||
114 | */ |
||
115 | View Code Duplication | public function getCache() |
|
126 | |||
127 | /** |
||
128 | * Get a database connection instance. |
||
129 | * |
||
130 | * @return \Illuminate\Database\Connection |
||
131 | */ |
||
132 | View Code Duplication | public function getDb() |
|
140 | |||
141 | /** |
||
142 | * Set locale and load texts |
||
143 | * |
||
144 | * @param string $lang |
||
145 | * @param array $texts |
||
146 | * @return void |
||
147 | */ |
||
148 | public function setLocale($lang, array $texts = null) |
||
163 | |||
164 | /** |
||
165 | * Load texts |
||
166 | * |
||
167 | * @param string $lang |
||
168 | * @return array |
||
169 | */ |
||
170 | public function loadTexts($lang = null) |
||
186 | |||
187 | /** |
||
188 | * Get translated text |
||
189 | * |
||
190 | * @param string $key |
||
191 | * @return string |
||
192 | */ |
||
193 | public function get($key) |
||
213 | |||
214 | /** |
||
215 | * Get texts |
||
216 | * |
||
217 | * @return array |
||
218 | */ |
||
219 | public function getRedirectUrl(Request $request) |
||
248 | |||
249 | public function detectLocale(Request $request) |
||
260 | |||
261 | /** |
||
262 | * Get texts |
||
263 | * |
||
264 | * @return array |
||
265 | */ |
||
266 | public function getTexts() |
||
271 | |||
272 | /** |
||
273 | * Set texts manually |
||
274 | * |
||
275 | * @param array $texts_array |
||
276 | * @return \Longman\LaravelMultiLang\MultiLang |
||
277 | */ |
||
278 | public function setTexts(array $texts_array) |
||
289 | |||
290 | /** |
||
291 | * Queue missing texts |
||
292 | * |
||
293 | * @param string $key |
||
294 | * @return void |
||
295 | */ |
||
296 | protected function queueToSave($key) |
||
300 | |||
301 | /** |
||
302 | * Check if we must load texts from cache |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | public function mustLoadFromCache() |
||
310 | |||
311 | protected function storeTextsInCache(array $texts) |
||
316 | |||
317 | View Code Duplication | public function loadTextsFromDatabase($lang) |
|
329 | |||
330 | public function loadTextsFromCache() |
||
336 | |||
337 | public function setCacheName($lang) |
||
341 | |||
342 | public function getCacheName() |
||
346 | |||
347 | public function getUrl($path) |
||
355 | |||
356 | public function autoSaveIsAllowed() |
||
363 | |||
364 | public function getLocale() |
||
368 | |||
369 | public function getLocales() |
||
373 | |||
374 | public function saveTexts() |
||
403 | |||
404 | protected function getTableName() |
||
409 | } |
||
410 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.