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) { |
||
192 | $aliases = $this->get_aliases(); |
||
193 | /** |
||
194 | * For `X-Facebook-Locale` and other similar |
||
195 | */ |
||
196 | foreach (Request::instance()->headers ?: [] as $i => $v) { |
||
197 | if (stripos($i, '-locale') !== false) { |
||
198 | $language = strtolower($v); |
||
199 | if (@in_array($aliases[$language], $active_languages)) { |
||
200 | return $aliases[$language]; |
||
201 | } |
||
202 | return false; |
||
203 | } |
||
204 | } |
||
205 | return false; |
||
206 | } |
||
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) { |
||
392 | /** |
||
393 | * @param string $filename |
||
394 | * |
||
395 | * @return string[] |
||
396 | */ |
||
397 | protected function get_translation_from_json ($filename) { |
||
401 | /** |
||
402 | * @param string[]|string[][] $translation |
||
403 | * |
||
404 | * @return string[] |
||
405 | */ |
||
406 | protected function get_translation_from_json_internal ($translation) { |
||
419 | /** |
||
420 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
421 | * |
||
422 | * @param string[] $translation |
||
423 | * @param string $language |
||
424 | * |
||
425 | * @return string[] |
||
426 | */ |
||
427 | protected function fill_required_translation_keys ($translation, $language) { |
||
444 | /** |
||
445 | * Time formatting according to the current language (adding correct endings) |
||
446 | * |
||
447 | * @param int $in time (number) |
||
448 | * @param string $type Type of formatting<br> |
||
449 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | function time ($in, $type) { |
||
475 | /** |
||
476 | * Allows to use formatted strings in translations |
||
477 | * |
||
478 | * @see format() |
||
479 | * |
||
480 | * @param string $item |
||
481 | * @param array $arguments |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | function __call ($item, $arguments) { |
||
488 | /** |
||
489 | * Allows to use formatted strings in translations |
||
490 | * |
||
491 | * @param string $item |
||
492 | * @param string[] $arguments |
||
493 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
494 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
495 | * |
||
496 | * @return string |
||
497 | */ |
||
498 | function format ($item, $arguments, $language = false, $prefix = '') { |
||
501 | /** |
||
502 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
503 | * |
||
504 | * @param string|string[] $data |
||
505 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
506 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
507 | * |
||
508 | * @return string|string[] |
||
509 | */ |
||
510 | function to_locale ($data, $short_may = false) { |
||
565 | /** |
||
566 | * Implementation of JsonSerializable interface |
||
567 | * |
||
568 | * @return string[] |
||
569 | */ |
||
570 | function jsonSerialize () { |
||
573 | } |
||
574 |