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 NounDeclension 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 NounDeclension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class NounDeclension extends \morphos\BaseInflection implements Cases, Gender |
||
| 11 | { |
||
| 12 | use RussianLanguage, CasesHelper; |
||
| 13 | |||
| 14 | const FIRST_DECLENSION = 1; |
||
| 15 | const SECOND_DECLENSION = 2; |
||
| 16 | const THIRD_DECLENSION = 3; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * These words has 2 declension type. |
||
| 20 | */ |
||
| 21 | protected static $abnormalExceptions = [ |
||
| 22 | 'бремя', |
||
| 23 | 'вымя', |
||
| 24 | 'темя', |
||
| 25 | 'пламя', |
||
| 26 | 'стремя', |
||
| 27 | 'пламя', |
||
| 28 | 'время', |
||
| 29 | 'знамя', |
||
| 30 | 'имя', |
||
| 31 | 'племя', |
||
| 32 | 'семя', |
||
| 33 | 'путь' => ['путь', 'пути', 'пути', 'путь', 'путем', 'пути'], |
||
| 34 | 'дитя' => ['дитя', 'дитяти', 'дитяти', 'дитя', 'дитятей', 'дитяти'], |
||
| 35 | ]; |
||
| 36 | |||
| 37 | protected static $masculineWithSoft = [ |
||
| 38 | 'олень', |
||
| 39 | 'конь', |
||
| 40 | 'ячмень', |
||
| 41 | 'путь', |
||
| 42 | 'зверь', |
||
| 43 | 'шкворень', |
||
| 44 | 'пельмень', |
||
| 45 | 'тюлень', |
||
| 46 | 'выхухоль', |
||
| 47 | 'табель', |
||
| 48 | 'рояль', |
||
| 49 | 'шампунь', |
||
| 50 | 'конь', |
||
| 51 | 'лось', |
||
| 52 | 'гвоздь', |
||
| 53 | 'медведь', |
||
| 54 | 'рубль', |
||
| 55 | 'дождь', |
||
| 56 | 'юань', |
||
| 57 | ]; |
||
| 58 | |||
| 59 | protected static $masculineWithSoftAndRunAwayVowels = [ |
||
| 60 | 'день', |
||
| 61 | 'пень', |
||
| 62 | 'парень', |
||
| 63 | 'камень', |
||
| 64 | 'корень', |
||
| 65 | 'трутень', |
||
| 66 | ]; |
||
| 67 | |||
| 68 | protected static $immutableWords = [ |
||
| 69 | // валюты |
||
| 70 | 'евро', 'пенни', 'песо', 'сентаво', |
||
| 71 | |||
| 72 | // на а |
||
| 73 | 'боа', 'бра', 'фейхоа', 'амплуа', 'буржуа', |
||
| 74 | // на о |
||
| 75 | 'манго', 'какао', 'кино', 'трюмо', 'пальто', 'бюро', 'танго', 'вето', 'бунгало', 'сабо', 'авокадо', 'депо', |
||
| 76 | // на у |
||
| 77 | 'зебу', 'кенгуру', 'рагу', 'какаду', 'шоу', |
||
| 78 | // на е |
||
| 79 | 'шимпанзе', 'конферансье', 'атташе', 'колье', 'резюме', 'пенсне', 'кашне', 'протеже', 'коммюнике', 'драже', 'суфле', 'пюре', 'купе', 'фойе', 'шоссе', |
||
| 80 | // на и |
||
| 81 | 'такси', 'жалюзи', 'шасси', 'алиби', 'киви', 'иваси', 'регби', 'конфетти', 'колибри', 'жюри', 'пенальти', 'рефери', 'кольраби', |
||
| 82 | // на э |
||
| 83 | 'каноэ', 'алоэ', |
||
| 84 | // на ю |
||
| 85 | 'меню', 'парвеню', 'авеню', 'дежавю', 'инженю', 'барбекю', 'интервью', |
||
| 86 | ]; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Проверка, изменяемое ли слово. |
||
| 90 | * @param string $word Слово для проверки |
||
| 91 | * @param bool $animateness Признак одушевленности |
||
| 92 | * @return bool |
||
| 93 | */ |
||
| 94 | 43 | public static function isMutable($word, $animateness = false) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Определение рода существительного. |
||
| 105 | * @param string $word |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 8 | public static function detectGender($word) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Определение склонения (по школьной программе) существительного. |
||
| 125 | * @param $word |
||
| 126 | * @return int |
||
| 127 | */ |
||
| 128 | 169 | public static function getDeclension($word) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Получение слова во всех 6 падежах. |
||
| 149 | * @param string $word |
||
| 150 | * @param bool $animateness Признак одушевлённости |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | 96 | public static function getCases($word, $animateness = false) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Получение всех форм слова первого склонения. |
||
| 198 | * @param $word |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | 25 | public static function declinateFirstDeclension($word) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Получение всех форм слова второго склонения. |
||
| 239 | * @param $word |
||
| 240 | * @param bool $animateness |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | 55 | public static function declinateSecondDeclension($word, $animateness = false) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Получение всех форм слова третьего склонения. |
||
| 292 | * @param $word |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | 4 | public static function declinateThirdDeclension($word) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Склонение существительных, образованных от прилагательных и причастий. |
||
| 311 | * Rules are from http://rusgram.narod.ru/1216-1231.html |
||
| 312 | * @param $word |
||
| 313 | * @param $animateness |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | 8 | public static function declinateAdjective($word, $animateness) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Получение одной формы слова (падежа). |
||
| 372 | * @param string $word Слово |
||
| 373 | * @param integer $case Падеж |
||
| 374 | * @param bool $animateness Признак одушевленности |
||
| 375 | * @return string |
||
| 376 | * @throws \Exception |
||
| 377 | */ |
||
| 378 | 37 | public static function getCase($word, $case, $animateness = false) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @param $word |
||
| 387 | * @param $last |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | 82 | public static function getPrefixOfSecondDeclension($word, $last) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * @param array $forms |
||
| 409 | * @param $animate |
||
| 410 | * @return mixed |
||
| 411 | */ |
||
| 412 | 101 | public static function getVinitCaseByAnimateness(array $forms, $animate) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @param $word |
||
| 423 | * @param $last |
||
| 424 | * @param $prefix |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | 78 | public static function getPredCaseOf12Declensions($word, $last, $prefix) |
|
| 439 | } |
||
| 440 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.