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 array Все гласные |
||
| 11 | */ |
||
| 12 | public static $vowels = [ |
||
| 13 | 'а', |
||
| 14 | 'е', |
||
| 15 | 'ё', |
||
| 16 | 'и', |
||
| 17 | 'о', |
||
| 18 | 'у', |
||
| 19 | 'ы', |
||
| 20 | 'э', |
||
| 21 | 'ю', |
||
| 22 | 'я', |
||
| 23 | ]; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array Все согласные |
||
| 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 array Пары согласных |
||
| 54 | */ |
||
| 55 | public static $pairs = [ |
||
| 56 | 'б' => 'п', |
||
| 57 | 'в' => 'ф', |
||
| 58 | 'г' => 'к', |
||
| 59 | 'д' => 'т', |
||
| 60 | 'ж' => 'ш', |
||
| 61 | 'з' => 'с', |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array Звонкие согласные |
||
| 66 | */ |
||
| 67 | public static $sonorousConsonants = ['б', 'в', 'г', 'д', 'з', 'ж', 'л', 'м', 'н', 'р']; |
||
| 68 | /** |
||
| 69 | * @var array Глухие согласные |
||
| 70 | */ |
||
| 71 | public static $deafConsonants = ['п', 'ф', 'к', 'т', 'с', 'ш', 'х', 'ч', 'щ']; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Проверка гласной |
||
| 75 | */ |
||
| 76 | 342 | public static function isVowel($char) |
|
| 77 | { |
||
| 78 | 342 | return in_array($char, self::$vowels); |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Проверка согласной |
||
| 83 | */ |
||
| 84 | 188 | public static function isConsonant($char) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Проверка звонкости согласной |
||
| 91 | */ |
||
| 92 | 2 | public static function isSonorousConsonant($char) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Проверка глухости согласной |
||
| 99 | */ |
||
| 100 | 6 | public static function isDeafConsonant($char) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Щипящая ли согласная |
||
| 107 | */ |
||
| 108 | 445 | public static function isHissingConsonant($consonant) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * |
||
| 115 | */ |
||
| 116 | 12 | protected static function isVelarConsonant($consonant) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Подсчет слогов |
||
| 123 | */ |
||
| 124 | public static function countSyllables($string) |
||
| 125 | { |
||
| 126 | return S::countChars($string, self::$vowels); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Проверка парности согласной |
||
| 131 | */ |
||
| 132 | public static function isPaired($consonant) |
||
| 133 | { |
||
| 134 | $consonant = S::lower($consonant); |
||
| 135 | return array_key_exists($consonant, self::$pairs) || (array_search($consonant, self::$pairs) !== false); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Проверка мягкости последней согласной |
||
| 140 | */ |
||
| 141 | 43 | public static function checkLastConsonantSoftness($word) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Проверяет, что гласная образует два звука в словах |
||
| 155 | * @param $vowel |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | 2 | public static function isBinaryVowel($vowel) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Выбор предлога по первой букве |
||
| 165 | */ |
||
| 166 | public static function choosePrepositionByFirstLetter($word, $prepositionWithVowel, $preposition) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Выбор окончания в зависимости от мягкости |
||
| 177 | */ |
||
| 178 | 116 | public static function chooseVowelAfterConsonant($last, $soft_last, $after_soft, $after_hard) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $verb Verb to modify if gender is female |
||
| 189 | * @param string $gender If not `m`, verb will be modified |
||
| 190 | * @return string Correct verb |
||
| 191 | */ |
||
| 192 | 8 | public static function verb($verb, $gender) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Add 'в' or 'во' prepositional before the word |
||
| 206 | * @param string $word |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | 1 | public static function in($word) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Add 'с' or 'со' prepositional before the word |
||
| 219 | * @param string $word |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | 1 | public static function with($word) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Add 'о' or 'об' or 'обо' prepositional before the word |
||
| 232 | * @param string $word |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | 1 | public static function about($word) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Выбирает первое или второе окончание в зависимости от звонкости/глухости в конце слова. |
||
| 249 | * @param string $word Слово (или префикс), на основе звонкости которого нужно выбрать окончание |
||
| 250 | * @param string $ifSonorous Окончание, если слово оканчивается на звонкую согласную |
||
| 251 | * @param string $ifDeaf Окончание, если слово оканчивается на глухую согласную |
||
| 252 | * @return string Первое или второе окончание |
||
| 253 | * @throws \Exception |
||
| 254 | */ |
||
| 255 | 2 | public static function chooseEndingBySonority($word, $ifSonorous, $ifDeaf) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Проверяет, является ли существительно адъективным существительным |
||
| 268 | * @param string $noun Существительное |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | 142 | public static function isAdjectiveNoun($noun) |
|
| 276 | } |
||
| 277 |
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.