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 | * Callable for time processing |
||
37 | * |
||
38 | * @var callable |
||
39 | */ |
||
40 | public $time; |
||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $current_language; |
||
45 | /** |
||
46 | * For single initialization |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $init = false; |
||
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 | /** |
||
64 | * Set basic language |
||
65 | */ |
||
66 | protected function construct () { |
||
95 | /** |
||
96 | * Initialization: set default language based on system configuration and request-specific parameters |
||
97 | */ |
||
98 | protected function init () { |
||
120 | /** |
||
121 | * Returns instance for simplified work with translations, when using common prefix |
||
122 | * |
||
123 | * @param string $prefix |
||
124 | * |
||
125 | * @return Prefix |
||
126 | */ |
||
127 | static function prefix ($prefix) { |
||
130 | /** |
||
131 | * Does URL have language prefix |
||
132 | * |
||
133 | * @param false|string $url Relative url, `Request::instance()->path` by default |
||
134 | * |
||
135 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
136 | */ |
||
137 | 32 | function url_language ($url = false) { |
|
156 | /** |
||
157 | * Checking Accept-Language header for languages that exists in configuration |
||
158 | * |
||
159 | * @param array $active_languages |
||
160 | * |
||
161 | * @return false|string |
||
162 | */ |
||
163 | protected function check_accept_header ($active_languages) { |
||
181 | /** |
||
182 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
183 | * |
||
184 | * @param string[] $active_languages |
||
185 | * |
||
186 | * @return false|string |
||
187 | */ |
||
188 | protected function check_locale_header ($active_languages) { |
||
204 | /** |
||
205 | * Get languages aliases |
||
206 | * |
||
207 | * @return array|false |
||
208 | */ |
||
209 | protected function get_aliases () { |
||
222 | /** |
||
223 | * Get translation |
||
224 | * |
||
225 | * @param bool|string $item |
||
226 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
227 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | function get ($item, $language = false, $prefix = '') { |
||
248 | /** |
||
249 | * Set translation |
||
250 | * |
||
251 | * @param array|string $item Item string, or key-value array |
||
252 | * @param null|string $value |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | function set ($item, $value = null) { |
||
264 | /** |
||
265 | * Get translation |
||
266 | * |
||
267 | * @param string $item |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | function __get ($item) { |
||
274 | /** |
||
275 | * Set translation |
||
276 | * |
||
277 | * @param array|string $item |
||
278 | * @param null|string $value |
||
279 | * |
||
280 | * @return string |
||
281 | */ |
||
282 | function __set ($item, $value = null) { |
||
285 | /** |
||
286 | * Will set language, but not actually change to it until requested for the first time |
||
287 | * |
||
288 | * @param string $language |
||
289 | */ |
||
290 | protected function set_language ($language) { |
||
293 | /** |
||
294 | * Change language |
||
295 | * |
||
296 | * @param string $language |
||
297 | * |
||
298 | * @return bool |
||
299 | */ |
||
300 | function change ($language) { |
||
338 | /** |
||
339 | * Check whether it is allowed to change to specified language according to configuration |
||
340 | * |
||
341 | * @param Config $Config |
||
342 | * @param string $language |
||
343 | * |
||
344 | * @return bool |
||
345 | */ |
||
346 | protected function can_be_changed_to ($Config, $language) { |
||
358 | /** |
||
359 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
360 | * |
||
361 | * @param $language |
||
362 | * |
||
363 | * @return string[] |
||
1 ignored issue
–
show
|
|||
364 | */ |
||
365 | protected function get_translation ($language) { |
||
396 | /** |
||
397 | * @param string $filename |
||
398 | * |
||
399 | * @return string[] |
||
400 | */ |
||
401 | protected function get_translation_from_json ($filename) { |
||
405 | /** |
||
406 | * @param string[]|string[][] $translation |
||
407 | * |
||
408 | * @return string[] |
||
409 | */ |
||
410 | protected function get_translation_from_json_internal ($translation) { |
||
423 | /** |
||
424 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
425 | * |
||
426 | * @param string[] $translation |
||
427 | * @param string $language |
||
428 | * |
||
429 | * @return string[] |
||
1 ignored issue
–
show
|
|||
430 | */ |
||
431 | protected function fill_required_translation_keys ($translation, $language) { |
||
443 | /** |
||
444 | * Time formatting according to the current language (adding correct endings) |
||
445 | * |
||
446 | * @param int $in time (number) |
||
447 | * @param string $type Type of formatting<br> |
||
448 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
449 | * |
||
450 | * @return string |
||
451 | */ |
||
452 | function time ($in, $type) { |
||
474 | /** |
||
475 | * Allows to use formatted strings in translations |
||
476 | * |
||
477 | * @see format() |
||
478 | * |
||
479 | * @param string $item |
||
480 | * @param array $arguments |
||
481 | * |
||
482 | * @return string |
||
483 | */ |
||
484 | function __call ($item, $arguments) { |
||
487 | /** |
||
488 | * Allows to use formatted strings in translations |
||
489 | * |
||
490 | * @param string $item |
||
491 | * @param string[] $arguments |
||
492 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
493 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
494 | * |
||
495 | * @return string |
||
496 | */ |
||
497 | function format ($item, $arguments, $language = false, $prefix = '') { |
||
500 | /** |
||
501 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
502 | * |
||
503 | * @param string|string[] $data |
||
504 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
505 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
506 | * |
||
507 | * @return string|string[] |
||
508 | */ |
||
509 | function to_locale ($data, $short_may = false) { |
||
564 | /** |
||
565 | * Implementation of JsonSerializable interface |
||
566 | * |
||
567 | * @return string[] |
||
568 | */ |
||
569 | function jsonSerialize () { |
||
572 | } |
||
573 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.