Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like RussianLanguage 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 RussianLanguage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | trait RussianLanguage |
||
8 | { |
||
9 | /** |
||
10 | * @var string[] Все гласные |
||
11 | */ |
||
12 | public static $vowels = [ |
||
13 | 'а', |
||
14 | 'е', |
||
15 | 'ё', |
||
16 | 'и', |
||
17 | 'о', |
||
18 | 'у', |
||
19 | 'ы', |
||
20 | 'э', |
||
21 | 'ю', |
||
22 | 'я', |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * @var string[] Все согласные |
||
27 | */ |
||
28 | public static $consonants = [ |
||
29 | 'б', |
||
30 | 'в', |
||
31 | 'г', |
||
32 | 'д', |
||
33 | 'ж', |
||
34 | 'з', |
||
35 | 'й', |
||
36 | 'к', |
||
37 | 'л', |
||
38 | 'м', |
||
39 | 'н', |
||
40 | 'п', |
||
41 | 'р', |
||
42 | 'с', |
||
43 | 'т', |
||
44 | 'ф', |
||
45 | 'х', |
||
46 | 'ц', |
||
47 | 'ч', |
||
48 | 'ш', |
||
49 | 'щ', |
||
50 | ]; |
||
51 | |||
52 | /** |
||
53 | * @var string[] Пары согласных |
||
54 | * @phpstan-var array<string, string> |
||
55 | */ |
||
56 | public static $pairs = [ |
||
57 | 'б' => 'п', |
||
58 | 'в' => 'ф', |
||
59 | 'г' => 'к', |
||
60 | 'д' => 'т', |
||
61 | 'ж' => 'ш', |
||
62 | 'з' => 'с', |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * @var string[] Звонкие согласные |
||
67 | */ |
||
68 | public static $sonorousConsonants = ['б', 'в', 'г', 'д', 'з', 'ж', 'л', 'м', 'н', 'р']; |
||
69 | /** |
||
70 | * @var string[] Глухие согласные |
||
71 | */ |
||
72 | public static $deafConsonants = ['п', 'ф', 'к', 'т', 'с', 'ш', 'х', 'ч', 'щ']; |
||
73 | |||
74 | /** |
||
75 | * @var string[] Союзы |
||
76 | */ |
||
77 | public static $unions = ['и', 'или']; |
||
78 | |||
79 | /** |
||
80 | * @return string[] |
||
81 | */ |
||
82 | public static function getVowels() |
||
86 | |||
87 | /** |
||
88 | * Проверка гласной |
||
89 | * @param string $char |
||
90 | * @return bool |
||
91 | */ |
||
92 | 354 | public static function isVowel($char) |
|
96 | |||
97 | /** |
||
98 | * Проверка согласной |
||
99 | * @param string $char |
||
100 | * @return bool |
||
101 | */ |
||
102 | 240 | public static function isConsonant($char) |
|
106 | |||
107 | /** |
||
108 | * Проверка звонкости согласной |
||
109 | * @param string $char |
||
110 | * @return bool |
||
111 | */ |
||
112 | 3 | public static function isSonorousConsonant($char) |
|
116 | |||
117 | /** |
||
118 | * Проверка глухости согласной |
||
119 | * @param string $char |
||
120 | * @return bool |
||
121 | */ |
||
122 | 8 | public static function isDeafConsonant($char) |
|
126 | |||
127 | /** |
||
128 | * Щипящая ли согласная |
||
129 | * @param string $consonant |
||
130 | * @return bool |
||
131 | */ |
||
132 | 454 | public static function isHissingConsonant($consonant) |
|
136 | |||
137 | /** |
||
138 | * Проверка на велярность согласной |
||
139 | * @param string $consonant |
||
140 | * @return bool |
||
141 | */ |
||
142 | 19 | protected static function isVelarConsonant($consonant) |
|
146 | |||
147 | /** |
||
148 | * Подсчет слогов |
||
149 | * @param string $string |
||
150 | * @return bool|int |
||
151 | */ |
||
152 | public static function countSyllables($string) |
||
156 | |||
157 | /** |
||
158 | * Проверка парности согласной |
||
159 | * |
||
160 | * @param string $consonant |
||
161 | * @return bool |
||
162 | */ |
||
163 | public static function isPairedConsonant($consonant) |
||
168 | |||
169 | /** |
||
170 | * Проверка мягкости последней согласной |
||
171 | * @param string $word |
||
172 | * @return bool |
||
173 | */ |
||
174 | 54 | public static function checkLastConsonantSoftness($word) |
|
187 | |||
188 | /** |
||
189 | * Проверка мягкости последней согласной, за исключением Н |
||
190 | * @param string $word |
||
191 | * @return bool |
||
192 | */ |
||
193 | 13 | public static function checkBaseLastConsonantSoftness($word) |
|
210 | |||
211 | /** |
||
212 | * Проверяет, что гласная образует два звука в словах |
||
213 | * @param string $vowel |
||
214 | * @return bool |
||
215 | */ |
||
216 | 2 | public static function isBinaryVowel($vowel) |
|
220 | |||
221 | /** |
||
222 | * Выбор предлога по первой букве |
||
223 | * |
||
224 | * @param string $word Слово |
||
225 | * @param string $prepositionWithVowel Предлог, если слово начинается с гласной |
||
226 | * @param string $preposition Предлог, если слово не начинается с гласной |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | public static function choosePrepositionByFirstLetter($word, $prepositionWithVowel, $preposition) |
||
238 | |||
239 | /** |
||
240 | * Выбор окончания в зависимости от мягкости |
||
241 | * |
||
242 | * @param string $last |
||
243 | * @param bool $softLast |
||
244 | * @param string $afterSoft |
||
245 | * @param string $afterHard |
||
246 | * |
||
247 | * @return string |
||
248 | */ |
||
249 | 151 | public static function chooseVowelAfterConsonant($last, $softLast, $afterSoft, $afterHard) |
|
257 | |||
258 | /** |
||
259 | * @param string $verb Verb to modify if gender is female |
||
260 | * @param string $gender If not `m`, verb will be modified |
||
261 | * @return string Correct verb |
||
262 | */ |
||
263 | 10 | public static function verb($verb, $gender) |
|
279 | |||
280 | /** |
||
281 | * Add 'в' or 'во' prepositional before the word |
||
282 | * @param string $word |
||
283 | * @return string |
||
284 | */ |
||
285 | 1 | public static function in($word) |
|
292 | |||
293 | /** |
||
294 | * Add 'с' or 'со' prepositional before the word |
||
295 | * @param string $word |
||
296 | * @return string |
||
297 | */ |
||
298 | 1 | public static function with($word) |
|
305 | |||
306 | /** |
||
307 | * Add 'о' or 'об' or 'обо' prepositional before the word |
||
308 | * @param string $word |
||
309 | * @return string |
||
310 | */ |
||
311 | 1 | public static function about($word) |
|
322 | |||
323 | /** |
||
324 | * Выбирает первое или второе окончание в зависимости от звонкости/глухости в конце слова. |
||
325 | * @param string $word Слово (или префикс), на основе звонкости которого нужно выбрать окончание |
||
326 | * @param string $ifSonorous Окончание, если слово оканчивается на звонкую согласную |
||
327 | * @param string $ifDeaf Окончание, если слово оканчивается на глухую согласную |
||
328 | * @return string Первое или второе окончание |
||
329 | * @throws \Exception |
||
330 | */ |
||
331 | 3 | public static function chooseEndingBySonority($word, $ifSonorous, $ifDeaf) |
|
341 | |||
342 | /** |
||
343 | * Проверяет, является ли существительно адъективным существительным |
||
344 | * @param string $noun Существительное |
||
345 | * @return bool |
||
346 | */ |
||
347 | 178 | public static function isAdjectiveNoun($noun) |
|
352 | |||
353 | /** |
||
354 | * @param string[] $forms |
||
355 | * @phpstan-param array<string, string> $forms |
||
356 | * @param bool $animate |
||
357 | * @return string |
||
358 | */ |
||
359 | 144 | public static function getVinitCaseByAnimateness(array $forms, $animate) |
|
367 | } |
||
368 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.