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 |
||
| 23 | class Language implements JsonSerializable { |
||
| 24 | use Singleton; |
||
| 25 | /** |
||
| 26 | * Current language |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $clanguage; |
||
| 31 | /** |
||
| 32 | * callable for time processing |
||
| 33 | * |
||
| 34 | * @var callable |
||
| 35 | */ |
||
| 36 | public $time; |
||
| 37 | /** |
||
| 38 | * For single initialization |
||
| 39 | * |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $init = false; |
||
| 43 | /** |
||
| 44 | * Local cache of translations |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $translation = []; |
||
| 49 | /** |
||
| 50 | * Cache to optimize frequent calls |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $localized_url = []; |
||
| 55 | /** |
||
| 56 | * Set basic language |
||
| 57 | */ |
||
| 58 | protected function construct () { |
||
| 87 | /** |
||
| 88 | * Initialization: set default language based on system configuration and request-specific parameters |
||
| 89 | */ |
||
| 90 | protected function init () { |
||
| 113 | /** |
||
| 114 | * Does URL have language prefix |
||
| 115 | * |
||
| 116 | * @param false|string $url Relative url, `$_SERVER->request_uri` by default |
||
| 117 | * |
||
| 118 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
| 119 | */ |
||
| 120 | function url_language ($url = false) { |
||
| 133 | /** |
||
| 134 | * Checking Accept-Language header for languages that exists in configuration |
||
| 135 | * |
||
| 136 | * @param array $active_languages |
||
| 137 | * |
||
| 138 | * @return false|string |
||
| 139 | */ |
||
| 140 | protected function check_accept_header ($active_languages) { |
||
| 161 | /** |
||
| 162 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
| 163 | * |
||
| 164 | * @param string[] $active_languages |
||
| 165 | * |
||
| 166 | * @return false|string |
||
| 167 | */ |
||
| 168 | protected function check_locale_header ($active_languages) { |
||
| 187 | /** |
||
| 188 | * Get languages aliases |
||
| 189 | * |
||
| 190 | * @return array|false |
||
| 191 | */ |
||
| 192 | protected function get_aliases () { |
||
| 205 | /** |
||
| 206 | * Get translation |
||
| 207 | * |
||
| 208 | * @param bool|string $item |
||
| 209 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 210 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | function get ($item, $language = false, $prefix = '') { |
||
| 231 | /** |
||
| 232 | * Set translation |
||
| 233 | * |
||
| 234 | * @param array|string $item Item string, or key-value array |
||
| 235 | * @param null|string $value |
||
| 236 | * |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | function set ($item, $value = null) { |
||
| 247 | /** |
||
| 248 | * Get translation |
||
| 249 | * |
||
| 250 | * @param string $item |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | function __get ($item) { |
||
| 257 | /** |
||
| 258 | * Set translation |
||
| 259 | * |
||
| 260 | * @param array|string $item |
||
| 261 | * @param null|string $value |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | function __set ($item, $value = null) { |
||
| 268 | /** |
||
| 269 | * Change language |
||
| 270 | * |
||
| 271 | * @param string $language |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | function change ($language) { |
||
| 310 | /** |
||
| 311 | * Check whether it is allowed to change to specified language according to configuration |
||
| 312 | * |
||
| 313 | * @param Config $Config |
||
| 314 | * @param string $language |
||
| 315 | * |
||
| 316 | * @return bool |
||
| 317 | */ |
||
| 318 | protected function can_be_changed_to ($Config, $language) { |
||
| 330 | /** |
||
| 331 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
| 332 | * |
||
| 333 | * @param $language |
||
| 334 | * |
||
| 335 | * @return string[] |
||
| 336 | */ |
||
| 337 | protected function get_translation ($language) { |
||
| 377 | /** |
||
| 378 | * @param string $filename |
||
| 379 | * |
||
| 380 | * @return string[] |
||
| 381 | */ |
||
| 382 | protected function get_translation_from_json ($filename) { |
||
| 386 | /** |
||
| 387 | * @param string[]|string[][] $translation |
||
| 388 | * |
||
| 389 | * @return string[] |
||
| 390 | */ |
||
| 391 | protected function get_translation_from_json_internal ($translation) { |
||
| 404 | /** |
||
| 405 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
| 406 | * |
||
| 407 | * @param string[] $translation |
||
| 408 | * @param string $language |
||
| 409 | * |
||
| 410 | * @return string[] |
||
| 411 | */ |
||
| 412 | protected function fill_required_translation_keys ($translation, $language) { |
||
| 429 | /** |
||
| 430 | * Time formatting according to the current language (adding correct endings) |
||
| 431 | * |
||
| 432 | * @param int $in time (number) |
||
| 433 | * @param string $type Type of formatting<br> |
||
| 434 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | function time ($in, $type) { |
||
| 460 | /** |
||
| 461 | * Allows to use formatted strings in translations |
||
| 462 | * |
||
| 463 | * @see format() |
||
| 464 | * |
||
| 465 | * @param string $item |
||
| 466 | * @param array $arguments |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | function __call ($item, $arguments) { |
||
| 473 | /** |
||
| 474 | * Allows to use formatted strings in translations |
||
| 475 | * |
||
| 476 | * @param string $item |
||
| 477 | * @param string[] $arguments |
||
| 478 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
| 479 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | function format ($item, $arguments, $language = false, $prefix = '') { |
||
| 486 | /** |
||
| 487 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
| 488 | * |
||
| 489 | * @param string|string[] $data |
||
| 490 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
| 491 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
| 492 | * |
||
| 493 | * @return string|string[] |
||
| 494 | */ |
||
| 495 | function to_locale ($data, $short_may = false) { |
||
| 550 | /** |
||
| 551 | * Implementation of JsonSerializable interface |
||
| 552 | * |
||
| 553 | * @return string[] |
||
| 554 | */ |
||
| 555 | function jsonSerialize () { |
||
| 558 | } |
||
| 559 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.