Complex classes like Language 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 Language, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Language implements JsonSerializable { |
||
| 38 | use |
||
| 39 | Singleton; |
||
| 40 | const INIT_STATE_METHOD = 'init'; |
||
| 41 | /** |
||
| 42 | * Callable for time processing |
||
| 43 | * |
||
| 44 | * @var callable |
||
| 45 | */ |
||
| 46 | public $time; |
||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $current_language; |
||
| 51 | /** |
||
| 52 | * Local cache of translations |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $translation = []; |
||
| 57 | /** |
||
| 58 | * Cache to optimize frequent calls |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $localized_url = []; |
||
| 63 | 84 | protected function init () { |
|
| 64 | /** |
||
| 65 | * Initialization: set default language based on system configuration and request-specific parameters |
||
| 66 | */ |
||
| 67 | 84 | $Config = Config::instance(true); |
|
| 68 | /** |
||
| 69 | * We need Config for initialization |
||
| 70 | */ |
||
| 71 | 84 | if (!$Config) { |
|
| 72 | Event::instance()->once( |
||
| 73 | 'System/Config/init/after', |
||
| 74 | function () { |
||
| 75 | $this->init_internal(); |
||
| 76 | } |
||
| 77 | ); |
||
| 78 | } else { |
||
| 79 | 84 | $this->init_internal(); |
|
| 80 | } |
||
| 81 | 84 | } |
|
| 82 | 84 | protected function init_internal () { |
|
| 104 | /** |
||
| 105 | * Returns instance for simplified work with translations, when using common prefix |
||
| 106 | * |
||
| 107 | * @param string $prefix |
||
| 108 | * |
||
| 109 | * @return Prefix |
||
| 110 | */ |
||
| 111 | 10 | public static function prefix ($prefix) { |
|
| 114 | /** |
||
| 115 | * Does URL have language prefix |
||
| 116 | * |
||
| 117 | * @param false|string $url Relative url, `Request::instance()->path` by default |
||
| 118 | * |
||
| 119 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
| 120 | */ |
||
| 121 | 52 | public function url_language ($url = false) { |
|
| 140 | /** |
||
| 141 | * Checking Accept-Language header for languages that exists in configuration |
||
| 142 | * |
||
| 143 | * @param array $active_languages |
||
| 144 | * |
||
| 145 | * @return false|string |
||
| 146 | */ |
||
| 147 | 8 | protected function check_accept_header ($active_languages) { |
|
| 165 | /** |
||
| 166 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
| 167 | * |
||
| 168 | * @param string[] $active_languages |
||
| 169 | * |
||
| 170 | * @return false|string |
||
| 171 | */ |
||
| 172 | 12 | protected function check_locale_header ($active_languages) { |
|
| 188 | /** |
||
| 189 | * Get languages aliases |
||
| 190 | * |
||
| 191 | * @return array|false |
||
| 192 | */ |
||
| 193 | 52 | protected function get_aliases () { |
|
| 209 | /** |
||
| 210 | * Get translation |
||
| 211 | * |
||
| 212 | * @param bool|string $item |
||
| 213 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 214 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | 82 | public function get ($item, $language = false, $prefix = '') { |
|
| 241 | /** |
||
| 242 | * Set translation |
||
| 243 | * |
||
| 244 | * @param array|string $item Item string, or key-value array |
||
| 245 | * @param null|string $value |
||
| 246 | * |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | public function set ($item, $value = null) { |
||
| 250 | $translate = &$this->translation[$this->current_language]; |
||
| 251 | if (is_array($item)) { |
||
| 252 | $translate = $item + ($translate ?: []); |
||
| 253 | } else { |
||
| 254 | $translate[$item] = $value; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | /** |
||
| 258 | * Get translation |
||
| 259 | * |
||
| 260 | * @param string $item |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | 82 | public function __get ($item) { |
|
| 267 | /** |
||
| 268 | * Set translation |
||
| 269 | * |
||
| 270 | * @param array|string $item |
||
| 271 | * @param null|string $value |
||
| 272 | */ |
||
| 273 | public function __set ($item, $value = null) { |
||
| 274 | $this->set($item, $value); |
||
| 275 | } |
||
| 276 | /** |
||
| 277 | * Change language |
||
| 278 | * |
||
| 279 | * @param string $language |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | 66 | public function change ($language) { |
|
| 315 | /** |
||
| 316 | * Check whether it is allowed to change to specified language according to configuration |
||
| 317 | * |
||
| 318 | * @param Config $Config |
||
| 319 | * @param string $language |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | 66 | protected function can_be_changed_to ($Config, $language) { |
|
| 346 | /** |
||
| 347 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
| 348 | * |
||
| 349 | * @param $language |
||
| 350 | * |
||
| 351 | * @return string[] |
||
| 352 | */ |
||
| 353 | 12 | protected function get_translation_internal ($language) { |
|
| 384 | /** |
||
| 385 | * @param string $filename |
||
| 386 | * |
||
| 387 | * @return string[] |
||
| 388 | */ |
||
| 389 | 12 | protected function get_translation_from_json ($filename) { |
|
| 394 | /** |
||
| 395 | * @param string[]|string[][] $translation |
||
| 396 | * |
||
| 397 | * @return string[] |
||
| 398 | */ |
||
| 399 | 12 | protected function get_translation_from_json_internal ($translation) { |
|
| 412 | /** |
||
| 413 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
| 414 | * |
||
| 415 | * @param string[] $translation |
||
| 416 | * @param string $language |
||
| 417 | * |
||
| 418 | * @return string[] |
||
| 419 | */ |
||
| 420 | 12 | protected function fill_required_translation_keys ($translation, $language) { |
|
| 432 | /** |
||
| 433 | * Time formatting according to the current language (adding correct endings) |
||
| 434 | * |
||
| 435 | * @param int $in time (number) |
||
| 436 | * @param string $type Type of formatting<br> |
||
| 437 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | 8 | public function time ($in, $type) { |
|
| 456 | /** |
||
| 457 | * Allows to use formatted strings in translations |
||
| 458 | * |
||
| 459 | * @see format() |
||
| 460 | * |
||
| 461 | * @param string $item |
||
| 462 | * @param array $arguments |
||
| 463 | * |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | 2 | public function __call ($item, $arguments) { |
|
| 469 | /** |
||
| 470 | * Allows to use formatted strings in translations |
||
| 471 | * |
||
| 472 | * @param string $item |
||
| 473 | * @param string[] $arguments |
||
| 474 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 475 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 476 | * |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | 8 | public function format ($item, $arguments, $language = false, $prefix = '') { |
|
| 482 | /** |
||
| 483 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
| 484 | * |
||
| 485 | * @param string|string[] $data |
||
| 486 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
| 487 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
| 488 | * |
||
| 489 | * @return string|string[] |
||
| 490 | */ |
||
| 491 | 2 | public function to_locale ($data, $short_may = false) { |
|
| 543 | /** |
||
| 544 | * Implementation of JsonSerializable interface |
||
| 545 | * |
||
| 546 | * @return string[] |
||
| 547 | */ |
||
| 548 | 4 | public function jsonSerialize () { |
|
| 553 | } |
||
| 554 |