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 | * @param $char |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | 348 | public static function isVowel($char) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Проверка согласной |
||
| 85 | * @param $char |
||
| 86 | * @return bool |
||
| 87 | */ |
||
| 88 | 208 | public static function isConsonant($char) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Проверка звонкости согласной |
||
| 95 | */ |
||
| 96 | 3 | public static function isSonorousConsonant($char) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Проверка глухости согласной |
||
| 103 | * @param $char |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | 6 | public static function isDeafConsonant($char) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Щипящая ли согласная |
||
| 113 | * @param $consonant |
||
| 114 | * @return bool |
||
| 115 | */ |
||
| 116 | 461 | public static function isHissingConsonant($consonant) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Проверка на велярность согласной |
||
| 123 | * @param string[1] $consonant |
||
|
|
|||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | 15 | protected static function isVelarConsonant($consonant) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Подсчет слогов |
||
| 133 | * @param $string |
||
| 134 | * @return bool|int |
||
| 135 | */ |
||
| 136 | public static function countSyllables($string) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Проверка парности согласной |
||
| 143 | * |
||
| 144 | * @param $consonant |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public static function isPairedConsonant($consonant) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Проверка мягкости последней согласной |
||
| 155 | * @param $word |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | 46 | public static function checkLastConsonantSoftness($word) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Проверка мягкости последней согласной, за исключением Н |
||
| 172 | * @param $word |
||
| 173 | * @return bool |
||
| 174 | */ |
||
| 175 | 10 | public static function checkBaseLastConsonantSoftness($word) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * Проверяет, что гласная образует два звука в словах |
||
| 193 | * @param $vowel |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 2 | public static function isBinaryVowel($vowel) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Выбор предлога по первой букве |
||
| 203 | * |
||
| 204 | * @param string $word Слово |
||
| 205 | * @param string $prepositionWithVowel Предлог, если слово начинается с гласной |
||
| 206 | * @param string $preposition Предлог, если слово не начинается с гласной |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public static function choosePrepositionByFirstLetter($word, $prepositionWithVowel, $preposition) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Выбор окончания в зависимости от мягкости |
||
| 221 | * |
||
| 222 | * @param $last |
||
| 223 | * @param $softLast |
||
| 224 | * @param $afterSoft |
||
| 225 | * @param $afterHard |
||
| 226 | * |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | 130 | public static function chooseVowelAfterConsonant($last, $softLast, $afterSoft, $afterHard) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $verb Verb to modify if gender is female |
||
| 240 | * @param string $gender If not `m`, verb will be modified |
||
| 241 | * @return string Correct verb |
||
| 242 | */ |
||
| 243 | 10 | public static function verb($verb, $gender) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Add 'в' or 'во' prepositional before the word |
||
| 262 | * @param string $word |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | 1 | public static function in($word) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Add 'с' or 'со' prepositional before the word |
||
| 275 | * @param string $word |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 1 | public static function with($word) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Add 'о' or 'об' or 'обо' prepositional before the word |
||
| 288 | * @param string $word |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | 1 | public static function about($word) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Выбирает первое или второе окончание в зависимости от звонкости/глухости в конце слова. |
||
| 305 | * @param string $word Слово (или префикс), на основе звонкости которого нужно выбрать окончание |
||
| 306 | * @param string $ifSonorous Окончание, если слово оканчивается на звонкую согласную |
||
| 307 | * @param string $ifDeaf Окончание, если слово оканчивается на глухую согласную |
||
| 308 | * @return string Первое или второе окончание |
||
| 309 | * @throws \Exception |
||
| 310 | */ |
||
| 311 | 3 | public static function chooseEndingBySonority($word, $ifSonorous, $ifDeaf) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Проверяет, является ли существительно адъективным существительным |
||
| 324 | * @param string $noun Существительное |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | 157 | public static function isAdjectiveNoun($noun) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param array $forms |
||
| 335 | * @param $animate |
||
| 336 | * @return mixed |
||
| 337 | */ |
||
| 338 | 117 | public static function getVinitCaseByAnimateness(array $forms, $animate) |
|
| 346 | } |
||
| 347 |
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.