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:
1 | <?php |
||
7 | trait RussianLanguage |
||
8 | { |
||
9 | public static $vowels = array( |
||
10 | 'а', |
||
11 | 'е', |
||
12 | 'ё', |
||
13 | 'и', |
||
14 | 'о', |
||
15 | 'у', |
||
16 | 'ы', |
||
17 | 'э', |
||
18 | 'ю', |
||
19 | 'я', |
||
20 | ); |
||
21 | |||
22 | public static $consonants = array( |
||
23 | 'б', |
||
24 | 'в', |
||
25 | 'г', |
||
26 | 'д', |
||
27 | 'ж', |
||
28 | 'з', |
||
29 | 'й', |
||
30 | 'к', |
||
31 | 'л', |
||
32 | 'м', |
||
33 | 'н', |
||
34 | 'п', |
||
35 | 'р', |
||
36 | 'с', |
||
37 | 'т', |
||
38 | 'ф', |
||
39 | 'х', |
||
40 | 'ц', |
||
41 | 'ч', |
||
42 | 'ш', |
||
43 | 'щ', |
||
44 | ); |
||
45 | |||
46 | public static $pairs = array( |
||
47 | 'б' => 'п', |
||
48 | 'в' => 'ф', |
||
49 | 'г' => 'к', |
||
50 | 'д' => 'т', |
||
51 | 'ж' => 'ш', |
||
52 | 'з' => 'с', |
||
53 | ); |
||
54 | |||
55 | public static $deafConsonants = ['х', 'ч', 'щ']; |
||
56 | public static $sonorousConsonants = ['л', 'м', 'н', 'р']; |
||
57 | |||
58 | public static function isVowel($char) |
||
62 | |||
63 | public static function isConsonant($char) |
||
67 | |||
68 | public static function isDeafConsonant($char) |
||
72 | |||
73 | public static function isSonorousConsonant($char) |
||
77 | |||
78 | public static function isHissingConsonant($consonant) |
||
82 | |||
83 | protected static function isVelarConsonant($consonant) |
||
87 | |||
88 | public static function countSyllables($string) |
||
92 | |||
93 | public static function isPaired($consonant) |
||
98 | |||
99 | public static function checkLastConsonantSoftness($word) |
||
110 | |||
111 | public static function choosePrepositionByFirstLetter($word, $prepositionWithVowel, $preposition) |
||
119 | |||
120 | public static function chooseVowelAfterConsonant($last, $soft_last, $after_soft, $after_hard) |
||
128 | |||
129 | /** |
||
130 | * @param string $verb Verb to modify if gender is female |
||
131 | * @param string $gender If not `m`, verb will be modified |
||
132 | * @return string Correct verb |
||
133 | */ |
||
134 | public static function verb($verb, $gender) |
||
145 | |||
146 | /** |
||
147 | * Add 'в' or 'во' prepositional before the word |
||
148 | * @param string $word |
||
149 | * @return string |
||
150 | */ |
||
151 | public static function in($word) |
||
158 | |||
159 | /** |
||
160 | * Add 'с' or 'со' prepositional before the word |
||
161 | * @param string $word |
||
162 | * @return string |
||
163 | */ |
||
164 | public static function with($word) |
||
171 | |||
172 | /** |
||
173 | * Add 'о' or 'об' or 'обо' prepositional before the word |
||
174 | * @param string $word |
||
175 | * @return string |
||
176 | */ |
||
177 | public static function about($word) |
||
188 | } |
||
189 |
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.