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 Singleton; |
||
39 | /** |
||
40 | * Callable for time processing |
||
41 | * |
||
42 | * @var callable |
||
43 | */ |
||
44 | public $time; |
||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $current_language; |
||
49 | /** |
||
50 | * For single initialization |
||
51 | * |
||
52 | * @var bool |
||
53 | */ |
||
54 | protected $init = false; |
||
55 | /** |
||
56 | * Local cache of translations |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $translation = []; |
||
61 | /** |
||
62 | * Cache to optimize frequent calls |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $localized_url = []; |
||
67 | /** |
||
68 | * Set basic language |
||
69 | */ |
||
70 | 64 | protected function construct () { |
|
99 | /** |
||
100 | * Initialization: set default language based on system configuration and request-specific parameters |
||
101 | */ |
||
102 | 62 | protected function init () { |
|
124 | /** |
||
125 | * Returns instance for simplified work with translations, when using common prefix |
||
126 | * |
||
127 | * @param string $prefix |
||
128 | * |
||
129 | * @return Prefix |
||
130 | */ |
||
131 | 22 | public static function prefix ($prefix) { |
|
134 | /** |
||
135 | * Does URL have language prefix |
||
136 | * |
||
137 | * @param false|string $url Relative url, `Request::instance()->path` by default |
||
138 | * |
||
139 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
140 | */ |
||
141 | 34 | public function url_language ($url = false) { |
|
160 | /** |
||
161 | * Checking Accept-Language header for languages that exists in configuration |
||
162 | * |
||
163 | * @param array $active_languages |
||
164 | * |
||
165 | * @return false|string |
||
166 | */ |
||
167 | 4 | protected function check_accept_header ($active_languages) { |
|
185 | /** |
||
186 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
187 | * |
||
188 | * @param string[] $active_languages |
||
189 | * |
||
190 | * @return false|string |
||
191 | */ |
||
192 | 8 | protected function check_locale_header ($active_languages) { |
|
208 | /** |
||
209 | * Get languages aliases |
||
210 | * |
||
211 | * @return array|false |
||
212 | */ |
||
213 | 34 | protected function get_aliases () { |
|
226 | /** |
||
227 | * Get translation |
||
228 | * |
||
229 | * @param bool|string $item |
||
230 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
231 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | 64 | public function get ($item, $language = false, $prefix = '') { |
|
258 | /** |
||
259 | * Set translation |
||
260 | * |
||
261 | * @param array|string $item Item string, or key-value array |
||
262 | * @param null|string $value |
||
263 | * |
||
264 | * @return void |
||
265 | */ |
||
266 | public function set ($item, $value = null) { |
||
274 | /** |
||
275 | * Get translation |
||
276 | * |
||
277 | * @param string $item |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | 64 | public function __get ($item) { |
|
284 | /** |
||
285 | * Set translation |
||
286 | * |
||
287 | * @param array|string $item |
||
288 | * @param null|string $value |
||
289 | * |
||
290 | * @return string |
||
291 | */ |
||
292 | public function __set ($item, $value = null) { |
||
295 | /** |
||
296 | * Change language |
||
297 | * |
||
298 | * @param string $language |
||
299 | * |
||
300 | * @return bool |
||
301 | */ |
||
302 | 54 | public 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 | 54 | protected function can_be_changed_to ($Config, $language) { |
|
364 | /** |
||
365 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
366 | * |
||
367 | * @param $language |
||
368 | * |
||
369 | * @return string[] |
||
370 | */ |
||
371 | 10 | protected function get_translation_internal ($language) { |
|
411 | /** |
||
412 | * @param string $filename |
||
413 | * |
||
414 | * @return string[] |
||
415 | */ |
||
416 | 10 | protected function get_translation_from_json ($filename) { |
|
421 | /** |
||
422 | * @param string[]|string[][] $translation |
||
423 | * |
||
424 | * @return string[] |
||
425 | */ |
||
426 | 10 | protected function get_translation_from_json_internal ($translation) { |
|
439 | /** |
||
440 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
441 | * |
||
442 | * @param string[] $translation |
||
443 | * @param string $language |
||
444 | * |
||
445 | * @return string[] |
||
446 | */ |
||
447 | 10 | protected function fill_required_translation_keys ($translation, $language) { |
|
459 | /** |
||
460 | * Time formatting according to the current language (adding correct endings) |
||
461 | * |
||
462 | * @param int $in time (number) |
||
463 | * @param string $type Type of formatting<br> |
||
464 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | 4 | public function time ($in, $type) { |
|
490 | /** |
||
491 | * Allows to use formatted strings in translations |
||
492 | * |
||
493 | * @see format() |
||
494 | * |
||
495 | * @param string $item |
||
496 | * @param array $arguments |
||
497 | * |
||
498 | * @return string |
||
499 | */ |
||
500 | 2 | public function __call ($item, $arguments) { |
|
503 | /** |
||
504 | * Allows to use formatted strings in translations |
||
505 | * |
||
506 | * @param string $item |
||
507 | * @param string[] $arguments |
||
508 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
509 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
510 | * |
||
511 | * @return string |
||
512 | */ |
||
513 | 6 | public function format ($item, $arguments, $language = false, $prefix = '') { |
|
516 | /** |
||
517 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
518 | * |
||
519 | * @param string|string[] $data |
||
520 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
521 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
522 | * |
||
523 | * @return string|string[] |
||
524 | */ |
||
525 | 2 | public function to_locale ($data, $short_may = false) { |
|
580 | /** |
||
581 | * Implementation of JsonSerializable interface |
||
582 | * |
||
583 | * @return string[] |
||
584 | */ |
||
585 | 4 | public function jsonSerialize () { |
|
588 | } |
||
589 |