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 | 62 | protected function init () { |
|
| 113 | /** |
||
| 114 | * Returns instance for simplified work with translations, when using common prefix |
||
| 115 | * |
||
| 116 | * @param string $prefix |
||
| 117 | * |
||
| 118 | * @return Prefix |
||
| 119 | */ |
||
| 120 | 22 | public static function prefix ($prefix) { |
|
| 123 | /** |
||
| 124 | * Does URL have language prefix |
||
| 125 | * |
||
| 126 | * @param false|string $url Relative url, `Request::instance()->path` by default |
||
| 127 | * |
||
| 128 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
| 129 | */ |
||
| 130 | 36 | public function url_language ($url = false) { |
|
| 149 | /** |
||
| 150 | * Checking Accept-Language header for languages that exists in configuration |
||
| 151 | * |
||
| 152 | * @param array $active_languages |
||
| 153 | * |
||
| 154 | * @return false|string |
||
| 155 | */ |
||
| 156 | 6 | protected function check_accept_header ($active_languages) { |
|
| 174 | /** |
||
| 175 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
| 176 | * |
||
| 177 | * @param string[] $active_languages |
||
| 178 | * |
||
| 179 | * @return false|string |
||
| 180 | */ |
||
| 181 | 10 | protected function check_locale_header ($active_languages) { |
|
| 197 | /** |
||
| 198 | * Get languages aliases |
||
| 199 | * |
||
| 200 | * @return array|false |
||
| 201 | */ |
||
| 202 | 36 | protected function get_aliases () { |
|
| 218 | /** |
||
| 219 | * Get translation |
||
| 220 | * |
||
| 221 | * @param bool|string $item |
||
| 222 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 223 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 54 | public function get ($item, $language = false, $prefix = '') { |
|
| 250 | /** |
||
| 251 | * Set translation |
||
| 252 | * |
||
| 253 | * @param array|string $item Item string, or key-value array |
||
| 254 | * @param null|string $value |
||
| 255 | * |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | public function set ($item, $value = null) { |
||
| 266 | /** |
||
| 267 | * Get translation |
||
| 268 | * |
||
| 269 | * @param string $item |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | 54 | public function __get ($item) { |
|
| 276 | /** |
||
| 277 | * Set translation |
||
| 278 | * |
||
| 279 | * @param array|string $item |
||
| 280 | * @param null|string $value |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function __set ($item, $value = null) { |
||
| 287 | /** |
||
| 288 | * Change language |
||
| 289 | * |
||
| 290 | * @param string $language |
||
| 291 | * |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | 54 | public function change ($language) { |
|
| 325 | /** |
||
| 326 | * Check whether it is allowed to change to specified language according to configuration |
||
| 327 | * |
||
| 328 | * @param Config $Config |
||
| 329 | * @param string $language |
||
| 330 | * |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | 54 | protected function can_be_changed_to ($Config, $language) { |
|
| 356 | /** |
||
| 357 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
| 358 | * |
||
| 359 | * @param $language |
||
| 360 | * |
||
| 361 | * @return string[] |
||
| 362 | */ |
||
| 363 | 10 | protected function get_translation_internal ($language) { |
|
| 403 | /** |
||
| 404 | * @param string $filename |
||
| 405 | * |
||
| 406 | * @return string[] |
||
| 407 | */ |
||
| 408 | 10 | protected function get_translation_from_json ($filename) { |
|
| 413 | /** |
||
| 414 | * @param string[]|string[][] $translation |
||
| 415 | * |
||
| 416 | * @return string[] |
||
| 417 | */ |
||
| 418 | 10 | protected function get_translation_from_json_internal ($translation) { |
|
| 431 | /** |
||
| 432 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
| 433 | * |
||
| 434 | * @param string[] $translation |
||
| 435 | * @param string $language |
||
| 436 | * |
||
| 437 | * @return string[] |
||
| 438 | */ |
||
| 439 | 10 | protected function fill_required_translation_keys ($translation, $language) { |
|
| 451 | /** |
||
| 452 | * Time formatting according to the current language (adding correct endings) |
||
| 453 | * |
||
| 454 | * @param int $in time (number) |
||
| 455 | * @param string $type Type of formatting<br> |
||
| 456 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
| 457 | * |
||
| 458 | * @return string |
||
| 459 | */ |
||
| 460 | 4 | public function time ($in, $type) { |
|
| 482 | /** |
||
| 483 | * Allows to use formatted strings in translations |
||
| 484 | * |
||
| 485 | * @see format() |
||
| 486 | * |
||
| 487 | * @param string $item |
||
| 488 | * @param array $arguments |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | 2 | public function __call ($item, $arguments) { |
|
| 495 | /** |
||
| 496 | * Allows to use formatted strings in translations |
||
| 497 | * |
||
| 498 | * @param string $item |
||
| 499 | * @param string[] $arguments |
||
| 500 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 501 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | 6 | public function format ($item, $arguments, $language = false, $prefix = '') { |
|
| 508 | /** |
||
| 509 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
| 510 | * |
||
| 511 | * @param string|string[] $data |
||
| 512 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
| 513 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
| 514 | * |
||
| 515 | * @return string|string[] |
||
| 516 | */ |
||
| 517 | 2 | public function to_locale ($data, $short_may = false) { |
|
| 572 | /** |
||
| 573 | * Implementation of JsonSerializable interface |
||
| 574 | * |
||
| 575 | * @return string[] |
||
| 576 | */ |
||
| 577 | 4 | public function jsonSerialize () { |
|
| 580 | } |
||
| 581 |