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 |
||
| 34 | class Language implements JsonSerializable { |
||
| 35 | use Singleton; |
||
| 36 | /** |
||
| 37 | * Current language |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | public $clanguage; |
||
| 42 | /** |
||
| 43 | * callable for time processing |
||
| 44 | * |
||
| 45 | * @var callable |
||
| 46 | */ |
||
| 47 | public $time; |
||
| 48 | /** |
||
| 49 | * For single initialization |
||
| 50 | * |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $init = false; |
||
| 54 | /** |
||
| 55 | * Local cache of translations |
||
| 56 | * |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $translation = []; |
||
| 60 | /** |
||
| 61 | * Cache to optimize frequent calls |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $localized_url = []; |
||
| 66 | /** |
||
| 67 | * Set basic language |
||
| 68 | */ |
||
| 69 | protected function construct () { |
||
| 98 | /** |
||
| 99 | * Initialization: set default language based on system configuration and request-specific parameters |
||
| 100 | */ |
||
| 101 | protected function init () { |
||
| 123 | /** |
||
| 124 | * Returns instance for simplified work with translations, when using common prefix |
||
| 125 | * |
||
| 126 | * @param string $prefix |
||
| 127 | * |
||
| 128 | * @return Prefix |
||
| 129 | */ |
||
| 130 | static function prefix ($prefix) { |
||
| 133 | /** |
||
| 134 | * Does URL have language prefix |
||
| 135 | * |
||
| 136 | * @param false|string $url Relative url, `Request::instance()->path` by default |
||
| 137 | * |
||
| 138 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
| 139 | */ |
||
| 140 | function url_language ($url = false) { |
||
| 159 | /** |
||
| 160 | * Checking Accept-Language header for languages that exists in configuration |
||
| 161 | * |
||
| 162 | * @param array $active_languages |
||
| 163 | * |
||
| 164 | * @return false|string |
||
| 165 | */ |
||
| 166 | protected function check_accept_header ($active_languages) { |
||
| 184 | /** |
||
| 185 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
| 186 | * |
||
| 187 | * @param string[] $active_languages |
||
| 188 | * |
||
| 189 | * @return false|string |
||
| 190 | */ |
||
| 191 | protected function check_locale_header ($active_languages) { |
||
| 207 | /** |
||
| 208 | * Get languages aliases |
||
| 209 | * |
||
| 210 | * @return array|false |
||
| 211 | */ |
||
| 212 | protected function get_aliases () { |
||
| 225 | /** |
||
| 226 | * Get translation |
||
| 227 | * |
||
| 228 | * @param bool|string $item |
||
| 229 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 230 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | function get ($item, $language = false, $prefix = '') { |
||
| 251 | /** |
||
| 252 | * Set translation |
||
| 253 | * |
||
| 254 | * @param array|string $item Item string, or key-value array |
||
| 255 | * @param null|string $value |
||
| 256 | * |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | function set ($item, $value = null) { |
||
| 267 | /** |
||
| 268 | * Get translation |
||
| 269 | * |
||
| 270 | * @param string $item |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | function __get ($item) { |
||
| 277 | /** |
||
| 278 | * Set translation |
||
| 279 | * |
||
| 280 | * @param array|string $item |
||
| 281 | * @param null|string $value |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | function __set ($item, $value = null) { |
||
| 288 | /** |
||
| 289 | * Change language |
||
| 290 | * |
||
| 291 | * @param string $language |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | */ |
||
| 295 | function change ($language) { |
||
| 333 | /** |
||
| 334 | * Check whether it is allowed to change to specified language according to configuration |
||
| 335 | * |
||
| 336 | * @param Config $Config |
||
| 337 | * @param string $language |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | protected function can_be_changed_to ($Config, $language) { |
||
| 353 | /** |
||
| 354 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
| 355 | * |
||
| 356 | * @param $language |
||
| 357 | * |
||
| 358 | * @return string[] |
||
| 359 | */ |
||
| 360 | protected function get_translation ($language) { |
||
| 400 | /** |
||
| 401 | * @param string $filename |
||
| 402 | * |
||
| 403 | * @return string[] |
||
| 404 | */ |
||
| 405 | protected function get_translation_from_json ($filename) { |
||
| 409 | /** |
||
| 410 | * @param string[]|string[][] $translation |
||
| 411 | * |
||
| 412 | * @return string[] |
||
| 413 | */ |
||
| 414 | protected function get_translation_from_json_internal ($translation) { |
||
| 427 | /** |
||
| 428 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
| 429 | * |
||
| 430 | * @param string[] $translation |
||
| 431 | * @param string $language |
||
| 432 | * |
||
| 433 | * @return string[] |
||
| 434 | */ |
||
| 435 | protected function fill_required_translation_keys ($translation, $language) { |
||
| 452 | /** |
||
| 453 | * Time formatting according to the current language (adding correct endings) |
||
| 454 | * |
||
| 455 | * @param int $in time (number) |
||
| 456 | * @param string $type Type of formatting<br> |
||
| 457 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | function time ($in, $type) { |
||
| 483 | /** |
||
| 484 | * Allows to use formatted strings in translations |
||
| 485 | * |
||
| 486 | * @see format() |
||
| 487 | * |
||
| 488 | * @param string $item |
||
| 489 | * @param array $arguments |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | function __call ($item, $arguments) { |
||
| 496 | /** |
||
| 497 | * Allows to use formatted strings in translations |
||
| 498 | * |
||
| 499 | * @param string $item |
||
| 500 | * @param string[] $arguments |
||
| 501 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 502 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | function format ($item, $arguments, $language = false, $prefix = '') { |
||
| 509 | /** |
||
| 510 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
| 511 | * |
||
| 512 | * @param string|string[] $data |
||
| 513 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
| 514 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
| 515 | * |
||
| 516 | * @return string|string[] |
||
| 517 | */ |
||
| 518 | function to_locale ($data, $short_may = false) { |
||
| 573 | /** |
||
| 574 | * Implementation of JsonSerializable interface |
||
| 575 | * |
||
| 576 | * @return string[] |
||
| 577 | */ |
||
| 578 | function jsonSerialize () { |
||
| 581 | } |
||
| 582 |