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:
1 | <?php |
||
11 | class CardinalNumeralGenerator extends NumeralGenerator implements Cases |
||
12 | { |
||
13 | use RussianLanguage, CasesHelper; |
||
14 | |||
15 | /** |
||
16 | * @var string[] |
||
17 | * @phpstan-var array<int, string> |
||
18 | */ |
||
19 | protected static $words = [ |
||
20 | 1 => 'один', |
||
21 | 2 => 'два', |
||
22 | 3 => 'три', |
||
23 | 4 => 'четыре', |
||
24 | 5 => 'пять', |
||
25 | 6 => 'шесть', |
||
26 | 7 => 'семь', |
||
27 | 8 => 'восемь', |
||
28 | 9 => 'девять', |
||
29 | 10 => 'десять', |
||
30 | 11 => 'одиннадцать', |
||
31 | 12 => 'двенадцать', |
||
32 | 13 => 'тринадцать', |
||
33 | 14 => 'четырнадцать', |
||
34 | 15 => 'пятнадцать', |
||
35 | 16 => 'шестнадцать', |
||
36 | 17 => 'семнадцать', |
||
37 | 18 => 'восемнадцать', |
||
38 | 19 => 'девятнадцать', |
||
39 | 20 => 'двадцать', |
||
40 | 30 => 'тридцать', |
||
41 | 40 => 'сорок', |
||
42 | 50 => 'пятьдесят', |
||
43 | 60 => 'шестьдесят', |
||
44 | 70 => 'семьдесят', |
||
45 | 80 => 'восемьдесят', |
||
46 | 90 => 'девяносто', |
||
47 | 100 => 'сто', |
||
48 | 200 => 'двести', |
||
49 | 300 => 'триста', |
||
50 | 400 => 'четыреста', |
||
51 | 500 => 'пятьсот', |
||
52 | 600 => 'шестьсот', |
||
53 | 700 => 'семьсот', |
||
54 | 800 => 'восемьсот', |
||
55 | 900 => 'девятьсот', |
||
56 | ]; |
||
57 | |||
58 | /** |
||
59 | * @var string[] |
||
60 | * @phpstan-var array<int, string> |
||
61 | */ |
||
62 | protected static $exponents = [ |
||
63 | 1000 => 'тысяча', |
||
64 | 1000000 => 'миллион', |
||
65 | 1000000000 => 'миллиард', |
||
66 | 1000000000000 => 'триллион', |
||
67 | 1000000000000000 => 'квадриллион', |
||
68 | ]; |
||
69 | |||
70 | /** |
||
71 | * @var array |
||
72 | * @phpstan-var array<string, array<string, array<string, string>|string>> |
||
73 | */ |
||
74 | protected static $precalculated = [ |
||
75 | 'один' => [ |
||
76 | self::MALE => [ |
||
77 | self::IMENIT => 'один', |
||
78 | self::RODIT => 'одного', |
||
79 | self::DAT => 'одному', |
||
80 | self::VINIT => 'один', |
||
81 | self::TVORIT => 'одним', |
||
82 | self::PREDLOJ => 'одном', |
||
83 | ], |
||
84 | self::FEMALE => [ |
||
85 | self::IMENIT => 'одна', |
||
86 | self::RODIT => 'одной', |
||
87 | self::DAT => 'одной', |
||
88 | self::VINIT => 'одну', |
||
89 | self::TVORIT => 'одной', |
||
90 | self::PREDLOJ => 'одной', |
||
91 | ], |
||
92 | self::NEUTER => [ |
||
93 | self::IMENIT => 'одно', |
||
94 | self::RODIT => 'одного', |
||
95 | self::DAT => 'одному', |
||
96 | self::VINIT => 'одно', |
||
97 | self::TVORIT => 'одним', |
||
98 | self::PREDLOJ => 'одном', |
||
99 | ], |
||
100 | ], |
||
101 | 'два' => [ |
||
102 | self::MALE => [ |
||
103 | self::IMENIT => 'два', |
||
104 | self::RODIT => 'двух', |
||
105 | self::DAT => 'двум', |
||
106 | self::VINIT => 'два', |
||
107 | self::TVORIT => 'двумя', |
||
108 | self::PREDLOJ => 'двух', |
||
109 | ], |
||
110 | self::FEMALE => [ |
||
111 | self::IMENIT => 'две', |
||
112 | self::RODIT => 'двух', |
||
113 | self::DAT => 'двум', |
||
114 | self::VINIT => 'две', |
||
115 | self::TVORIT => 'двумя', |
||
116 | self::PREDLOJ => 'двух', |
||
117 | ], |
||
118 | self::NEUTER => [ |
||
119 | self::IMENIT => 'два', |
||
120 | self::RODIT => 'двух', |
||
121 | self::DAT => 'двум', |
||
122 | self::VINIT => 'два', |
||
123 | self::TVORIT => 'двумя', |
||
124 | self::PREDLOJ => 'двух', |
||
125 | ], |
||
126 | ], |
||
127 | 'три' => [ |
||
128 | self::IMENIT => 'три', |
||
129 | self::RODIT => 'трех', |
||
130 | self::DAT => 'трем', |
||
131 | self::VINIT => 'три', |
||
132 | self::TVORIT => 'тремя', |
||
133 | self::PREDLOJ => 'трех', |
||
134 | ], |
||
135 | 'четыре' => [ |
||
136 | self::IMENIT => 'четыре', |
||
137 | self::RODIT => 'четырех', |
||
138 | self::DAT => 'четырем', |
||
139 | self::VINIT => 'четыре', |
||
140 | self::TVORIT => 'четырьмя', |
||
141 | self::PREDLOJ => 'четырех', |
||
142 | ], |
||
143 | 'двести' => [ |
||
144 | self::IMENIT => 'двести', |
||
145 | self::RODIT => 'двухсот', |
||
146 | self::DAT => 'двумстам', |
||
147 | self::VINIT => 'двести', |
||
148 | self::TVORIT => 'двумястами', |
||
149 | self::PREDLOJ => 'двухстах', |
||
150 | ], |
||
151 | 'восемьсот' => [ |
||
152 | self::IMENIT => 'восемьсот', |
||
153 | self::RODIT => 'восьмисот', |
||
154 | self::DAT => 'восьмистам', |
||
155 | self::VINIT => 'восемьсот', |
||
156 | self::TVORIT => 'восьмистами', |
||
157 | self::PREDLOJ => 'восьмистах', |
||
158 | ], |
||
159 | 'тысяча' => [ |
||
160 | self::IMENIT => 'тысяча', |
||
161 | self::RODIT => 'тысяч', |
||
162 | self::DAT => 'тысячам', |
||
163 | self::VINIT => 'тысяч', |
||
164 | self::TVORIT => 'тысячей', |
||
165 | self::PREDLOJ => 'тысячах', |
||
166 | ], |
||
167 | ]; |
||
168 | |||
169 | /** |
||
170 | * @param int $number |
||
171 | * @param string $gender |
||
172 | * @return string[] |
||
173 | * @phpstan-return array<string, string> |
||
174 | * @throws \Exception |
||
175 | */ |
||
176 | 33 | public static function getCases($number, $gender = self::MALE) |
|
305 | |||
306 | /** |
||
307 | * @param int $number |
||
308 | * @param string $case |
||
309 | * @param string $gender |
||
310 | * |
||
311 | * @return string |
||
312 | * @throws \Exception |
||
313 | */ |
||
314 | 25 | public static function getCase($number, $case, $gender = self::MALE) |
|
320 | } |
||
321 |
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.