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 | * @var array Союзы |
||
| 75 | */ |
||
| 76 | public static $unions = ['и', 'или']; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return array |
||
| 80 | */ |
||
| 81 | public static function getVowels() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Проверка гласной |
||
| 88 | * @param $char |
||
| 89 | * @return bool |
||
| 90 | */ |
||
| 91 | 348 | public static function isVowel($char) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Проверка согласной |
||
| 98 | * @param $char |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | 208 | public static function isConsonant($char) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Проверка звонкости согласной |
||
| 108 | */ |
||
| 109 | 3 | public static function isSonorousConsonant($char) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Проверка глухости согласной |
||
| 116 | * @param $char |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | 6 | public static function isDeafConsonant($char) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Щипящая ли согласная |
||
| 126 | * @param $consonant |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | 461 | public static function isHissingConsonant($consonant) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Проверка на велярность согласной |
||
| 136 | * @param string[1] $consonant |
||
|
|
|||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | 15 | protected static function isVelarConsonant($consonant) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Подсчет слогов |
||
| 146 | * @param $string |
||
| 147 | * @return bool|int |
||
| 148 | */ |
||
| 149 | public static function countSyllables($string) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Проверка парности согласной |
||
| 156 | * |
||
| 157 | * @param $consonant |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | public static function isPairedConsonant($consonant) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Проверка мягкости последней согласной |
||
| 168 | * @param $word |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | 46 | public static function checkLastConsonantSoftness($word) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Проверка мягкости последней согласной, за исключением Н |
||
| 185 | * @param $word |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | 10 | public static function checkBaseLastConsonantSoftness($word) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Проверяет, что гласная образует два звука в словах |
||
| 206 | * @param $vowel |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | 2 | public static function isBinaryVowel($vowel) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Выбор предлога по первой букве |
||
| 216 | * |
||
| 217 | * @param string $word Слово |
||
| 218 | * @param string $prepositionWithVowel Предлог, если слово начинается с гласной |
||
| 219 | * @param string $preposition Предлог, если слово не начинается с гласной |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public static function choosePrepositionByFirstLetter($word, $prepositionWithVowel, $preposition) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Выбор окончания в зависимости от мягкости |
||
| 234 | * |
||
| 235 | * @param $last |
||
| 236 | * @param $softLast |
||
| 237 | * @param $afterSoft |
||
| 238 | * @param $afterHard |
||
| 239 | * |
||
| 240 | * @return mixed |
||
| 241 | */ |
||
| 242 | 130 | public static function chooseVowelAfterConsonant($last, $softLast, $afterSoft, $afterHard) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $verb Verb to modify if gender is female |
||
| 253 | * @param string $gender If not `m`, verb will be modified |
||
| 254 | * @return string Correct verb |
||
| 255 | */ |
||
| 256 | 10 | public static function verb($verb, $gender) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Add 'в' or 'во' prepositional before the word |
||
| 275 | * @param string $word |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 1 | public static function in($word) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Add 'с' or 'со' prepositional before the word |
||
| 288 | * @param string $word |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | 1 | public static function with($word) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Add 'о' or 'об' or 'обо' prepositional before the word |
||
| 301 | * @param string $word |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | 1 | public static function about($word) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Выбирает первое или второе окончание в зависимости от звонкости/глухости в конце слова. |
||
| 318 | * @param string $word Слово (или префикс), на основе звонкости которого нужно выбрать окончание |
||
| 319 | * @param string $ifSonorous Окончание, если слово оканчивается на звонкую согласную |
||
| 320 | * @param string $ifDeaf Окончание, если слово оканчивается на глухую согласную |
||
| 321 | * @return string Первое или второе окончание |
||
| 322 | * @throws \Exception |
||
| 323 | */ |
||
| 324 | 3 | public static function chooseEndingBySonority($word, $ifSonorous, $ifDeaf) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Проверяет, является ли существительно адъективным существительным |
||
| 337 | * @param string $noun Существительное |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | 157 | public static function isAdjectiveNoun($noun) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @param array $forms |
||
| 348 | * @param $animate |
||
| 349 | * @return mixed |
||
| 350 | */ |
||
| 351 | 117 | public static function getVinitCaseByAnimateness(array $forms, $animate) |
|
| 359 | } |
||
| 360 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.