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\BasePluralization implements Cases |
||
10 | { |
||
11 | use RussianLanguage, CasesHelper; |
||
12 | |||
13 | const ONE = 1; |
||
14 | const TWO_FOUR = 2; |
||
15 | const FIVE_OTHER = 3; |
||
16 | |||
17 | /** |
||
18 | * @var string[][] |
||
19 | * @phpstan-var array<string, string[]> |
||
20 | */ |
||
21 | protected static $abnormalExceptions = [ |
||
22 | 'человек' => ['люди', 'человек', 'людям', 'людей', 'людьми', 'людях'], |
||
23 | ]; |
||
24 | |||
25 | /** @var string[] */ |
||
26 | protected static $neuterExceptions = [ |
||
27 | 'поле', |
||
28 | 'море', |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * @var string[] |
||
33 | * @phpstan-var array<string, string> |
||
34 | */ |
||
35 | protected static $genitiveExceptions = [ |
||
36 | 'письмо' => 'писем', |
||
37 | 'пятно' => 'пятен', |
||
38 | 'кресло' => 'кресел', |
||
39 | 'коромысло' => 'коромысел', |
||
40 | 'ядро' => 'ядер', |
||
41 | 'блюдце' => 'блюдец', |
||
42 | 'полотенце' => 'полотенец', |
||
43 | 'гривна' => 'гривен', |
||
44 | 'год' => 'лет', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Склонение существительного для сочетания с числом (кол-вом предметов). |
||
49 | * |
||
50 | * @param string|int $word Название предмета |
||
51 | * @param int|float|string $count Количество предметов |
||
52 | * @param bool $animateness Признак одушевленности |
||
53 | * @param string $case Род существительного |
||
54 | * |
||
55 | * @return string |
||
56 | * @throws \Exception |
||
57 | */ |
||
58 | 62 | public static function pluralize($word, $count = 2, $animateness = false, $case = null) |
|
59 | { |
||
60 | // меняем местами аргументы, если они переданы в старом формате |
||
61 | 62 | View Code Duplication | if (is_string($count) && is_numeric($word)) { |
|
|||
62 | 1 | list($count, $word) = [$word, $count]; |
|
63 | } |
||
64 | |||
65 | 62 | if ($case !== null) |
|
66 | 3 | $case = static::canonizeCase($case); |
|
67 | |||
68 | // для адъективных существительных правила склонения проще: |
||
69 | // только две формы |
||
70 | 62 | View Code Duplication | if (static::isAdjectiveNoun($word)) { |
71 | 3 | if (static::getNumeralForm($count) == static::ONE) |
|
72 | 2 | return $word; |
|
73 | else |
||
74 | 3 | return NounPluralization::getCase($word, |
|
75 | 3 | $case !== null |
|
76 | ? $case |
||
77 | 3 | : static::RODIT, $animateness); |
|
78 | } |
||
79 | |||
80 | 60 | if ($case === null) { |
|
81 | 57 | switch (static::getNumeralForm($count)) { |
|
82 | 57 | case static::ONE: |
|
83 | 29 | return $word; |
|
84 | 57 | case static::TWO_FOUR: |
|
85 | 36 | return NounDeclension::getCase($word, static::RODIT, $animateness); |
|
86 | 44 | case static::FIVE_OTHER: |
|
87 | 44 | return NounPluralization::getCase($word, static::RODIT, $animateness); |
|
88 | } |
||
89 | } |
||
90 | |||
91 | 3 | View Code Duplication | if (static::getNumeralForm($count) == static::ONE) |
92 | 3 | return NounDeclension::getCase($word, $case, $animateness); |
|
93 | else |
||
94 | 3 | return NounPluralization::getCase($word, $case, $animateness); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param int|float $count |
||
99 | * @return int |
||
100 | */ |
||
101 | 74 | public static function getNumeralForm($count) |
|
116 | |||
117 | /** |
||
118 | * @param string $word |
||
119 | * @param string $case |
||
120 | * @param bool $animateness |
||
121 | * @return string |
||
122 | * @throws \Exception |
||
123 | */ |
||
124 | 49 | public static function getCase($word, $case, $animateness = false) |
|
130 | |||
131 | /** |
||
132 | * @param string $word |
||
133 | * @param bool $animateness |
||
134 | * @return string[] |
||
135 | * @phpstan-return array<string, string> |
||
136 | */ |
||
137 | 97 | public static function getCases($word, $animateness = false) |
|
167 | |||
168 | /** |
||
169 | * Склонение обычных существительных. |
||
170 | * @param string $word |
||
171 | * @param bool $animateness |
||
172 | * @return string[] |
||
173 | * @phpstan-return array<string, string> |
||
174 | */ |
||
175 | 86 | protected static function declinateSubstative($word, $animateness) |
|
254 | |||
255 | /** |
||
256 | * Склонение существительных, образованных от прилагательных и причастий. |
||
257 | * Rules are from http://rusgram.narod.ru/1216-1231.html |
||
258 | * @param string $word |
||
259 | * @param bool $animateness |
||
260 | * @return string[] |
||
261 | * @phpstan-return array<string, string> |
||
262 | */ |
||
263 | 11 | protected static function declinateAdjective($word, $animateness) |
|
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.