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 |
||
12 | class Language |
||
|
|||
13 | { |
||
14 | /** |
||
15 | * Language files format. |
||
16 | */ |
||
17 | const FORMAT = 'json'; |
||
18 | |||
19 | /** |
||
20 | * Contains module language translations. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected static $languageContainer = []; |
||
25 | |||
26 | /** @var string Current language. */ |
||
27 | private static $language = ''; |
||
28 | |||
29 | /** |
||
30 | * Functions that gets translated string. |
||
31 | * |
||
32 | * @param string $key - string which need to be translated |
||
33 | * @param string $module - module scope in which the translation need to be check |
||
34 | * @param string $currentLanguage |
||
35 | * |
||
36 | * @return string - translated string |
||
37 | */ |
||
38 | public static function translate(string $key, string $module = 'Basic', string $currentLanguage = ''): string |
||
62 | |||
63 | /** |
||
64 | * Functions that gets translated string by $args. |
||
65 | * |
||
66 | * @param string $key - string which need to be translated |
||
67 | * @param string $moduleName - module scope in which the translation need to be check |
||
68 | * |
||
69 | * @return string - translated string |
||
70 | */ |
||
71 | public static function translateArgs(string $key, string $moduleName = 'Basic'): string |
||
80 | |||
81 | /** |
||
82 | * Function that returns current language. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | public static function getLanguage(): string |
||
110 | |||
111 | /** |
||
112 | * Function returns language specific translated string. |
||
113 | * |
||
114 | * @param string $language - en_us etc |
||
115 | * @param string $key - label |
||
116 | * @param string $module - module name |
||
117 | * |
||
118 | * @return string translated string or null if translation not found |
||
119 | */ |
||
120 | public static function getLanguageTranslatedString(string $language, string $key, string $module = 'Basic') |
||
133 | |||
134 | public static function getModuleStringsFromFile(string $language, string $module = 'Basic') |
||
144 | |||
145 | /** |
||
146 | * Load language file from JSON. |
||
147 | * |
||
148 | * @param string $language |
||
149 | * @param string $moduleName |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | public static function loadLanguageFile(string $language, string $moduleName = 'Basic') |
||
167 | |||
168 | /** |
||
169 | * Functions that gets translated string for Client side. |
||
170 | * |
||
171 | * @param <String> $key - string which need to be translated |
||
172 | * @param <String> $module - module scope in which the translation need to be check |
||
173 | * @param mixed $language |
||
174 | * |
||
175 | * @return <String> - translated string |
||
176 | */ |
||
177 | public static function jstranslate($language, $key, $module = 'Basic') |
||
190 | |||
191 | /** |
||
192 | * Function to returns all language information. |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | public static function getAllLanguages(): array |
||
206 | |||
207 | /** |
||
208 | * Function that returns current language short name. |
||
209 | * |
||
210 | * @return <String> - |
||
211 | */ |
||
212 | public static function getShortLanguageName() |
||
217 | |||
218 | /** |
||
219 | * Function returns module strings. |
||
220 | * |
||
221 | * @param <String> $module - module Name |
||
222 | * @param <String> languageStrings or jsLanguageStrings |
||
223 | * @param mixed $type |
||
224 | * |
||
225 | * @return <Array> |
||
226 | */ |
||
227 | public static function export($module, $type = 'php') |
||
243 | |||
244 | public static function translateModule($module) |
||
252 | |||
253 | /** |
||
254 | * Get display language name. |
||
255 | * |
||
256 | * @param string $prefix |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | public static function getDisplayName(string $prefix): string |
||
264 | } |
||
265 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.