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 |
||
33 | class Language implements JsonSerializable { |
||
34 | use Singleton; |
||
35 | /** |
||
36 | * Current language |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | public $clanguage; |
||
41 | /** |
||
42 | * callable for time processing |
||
43 | * |
||
44 | * @var callable |
||
45 | */ |
||
46 | public $time; |
||
47 | /** |
||
48 | * For single initialization |
||
49 | * |
||
50 | * @var bool |
||
51 | */ |
||
52 | protected $init = false; |
||
53 | /** |
||
54 | * Local cache of translations |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $translation = []; |
||
59 | /** |
||
60 | * Cache to optimize frequent calls |
||
61 | * |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $localized_url = []; |
||
65 | /** |
||
66 | * Set basic language |
||
67 | */ |
||
68 | protected function construct () { |
||
97 | /** |
||
98 | * Initialization: set default language based on system configuration and request-specific parameters |
||
99 | */ |
||
100 | protected function init () { |
||
122 | /** |
||
123 | * Does URL have language prefix |
||
124 | * |
||
125 | * @param false|string $url Relative url, `Request::instance()->path` by default |
||
126 | * |
||
127 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
128 | */ |
||
129 | function url_language ($url = false) { |
||
148 | /** |
||
149 | * Checking Accept-Language header for languages that exists in configuration |
||
150 | * |
||
151 | * @param array $active_languages |
||
152 | * |
||
153 | * @return false|string |
||
154 | */ |
||
155 | protected function check_accept_header ($active_languages) { |
||
173 | /** |
||
174 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
175 | * |
||
176 | * @param string[] $active_languages |
||
177 | * |
||
178 | * @return false|string |
||
179 | */ |
||
180 | protected function check_locale_header ($active_languages) { |
||
196 | /** |
||
197 | * Get languages aliases |
||
198 | * |
||
199 | * @return array|false |
||
200 | */ |
||
201 | protected function get_aliases () { |
||
214 | /** |
||
215 | * Get translation |
||
216 | * |
||
217 | * @param bool|string $item |
||
218 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
219 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
220 | * |
||
221 | * @return string |
||
222 | */ |
||
223 | function get ($item, $language = false, $prefix = '') { |
||
240 | /** |
||
241 | * Set translation |
||
242 | * |
||
243 | * @param array|string $item Item string, or key-value array |
||
244 | * @param null|string $value |
||
245 | * |
||
246 | * @return void |
||
247 | */ |
||
248 | function set ($item, $value = null) { |
||
256 | /** |
||
257 | * Get translation |
||
258 | * |
||
259 | * @param string $item |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | function __get ($item) { |
||
266 | /** |
||
267 | * Set translation |
||
268 | * |
||
269 | * @param array|string $item |
||
270 | * @param null|string $value |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | function __set ($item, $value = null) { |
||
277 | /** |
||
278 | * Change language |
||
279 | * |
||
280 | * @param string $language |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | function change ($language) { |
||
322 | /** |
||
323 | * Check whether it is allowed to change to specified language according to configuration |
||
324 | * |
||
325 | * @param Config $Config |
||
326 | * @param string $language |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | protected function can_be_changed_to ($Config, $language) { |
||
342 | /** |
||
343 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
344 | * |
||
345 | * @param $language |
||
346 | * |
||
347 | * @return string[] |
||
348 | */ |
||
349 | protected function get_translation ($language) { |
||
389 | /** |
||
390 | * @param string $filename |
||
391 | * |
||
392 | * @return string[] |
||
393 | */ |
||
394 | protected function get_translation_from_json ($filename) { |
||
398 | /** |
||
399 | * @param string[]|string[][] $translation |
||
400 | * |
||
401 | * @return string[] |
||
402 | */ |
||
403 | protected function get_translation_from_json_internal ($translation) { |
||
416 | /** |
||
417 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
418 | * |
||
419 | * @param string[] $translation |
||
420 | * @param string $language |
||
421 | * |
||
422 | * @return string[] |
||
423 | */ |
||
424 | protected function fill_required_translation_keys ($translation, $language) { |
||
441 | /** |
||
442 | * Time formatting according to the current language (adding correct endings) |
||
443 | * |
||
444 | * @param int $in time (number) |
||
445 | * @param string $type Type of formatting<br> |
||
446 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
447 | * |
||
448 | * @return string |
||
449 | */ |
||
450 | function time ($in, $type) { |
||
472 | /** |
||
473 | * Allows to use formatted strings in translations |
||
474 | * |
||
475 | * @see format() |
||
476 | * |
||
477 | * @param string $item |
||
478 | * @param array $arguments |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | function __call ($item, $arguments) { |
||
485 | /** |
||
486 | * Allows to use formatted strings in translations |
||
487 | * |
||
488 | * @param string $item |
||
489 | * @param string[] $arguments |
||
490 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
491 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | function format ($item, $arguments, $language = false, $prefix = '') { |
||
498 | /** |
||
499 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
500 | * |
||
501 | * @param string|string[] $data |
||
502 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
503 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
504 | * |
||
505 | * @return string|string[] |
||
506 | */ |
||
507 | function to_locale ($data, $short_may = false) { |
||
562 | /** |
||
563 | * Implementation of JsonSerializable interface |
||
564 | * |
||
565 | * @return string[] |
||
566 | */ |
||
567 | function jsonSerialize () { |
||
570 | } |
||
571 |