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 |
||
22 | class MultiLang |
||
23 | { |
||
24 | /** |
||
25 | * Language/Locale. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $lang; |
||
30 | |||
31 | /** |
||
32 | * System environment |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $environment; |
||
37 | |||
38 | /** |
||
39 | * Config. |
||
40 | * |
||
41 | * @var \Longman\LaravelMultiLang\Config |
||
42 | */ |
||
43 | protected $config; |
||
44 | |||
45 | /** |
||
46 | * Repository |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $repository; |
||
51 | |||
52 | /** |
||
53 | * Texts. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $texts; |
||
58 | |||
59 | /** |
||
60 | * Missing texts. |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $new_texts; |
||
65 | |||
66 | /** |
||
67 | * Create a new MultiLang instance. |
||
68 | * |
||
69 | * @param string $environment |
||
70 | * @param array $config |
||
71 | * @param \Illuminate\Cache\CacheManager $cache |
||
72 | * @param \Illuminate\Database\DatabaseManager $db |
||
73 | */ |
||
74 | 23 | public function __construct($environment, array $config, Cache $cache, Database $db) |
|
84 | |||
85 | 23 | public function setConfig(array $config) |
|
90 | |||
91 | 23 | public function setRepository(Repository $repository) |
|
96 | |||
97 | 2 | public function getRepository() |
|
101 | |||
102 | /** |
||
103 | * Set locale and load texts |
||
104 | * |
||
105 | * @param string $lang |
||
106 | * @param array $texts |
||
107 | * @return void |
||
108 | */ |
||
109 | 21 | public function setLocale($lang, array $texts = null) |
|
122 | |||
123 | /** |
||
124 | * Load texts |
||
125 | * |
||
126 | * @param string $lang |
||
127 | * @return array |
||
128 | */ |
||
129 | 17 | public function loadTexts($lang) |
|
145 | |||
146 | /** |
||
147 | * Get translated text |
||
148 | * |
||
149 | * @param string $key |
||
150 | * @return string |
||
151 | */ |
||
152 | 8 | public function get($key) |
|
172 | |||
173 | /** |
||
174 | * Get texts |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | 4 | public function getRedirectUrl(Request $request) |
|
211 | |||
212 | 1 | public function detectLocale(Request $request) |
|
223 | |||
224 | public function routeGroup(Closure $callback) |
||
238 | |||
239 | public function manageTextsRoutes() |
||
254 | |||
255 | /** |
||
256 | * Get texts |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | 4 | public function getTexts() |
|
265 | |||
266 | /** |
||
267 | * Set texts manually |
||
268 | * |
||
269 | * @param array $texts_array |
||
270 | * @return \Longman\LaravelMultiLang\MultiLang |
||
271 | */ |
||
272 | 3 | public function setTexts(array $texts_array) |
|
283 | |||
284 | /** |
||
285 | * Queue missing texts |
||
286 | * |
||
287 | * @param string $key |
||
288 | * @return void |
||
289 | */ |
||
290 | 4 | protected function queueToSave($key) |
|
294 | |||
295 | 2 | public function getUrl($path) |
|
303 | |||
304 | 5 | public function autoSaveIsAllowed() |
|
311 | |||
312 | 18 | public function getLocale() |
|
316 | |||
317 | 1 | public function getLocales() |
|
321 | |||
322 | 3 | public function saveTexts() |
|
331 | } |
||
332 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: