Conditions | 31 |
Paths | 106 |
Total Lines | 115 |
Code Lines | 89 |
Lines | 25 |
Ratio | 21.74 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
154 | static public function getCases($number, $gender = self::MALE) { |
||
155 | // simple numeral |
||
156 | if (isset(self::$words[$number]) || isset(self::$exponents[$number])) { |
||
157 | $word = isset(self::$words[$number]) ? self::$words[$number] : self::$exponents[$number]; |
||
158 | if (isset(self::$precalculated[$word])) { |
||
159 | if (isset(self::$precalculated[$word][self::MALE])) { |
||
160 | return self::$precalculated[$word][$gender]; |
||
161 | } else { |
||
162 | return self::$precalculated[$word]; |
||
163 | } |
||
164 | } else if (($number >= 5 && $number <= 20) || $number == 30) { |
||
165 | $prefix = S::slice($word, 0, -1); |
||
166 | return array( |
||
167 | self::IMENIT => $word, |
||
168 | self::RODIT => $prefix.'и', |
||
169 | self::DAT => $prefix.'и', |
||
170 | self::VINIT => $word, |
||
171 | self::TVORIT => $prefix.'ью', |
||
172 | self::PREDLOJ => self::choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'и', |
||
173 | ); |
||
174 | } else if (in_array($number, array(40, 90, 100))) { |
||
175 | $prefix = $number == 40 ? $word : S::slice($word, 0, -1); |
||
176 | return array( |
||
177 | self::IMENIT => $word, |
||
178 | self::RODIT => $prefix.'а', |
||
179 | self::DAT => $prefix.'а', |
||
180 | self::VINIT => $word, |
||
181 | self::TVORIT => $prefix.'а', |
||
182 | self::PREDLOJ => self::choosePrepositionByFirstLetter($prefix, 'об', 'о').' '.$prefix.'а', |
||
183 | ); |
||
184 | } else if (($number >= 50 && $number <= 80)) { |
||
185 | $prefix = S::slice($word, 0, -6); |
||
186 | return array( |
||
187 | self::IMENIT => $prefix.'ьдесят', |
||
188 | self::RODIT => $prefix.'идесяти', |
||
189 | self::DAT => $prefix.'идесяти', |
||
190 | self::VINIT => $prefix.'ьдесят', |
||
191 | self::TVORIT => $prefix.'ьюдесятью', |
||
192 | self::PREDLOJ => self::choosePrepositionByFirstLetter($word, 'об', 'о').' '.$prefix.'идесяти', |
||
193 | ); |
||
194 | } else if (in_array($number, array(300, 400))) { |
||
195 | $prefix = S::slice($word, 0, -4); |
||
196 | return array( |
||
197 | self::IMENIT => $word, |
||
198 | self::RODIT => $prefix.'ехсот', |
||
199 | self::DAT => $prefix.'емстам', |
||
200 | self::VINIT => $word, |
||
201 | self::TVORIT => $prefix.($number == 300 ? 'е' : 'ь').'мястами', |
||
202 | self::PREDLOJ => self::choosePrepositionByFirstLetter($word, 'об', 'о').' '.$prefix.'ехстах', |
||
203 | ); |
||
204 | } else if ($number >= 500 && $number <= 900) { |
||
205 | $prefix = S::slice($word, 0, -4); |
||
206 | return array( |
||
207 | self::IMENIT => $word, |
||
208 | self::RODIT => $prefix.'исот', |
||
209 | self::DAT => $prefix.'истам', |
||
210 | self::VINIT => $word, |
||
211 | self::TVORIT => $prefix.'ьюстами', |
||
212 | self::PREDLOJ => self::choosePrepositionByFirstLetter($word, 'об', 'о').' '.$prefix.'истах', |
||
213 | ); |
||
214 | } else if (isset(self::$exponents[$number])) { |
||
215 | return GeneralDeclension::getCases($word, false); |
||
216 | } |
||
217 | } |
||
218 | // compound numeral |
||
219 | else { |
||
220 | $parts = array(); |
||
221 | $result = array(); |
||
222 | |||
223 | foreach (array_reverse(self::$exponents, true) as $word_number => $word) { |
||
224 | if ($number >= $word_number) { |
||
225 | $count = floor($number / $word_number); |
||
226 | $parts[] = self::getCases($count, ($word_number == 1000 ? self::FEMALE : self::MALE)); |
||
227 | |||
228 | switch (Plurality::getNumeralForm($count)) { |
||
229 | case Plurality::ONE: |
||
230 | $parts[] = GeneralDeclension::getCases($word, false); |
||
231 | break; |
||
232 | View Code Duplication | case Plurality::TWO_FOUR: |
|
|
|||
233 | $part = Plurality::getCases($word); |
||
234 | if ($word_number != 1000) // get dative case of word for 1000000, 1000000000 and 1000000000000 |
||
235 | $part[Cases::IMENIT] = $part[Cases::VINIT] = GeneralDeclension::getCase($word, Cases::RODIT); |
||
236 | $parts[] = $part; |
||
237 | break; |
||
238 | View Code Duplication | case Plurality::FIVE_OTHER: |
|
239 | $part = Plurality::getCases($word); |
||
240 | $part[Cases::IMENIT] = $part[Cases::VINIT] = $part[Cases::RODIT]; |
||
241 | $parts[] = $part; |
||
242 | break; |
||
243 | } |
||
244 | |||
245 | $number = $number % ($count * $word_number); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | View Code Duplication | foreach (array_reverse(self::$words, true) as $word_number => $word) { |
|
250 | if ($number >= $word_number) { |
||
251 | $parts[] = self::getCases($word_number, $gender); |
||
252 | $number %= $word_number; |
||
253 | } |
||
254 | } |
||
255 | |||
256 | // make one array with cases and delete 'o/об' prepositional from all parts except the last one |
||
257 | View Code Duplication | foreach (array(self::IMENIT, self::RODIT, self::DAT, self::VINIT, self::TVORIT, self::PREDLOJ) as $case) { |
|
258 | $result[$case] = array(); |
||
259 | foreach ($parts as $partN => $part) { |
||
260 | if ($case == self::PREDLOJ && $partN > 0) list(, $part[$case]) = explode(' ', $part[$case], 2); |
||
261 | $result[$case][] = $part[$case]; |
||
262 | } |
||
263 | $result[$case] = implode(' ', $result[$case]); |
||
264 | } |
||
265 | |||
266 | return $result; |
||
267 | } |
||
268 | } |
||
269 | |||
276 |
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.