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 NounPluralization 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 NounPluralization, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class NounPluralization extends \morphos\NounPluralization implements Cases |
||
| 10 | { |
||
| 11 | use RussianLanguage, CasesHelper; |
||
| 12 | |||
| 13 | const ONE = 1; |
||
| 14 | const TWO_FOUR = 2; |
||
| 15 | const FIVE_OTHER = 3; |
||
| 16 | |||
| 17 | protected static $neuterExceptions = [ |
||
| 18 | 'поле', |
||
| 19 | 'море', |
||
| 20 | ]; |
||
| 21 | |||
| 22 | protected static $genitiveExceptions = [ |
||
| 23 | 'письмо' => 'писем', |
||
| 24 | 'пятно' => 'пятен', |
||
| 25 | 'кресло' => 'кресел', |
||
| 26 | 'коромысло' => 'коромысел', |
||
| 27 | 'ядро' => 'ядер', |
||
| 28 | 'блюдце' => 'блюдец', |
||
| 29 | 'полотенце' => 'полотенец', |
||
| 30 | 'гривна' => 'гривен', |
||
| 31 | 'год' => 'лет', |
||
| 32 | ]; |
||
| 33 | |||
| 34 | protected static $immutableWords = [ |
||
| 35 | 'евро', |
||
| 36 | 'пенни', |
||
| 37 | ]; |
||
| 38 | |||
| 39 | protected static $runawayVowelsExceptions = [ |
||
| 40 | 'писе*ц', |
||
| 41 | 'песе*ц', |
||
| 42 | 'глото*к', |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return array|bool |
||
| 47 | */ |
||
| 48 | 69 | View Code Duplication | protected static function getRunAwayVowelsList() |
| 56 | |||
| 57 | /** |
||
| 58 | * Склонение существительного для сочетания с числом (кол-вом предметов). |
||
| 59 | * @param int|string $count Количество предметов |
||
| 60 | * @param string|int $word Название предмета |
||
| 61 | * @param bool $animateness Признак одушевленности |
||
| 62 | * @return string |
||
| 63 | * @throws \Exception |
||
| 64 | */ |
||
| 65 | 50 | public static function pluralize($word, $count = 2, $animateness = false) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param $count |
||
| 98 | * @return int |
||
| 99 | */ |
||
| 100 | 62 | public static function getNumeralForm($count) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @param $word |
||
| 118 | * @param $case |
||
| 119 | * @param bool $animateness |
||
| 120 | * @return string |
||
| 121 | * @throws \Exception |
||
| 122 | */ |
||
| 123 | 36 | public static function getCase($word, $case, $animateness = false) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @param $word |
||
| 132 | * @param bool $animateness |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 79 | public static function getCases($word, $animateness = false) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Склонение обычных существительных. |
||
| 161 | * @param $word |
||
| 162 | * @param $animateness |
||
| 163 | * @return array |
||
| 164 | */ |
||
| 165 | 69 | protected static function declinateSubstative($word, $animateness) |
|
| 166 | { |
||
| 167 | 69 | $prefix = S::slice($word, 0, -1); |
|
| 168 | 69 | $last = S::slice($word, -1); |
|
| 169 | |||
| 170 | 69 | $runaway_vowels_list = static::getRunAwayVowelsList(); |
|
| 171 | 69 | if (isset($runaway_vowels_list[$word])) { |
|
| 172 | 1 | $vowel_offset = $runaway_vowels_list[$word]; |
|
| 173 | 1 | $word = S::slice($word, 0, $vowel_offset) . S::slice($word, $vowel_offset + 1); |
|
| 174 | } |
||
| 175 | |||
| 176 | 69 | if (($declension = NounDeclension::getDeclension($word)) == NounDeclension::SECOND_DECLENSION) { |
|
| 177 | 42 | $soft_last = $last == 'й' || (in_array($last, ['ь', 'е', 'ё', 'ю', 'я'], true) |
|
| 178 | && (( |
||
| 179 | 12 | self::isConsonant(S::slice($word, -2, -1)) && !self::isHissingConsonant(S::slice($word, -2, -1))) |
|
| 180 | 42 | || S::slice($word, -2, -1) == 'и')); |
|
| 181 | 42 | $prefix = NounDeclension::getPrefixOfSecondDeclension($word, $last); |
|
| 182 | 33 | } elseif ($declension == NounDeclension::FIRST_DECLENSION) { |
|
| 183 | 29 | $soft_last = self::checkLastConsonantSoftness($word); |
|
| 184 | } else { |
||
| 185 | 4 | $soft_last = in_array(S::slice($word, -2), ['чь', 'сь', 'ть', 'нь'], true); |
|
| 186 | } |
||
| 187 | |||
| 188 | 69 | $forms = []; |
|
| 189 | |||
| 190 | 69 | if (in_array($last, ['ч', 'г'], false) || in_array(S::slice($word, -2), ['чь', 'сь', 'ть', 'нь'], true) |
|
| 191 | 69 | || (self::isVowel($last) && in_array(S::slice($word, -2, -1), ['ч', 'к'], true))) { // before ч, чь, сь, ч+vowel, к+vowel |
|
| 192 | 32 | $forms[Cases::IMENIT] = $prefix.'и'; |
|
| 193 | 42 | } elseif (in_array($last, ['н', 'ц', 'р', 'т'], true)) { |
|
| 194 | 16 | $forms[Cases::IMENIT] = $prefix.'ы'; |
|
| 195 | View Code Duplication | } else { |
|
| 196 | 29 | $forms[Cases::IMENIT] = self::chooseVowelAfterConsonant($last, $soft_last, $prefix.'я', $prefix.'а'); |
|
| 197 | } |
||
| 198 | |||
| 199 | // RODIT |
||
| 200 | 69 | if (isset(self::$genitiveExceptions[$word])) { |
|
| 201 | 5 | $forms[Cases::RODIT] = self::$genitiveExceptions[$word]; |
|
| 202 | 64 | } elseif (in_array($last, ['о', 'е'], true)) { |
|
| 203 | // exceptions |
||
| 204 | 8 | if (in_array($word, self::$neuterExceptions, true)) { |
|
| 205 | 3 | $forms[Cases::RODIT] = $prefix.'ей'; |
|
| 206 | 5 | View Code Duplication | } elseif (S::slice($word, -2, -1) == 'и') { |
| 207 | 2 | $forms[Cases::RODIT] = $prefix.'й'; |
|
| 208 | } else { |
||
| 209 | 8 | $forms[Cases::RODIT] = $prefix; |
|
| 210 | } |
||
| 211 | 56 | } elseif (S::slice($word, -2) == 'ка' && S::slice($word, -3, -2) !== 'и') { // words ending with -ка: чашка, вилка, ложка, тарелка, копейка, батарейка |
|
| 212 | 7 | if (S::slice($word, -3, -2) == 'л') { |
|
| 213 | 2 | $forms[Cases::RODIT] = S::slice($word, 0, -2).'ок'; |
|
| 214 | 5 | View Code Duplication | } elseif (S::slice($word, -3, -2) == 'й') { |
| 215 | 3 | $forms[Cases::RODIT] = S::slice($word, 0, -3).'ек'; |
|
| 216 | } else { |
||
| 217 | 7 | $forms[Cases::RODIT] = S::slice($word, 0, -2).'ек'; |
|
| 218 | } |
||
| 219 | 49 | } elseif (in_array($last, ['а'], true)) { // обида, ябеда |
|
| 220 | 20 | $forms[Cases::RODIT] = $prefix; |
|
| 221 | 35 | View Code Duplication | } elseif (in_array($last, ['я'], true)) { // молния |
| 222 | 1 | $forms[Cases::RODIT] = $prefix.'й'; |
|
| 223 | 34 | } elseif (RussianLanguage::isHissingConsonant($last) || ($soft_last && $last != 'й') || in_array(S::slice($word, -2), ['чь', 'сь', 'ть', 'нь'], true)) { |
|
| 224 | 12 | $forms[Cases::RODIT] = $prefix.'ей'; |
|
| 225 | 24 | View Code Duplication | } elseif ($last == 'й' || S::slice($word, -2) == 'яц') { // месяц |
| 226 | 7 | $forms[Cases::RODIT] = $prefix.'ев'; |
|
| 227 | } else { // (self::isConsonant($last) && !RussianLanguage::isHissingConsonant($last)) |
||
| 228 | 17 | $forms[Cases::RODIT] = $prefix.'ов'; |
|
| 229 | } |
||
| 230 | |||
| 231 | // DAT |
||
| 232 | 69 | $forms[Cases::DAT] = self::chooseVowelAfterConsonant($last, $soft_last && S::slice($word, -2, -1) != 'ч', $prefix.'ям', $prefix.'ам'); |
|
| 233 | |||
| 234 | // VINIT |
||
| 235 | 69 | $forms[Cases::VINIT] = NounDeclension::getVinitCaseByAnimateness($forms, $animateness); |
|
| 236 | |||
| 237 | // TVORIT |
||
| 238 | // my personal rule |
||
| 239 | 69 | if ($last == 'ь' && $declension == NounDeclension::THIRD_DECLENSION && !in_array(S::slice($word, -2), ['чь', 'сь', 'ть', 'нь'], true)) { |
|
| 240 | $forms[Cases::TVORIT] = $prefix.'ми'; |
||
| 241 | } else { |
||
| 242 | 69 | $forms[Cases::TVORIT] = self::chooseVowelAfterConsonant($last, $soft_last && S::slice($word, -2, -1) != 'ч', $prefix.'ями', $prefix.'ами'); |
|
| 243 | } |
||
| 244 | |||
| 245 | // PREDLOJ |
||
| 246 | 69 | $forms[Cases::PREDLOJ] = self::chooseVowelAfterConsonant($last, $soft_last && S::slice($word, -2, -1) != 'ч', $prefix.'ях', $prefix.'ах'); |
|
| 247 | 69 | return $forms; |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Склонение существительных, образованных от прилагательных и причастий. |
||
| 252 | * Rules are from http://rusgram.narod.ru/1216-1231.html |
||
| 253 | * @param $word |
||
| 254 | * @param $animateness |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | 11 | protected static function declinateAdjective($word, $animateness) |
|
| 270 | } |
||
| 271 |
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.