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 |
||
39 | Singleton; |
||
40 | const INIT_STATE_METHOD = 'init'; |
||
41 | /** |
||
42 | * Callable for time processing |
||
43 | * |
||
44 | * @var callable |
||
45 | */ |
||
46 | public $time; |
||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $current_language; |
||
51 | /** |
||
52 | * Local cache of translations |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $translation = []; |
||
57 | /** |
||
58 | * Cache to optimize frequent calls |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $localized_url = []; |
||
63 | 84 | protected function init () { |
|
104 | /** |
||
105 | * Returns instance for simplified work with translations, when using common prefix |
||
106 | * |
||
107 | * @param string $prefix |
||
108 | * |
||
109 | * @return Prefix |
||
110 | */ |
||
111 | 32 | public static function prefix ($prefix) { |
|
114 | /** |
||
115 | * Does URL have language prefix |
||
116 | * |
||
117 | * @param false|string $url Relative url, `Request::instance()->path` by default |
||
118 | * |
||
119 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
120 | */ |
||
121 | 52 | public function url_language ($url = false) { |
|
140 | /** |
||
141 | * Checking Accept-Language header for languages that exists in configuration |
||
142 | * |
||
143 | * @param array $active_languages |
||
144 | * |
||
145 | * @return false|string |
||
146 | */ |
||
147 | 8 | protected function check_accept_header ($active_languages) { |
|
165 | /** |
||
166 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
167 | * |
||
168 | * @param string[] $active_languages |
||
169 | * |
||
170 | * @return false|string |
||
171 | */ |
||
172 | 12 | protected function check_locale_header ($active_languages) { |
|
188 | /** |
||
189 | * Get languages aliases |
||
190 | * |
||
191 | * @return array|false |
||
192 | */ |
||
193 | 52 | protected function get_aliases () { |
|
209 | /** |
||
210 | * Get translation |
||
211 | * |
||
212 | * @param bool|string $item |
||
213 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
214 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | 82 | public function get ($item, $language = false, $prefix = '') { |
|
241 | /** |
||
242 | * Set translation |
||
243 | * |
||
244 | * @param array|string $item Item string, or key-value array |
||
245 | * @param null|string $value |
||
246 | * |
||
247 | * @return void |
||
248 | */ |
||
249 | public function set ($item, $value = null) { |
||
257 | /** |
||
258 | * Get translation |
||
259 | * |
||
260 | * @param string $item |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | 82 | public function __get ($item) { |
|
267 | /** |
||
268 | * Set translation |
||
269 | * |
||
270 | * @param array|string $item |
||
271 | * @param null|string $value |
||
272 | */ |
||
273 | public function __set ($item, $value = null) { |
||
276 | /** |
||
277 | * Change language |
||
278 | * |
||
279 | * @param string $language |
||
280 | * |
||
281 | * @return bool |
||
282 | */ |
||
283 | 66 | public function change ($language) { |
|
315 | /** |
||
316 | * Check whether it is allowed to change to specified language according to configuration |
||
317 | * |
||
318 | * @param Config $Config |
||
319 | * @param string $language |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | 66 | protected function can_be_changed_to ($Config, $language) { |
|
346 | /** |
||
347 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
348 | * |
||
349 | * @param $language |
||
350 | * |
||
351 | * @return string[] |
||
352 | */ |
||
353 | 12 | protected function get_translation_internal ($language) { |
|
384 | /** |
||
385 | * @param string $filename |
||
386 | * |
||
387 | * @return string[] |
||
388 | */ |
||
389 | 12 | protected function get_translation_from_json ($filename) { |
|
394 | /** |
||
395 | * @param string[]|string[][] $translation |
||
396 | * |
||
397 | * @return string[] |
||
398 | */ |
||
399 | 12 | protected function get_translation_from_json_internal ($translation) { |
|
412 | /** |
||
413 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
414 | * |
||
415 | * @param string[] $translation |
||
416 | * @param string $language |
||
417 | * |
||
418 | * @return string[] |
||
419 | */ |
||
420 | 12 | protected function fill_required_translation_keys ($translation, $language) { |
|
432 | /** |
||
433 | * Time formatting according to the current language (adding correct endings) |
||
434 | * |
||
435 | * @param int $in time (number) |
||
436 | * @param string $type Type of formatting<br> |
||
437 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | 10 | public function time ($in, $type) { |
|
463 | /** |
||
464 | * Allows to use formatted strings in translations |
||
465 | * |
||
466 | * @see format() |
||
467 | * |
||
468 | * @param string $item |
||
469 | * @param array $arguments |
||
470 | * |
||
471 | * @return string |
||
472 | */ |
||
473 | 2 | public function __call ($item, $arguments) { |
|
476 | /** |
||
477 | * Allows to use formatted strings in translations |
||
478 | * |
||
479 | * @param string $item |
||
480 | * @param string[] $arguments |
||
481 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
482 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
483 | * |
||
484 | * @return string |
||
485 | */ |
||
486 | 12 | public function format ($item, $arguments, $language = false, $prefix = '') { |
|
489 | /** |
||
490 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
491 | * |
||
492 | * @param string|string[] $data |
||
493 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
494 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
495 | * |
||
496 | * @return string|string[] |
||
497 | */ |
||
498 | 2 | public function to_locale ($data, $short_may = false) { |
|
553 | /** |
||
554 | * Implementation of JsonSerializable interface |
||
555 | * |
||
556 | * @return string[] |
||
557 | */ |
||
558 | 4 | public function jsonSerialize () { |
|
563 | } |
||
564 |