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 |
||
25 | class Language implements JsonSerializable { |
||
26 | use Singleton; |
||
27 | /** |
||
28 | * Current language |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | public $clanguage; |
||
33 | /** |
||
34 | * callable for time processing |
||
35 | * |
||
36 | * @var callable |
||
37 | */ |
||
38 | public $time; |
||
39 | /** |
||
40 | * For single initialization |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $init = false; |
||
45 | /** |
||
46 | * Local cache of translations |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $translation = []; |
||
51 | /** |
||
52 | * Cache to optimize frequent calls |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $localized_url = []; |
||
57 | /** |
||
58 | * Set basic language |
||
59 | */ |
||
60 | protected function construct () { |
||
64 | /** |
||
65 | * Initialization |
||
66 | * |
||
67 | * Is called from Config class by system. Usually there is no need to call it manually. |
||
68 | */ |
||
69 | function init () { |
||
94 | /** |
||
95 | * Does URL have language prefix |
||
96 | * |
||
97 | * @param false|string $url Relative url, `$_SERVER->request_uri` by default |
||
98 | * |
||
99 | * @return false|string If there is language prefix - language will be returned, `false` otherwise |
||
100 | */ |
||
101 | function url_language ($url = false) { |
||
114 | /** |
||
115 | * Checking Accept-Language header for languages that exists in configuration |
||
116 | * |
||
117 | * @param array $active_languages |
||
118 | * |
||
119 | * @return false|string |
||
120 | */ |
||
121 | protected function check_accept_header ($active_languages) { |
||
142 | /** |
||
143 | * Check `*-Locale` header (for instance, `X-Facebook-Locale`) that exists in configuration |
||
144 | * |
||
145 | * @param array $active_languages |
||
146 | * |
||
147 | * @return false|string |
||
148 | */ |
||
149 | protected function check_locale_header ($active_languages) { |
||
168 | /** |
||
169 | * Get languages aliases |
||
170 | * |
||
171 | * @return array|false |
||
172 | */ |
||
173 | protected function get_aliases () { |
||
186 | /** |
||
187 | * Get translation |
||
188 | * |
||
189 | * @param bool|string $item |
||
190 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
191 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | function get ($item, $language = false, $prefix = '') { |
||
212 | /** |
||
213 | * Set translation |
||
214 | * |
||
215 | * @param array|string $item Item string, or key-value array |
||
216 | * @param null|string $value |
||
217 | * |
||
218 | * @return void |
||
219 | */ |
||
220 | function set ($item, $value = null) { |
||
228 | /** |
||
229 | * Get translation |
||
230 | * |
||
231 | * @param string $item |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | function __get ($item) { |
||
238 | /** |
||
239 | * Set translation |
||
240 | * |
||
241 | * @param array|string $item |
||
242 | * @param null|string $value |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | function __set ($item, $value = null) { |
||
249 | /** |
||
250 | * Change language |
||
251 | * |
||
252 | * @param string $language |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | function change ($language) { |
||
291 | /** |
||
292 | * Check whether it is allowed to change to specified language according to configuration |
||
293 | * |
||
294 | * @param Config $Config |
||
295 | * @param string $language |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | protected function can_be_changed_to ($Config, $language) { |
||
311 | /** |
||
312 | * Load translation from all over the system, set `$this->translation[$language]` and return it |
||
313 | * |
||
314 | * @param $language |
||
315 | * |
||
316 | * @return string[] |
||
317 | */ |
||
318 | protected function get_translation ($language) { |
||
358 | /** |
||
359 | * @param string $filename |
||
360 | * |
||
361 | * @return string[] |
||
362 | */ |
||
363 | protected function get_translation_from_json ($filename) { |
||
367 | /** |
||
368 | * @param string[]|string[][] $translation |
||
369 | * |
||
370 | * @return string[] |
||
371 | */ |
||
372 | protected function get_translation_from_json_internal ($translation) { |
||
385 | /** |
||
386 | * Some required keys might be missing in translation, this functions tries to guess and fill them automatically |
||
387 | * |
||
388 | * @param string[] $translation |
||
389 | * @param string $language |
||
390 | * |
||
391 | * @return string[] |
||
392 | */ |
||
393 | protected function fill_required_translation_keys ($translation, $language) { |
||
410 | /** |
||
411 | * Time formatting according to the current language (adding correct endings) |
||
412 | * |
||
413 | * @param int $in time (number) |
||
414 | * @param string $type Type of formatting<br> |
||
415 | * s - seconds<br>m - minutes<br>h - hours<br>d - days<br>M - months<br>y - years |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | function time ($in, $type) { |
||
441 | /** |
||
442 | * Allows to use formatted strings in translations |
||
443 | * |
||
444 | * @see format() |
||
445 | * |
||
446 | * @param string $item |
||
447 | * @param array $arguments |
||
448 | * |
||
449 | * @return string |
||
450 | */ |
||
451 | function __call ($item, $arguments) { |
||
454 | /** |
||
455 | * Allows to use formatted strings in translations |
||
456 | * |
||
457 | * @param string $item |
||
458 | * @param string[] $arguments |
||
459 | * @param false|string $language If specified - translation for specified language will be returned, otherwise for current |
||
460 | * @param string $prefix Used by `\cs\Language\Prefix`, usually no need to use it directly |
||
461 | * |
||
462 | * @return string |
||
463 | */ |
||
464 | function format ($item, $arguments, $language = false, $prefix = '') { |
||
467 | /** |
||
468 | * Formatting date according to language locale (translating months names, days of week, etc.) |
||
469 | * |
||
470 | * @param string|string[] $data |
||
471 | * @param bool $short_may When in date() or similar functions "M" format option is used, third month "May" have the same short textual |
||
472 | * representation as full, so, this option allows to specify, which exactly form of representation do you want |
||
473 | * |
||
474 | * @return string|string[] |
||
475 | */ |
||
476 | function to_locale ($data, $short_may = false) { |
||
531 | /** |
||
532 | * Implementation of JsonSerializable interface |
||
533 | * |
||
534 | * @return string[] |
||
535 | */ |
||
536 | function jsonSerialize () { |
||
539 | } |
||
540 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.