Complex classes like I18N 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 I18N, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Rocket\Translation; |
||
16 | class I18N implements I18NInterface |
||
17 | { |
||
18 | /** |
||
19 | * An array of the loaded languages |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $languagesLoaded = []; |
||
23 | |||
24 | /** |
||
25 | * An array of existing languages by ISO |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $languagesIso = []; |
||
29 | |||
30 | /** |
||
31 | * An array of existing languages by ID |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $languagesId = []; |
||
35 | |||
36 | /** |
||
37 | * @var integer |
||
38 | */ |
||
39 | protected $defaultLanguage; |
||
40 | |||
41 | /** |
||
42 | * Language currently in use |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $currentLanguage; |
||
46 | |||
47 | /** |
||
48 | * Language currently in use (ID) |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $currentLanguageId; |
||
52 | |||
53 | /** |
||
54 | * All the translation strings |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $strings = []; |
||
58 | |||
59 | /** |
||
60 | * Context of the current page |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $pageContext; |
||
64 | |||
65 | /** |
||
66 | * @var CacheRepository The cache to store the terms in |
||
67 | */ |
||
68 | protected $cache; |
||
69 | |||
70 | /** |
||
71 | * @var Session The session store |
||
72 | */ |
||
73 | protected $session; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | protected $languageFilesPath; |
||
79 | |||
80 | /** |
||
81 | * @var Router |
||
82 | */ |
||
83 | 177 | protected $router; |
|
84 | |||
85 | 177 | /** |
|
86 | 177 | * Prepare the translation service |
|
87 | 177 | * |
|
88 | 177 | * @param Application $app |
|
89 | */ |
||
90 | 177 | public function __construct( |
|
127 | |||
128 | /** |
||
129 | 177 | * Detects the default languages in the following order : |
|
130 | * |
||
131 | 177 | * 1. Is a user session var defined ? |
|
132 | * 2. Can we take it from the browser ? |
||
133 | * 3. Take the site default |
||
134 | * |
||
135 | * @param $locale string |
||
136 | * @param $fallback string |
||
137 | 177 | * @throws \RuntimeException if a default language cannot be found |
|
138 | * @return string |
||
139 | */ |
||
140 | public function getCurrentLanguage($locale, $fallback, Session $session, Request $request) |
||
176 | |||
177 | /** |
||
178 | * Load a language file |
||
179 | * |
||
180 | * @param string $language |
||
181 | * @return bool |
||
182 | */ |
||
183 | public function loadLanguage($language) |
||
201 | |||
202 | 177 | /** |
|
203 | * Get the current language |
||
204 | * @return string |
||
205 | */ |
||
206 | 177 | public function getCurrent() |
|
210 | |||
211 | /** |
||
212 | * Get the current language id |
||
213 | 27 | * @return int |
|
214 | */ |
||
215 | 27 | public function getCurrentId() |
|
219 | |||
220 | /** |
||
221 | * Set the language to use |
||
222 | 150 | * |
|
223 | * @param string $language |
||
224 | 150 | * @throws \RuntimeException if the language doesn't exist |
|
225 | */ |
||
226 | public function setLanguageForRequest($language) |
||
255 | 177 | ||
256 | 177 | /** |
|
257 | 177 | * Set the current language |
|
258 | * |
||
259 | * @param string $language |
||
260 | * @throws \RuntimeException if the language doesn't exist |
||
261 | */ |
||
262 | public function setLanguageForSession($language) |
||
272 | |||
273 | /** |
||
274 | * Checks if a language is loaded or not |
||
275 | * |
||
276 | * @param string $language |
||
277 | * @return bool |
||
278 | */ |
||
279 | protected function isLoaded($language) |
||
283 | |||
284 | /** |
||
285 | * Checks if a language is the default one |
||
286 | * |
||
287 | * @param string $language |
||
288 | * @return bool |
||
289 | */ |
||
290 | protected function isDefault($language) |
||
298 | |||
299 | /** |
||
300 | * Checks if the language is availavble |
||
301 | * |
||
302 | * @param string $language |
||
303 | * @return bool |
||
304 | */ |
||
305 | protected function isAvailable($language) |
||
309 | |||
310 | /** |
||
311 | * Retrieve languages. |
||
312 | * |
||
313 | * this is a hybrid method. |
||
314 | * |
||
315 | * |
||
316 | * I18N::languages(); |
||
317 | * returns ['fr' => ['id' => 1, 'name' => 'francais', 'iso' => 'fr'], 'en' => ...] |
||
318 | * |
||
319 | * |
||
320 | * I18N::languages('fr'); |
||
321 | 36 | * returns ['id' => 1, 'name' => 'francais', 'iso' => 'fr'] |
|
322 | * |
||
323 | 36 | * |
|
324 | 33 | * I18N::languages(1); |
|
325 | * returns ['id' => 1, 'name' => 'francais', 'iso' => 'fr'] |
||
326 | * |
||
327 | 3 | * |
|
328 | * I18N::languages('fr', 'id'); |
||
329 | * returns 1 |
||
330 | * |
||
331 | * @param int|string $key |
||
332 | * @param string $subkey |
||
333 | * @return array |
||
334 | */ |
||
335 | 3 | public function languages($key = null, $subkey = null) |
|
355 | |||
356 | public function languagesForSelect() |
||
365 | |||
366 | /** |
||
367 | * Retreive a string to translate |
||
368 | * |
||
369 | * if it doesn't find it, put it in the database |
||
370 | * |
||
371 | * @param string $keyString |
||
372 | * @param string $context |
||
373 | * @param string $language |
||
374 | * @return string |
||
375 | */ |
||
376 | public function translate($keyString, $context = 'default', $language = 'default') |
||
412 | |||
413 | /** |
||
414 | * Get the page's context |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | public function getContext() |
||
440 | |||
441 | public function dumpCache() |
||
464 | } |
||
465 |