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(Application $app, CacheRepository $cache, Session $session, ConfigRepository $config, Router $router, Request $request) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Detects the default languages in the following order : |
||
| 123 | * |
||
| 124 | * 1. Is a user session var defined ? |
||
| 125 | * 2. Can we take it from the browser ? |
||
| 126 | 177 | * 3. Take the site default |
|
| 127 | * |
||
| 128 | * @param $locale string |
||
| 129 | 177 | * @param $fallback string |
|
| 130 | * @throws \RuntimeException if a default language cannot be found |
||
| 131 | 177 | * @return string |
|
| 132 | */ |
||
| 133 | public function getCurrentLanguage($locale, $fallback, Session $session, Request $request) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Load a language file |
||
| 172 | * |
||
| 173 | * @param string $language |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function loadLanguage($language) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the current language |
||
| 197 | 177 | * @return string |
|
| 198 | */ |
||
| 199 | 177 | public function getCurrent() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Get the current language id |
||
| 206 | 177 | * @return int |
|
| 207 | 177 | */ |
|
| 208 | public function getCurrentId() |
||
| 212 | |||
| 213 | 27 | /** |
|
| 214 | * Set the language to use |
||
| 215 | 27 | * |
|
| 216 | * @param string $language |
||
| 217 | * @throws \RuntimeException if the language doesn't exist |
||
| 218 | */ |
||
| 219 | public function setLanguageForRequest($language) |
||
| 248 | 177 | ||
| 249 | 177 | /** |
|
| 250 | * Set the current language |
||
| 251 | * |
||
| 252 | * @param string $language |
||
| 253 | * @throws \RuntimeException if the language doesn't exist |
||
| 254 | */ |
||
| 255 | 177 | public function setLanguageForSession($language) |
|
| 265 | 177 | ||
| 266 | /** |
||
| 267 | 177 | * Checks if a language is loaded or not |
|
| 268 | * |
||
| 269 | * @param string $language |
||
| 270 | * @return bool |
||
| 271 | */ |
||
| 272 | protected function isLoaded($language) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Checks if a language is the default one |
||
| 279 | * |
||
| 280 | * @param string $language |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | protected function isDefault($language) |
||
| 291 | 177 | ||
| 292 | /** |
||
| 293 | 177 | * Checks if the language is availavble |
|
| 294 | * |
||
| 295 | * @param string $language |
||
| 296 | * @return bool |
||
| 297 | */ |
||
| 298 | protected function isAvailable($language) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Retrieve languages. |
||
| 305 | * |
||
| 306 | * this is a hybrid method. |
||
| 307 | * |
||
| 308 | * |
||
| 309 | * I18N::languages(); |
||
| 310 | * returns ['fr' => ['id' => 1, 'name' => 'francais', 'iso' => 'fr'], 'en' => ...] |
||
| 311 | * |
||
| 312 | * |
||
| 313 | * I18N::languages('fr'); |
||
| 314 | * returns ['id' => 1, 'name' => 'francais', 'iso' => 'fr'] |
||
| 315 | * |
||
| 316 | * |
||
| 317 | * I18N::languages(1); |
||
| 318 | * returns ['id' => 1, 'name' => 'francais', 'iso' => 'fr'] |
||
| 319 | * |
||
| 320 | * |
||
| 321 | 36 | * I18N::languages('fr', 'id'); |
|
| 322 | * returns 1 |
||
| 323 | 36 | * |
|
| 324 | 33 | * @param int|string $key |
|
| 325 | * @param string $subkey |
||
| 326 | * @return array |
||
| 327 | 3 | */ |
|
| 328 | public function languages($key = null, $subkey = null) |
||
| 348 | |||
| 349 | public function languagesForSelect() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Retreive a string to translate |
||
| 361 | * |
||
| 362 | * if it doesn't find it, put it in the database |
||
| 363 | * |
||
| 364 | * @param string $keyString |
||
| 365 | * @param string $context |
||
| 366 | * @param string $language |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | public function translate($keyString, $context = 'default', $language = 'default') |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Get the page's context |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function getContext() |
||
| 428 | |||
| 429 | public function dumpCache() |
||
| 452 | } |
||
| 453 |