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 |
||
9 | class LastNamesInflection extends \morphos\NamesInflection implements Cases |
||
10 | { |
||
11 | use RussianLanguage, CasesHelper; |
||
12 | |||
13 | protected static $menPostfixes = ['ов', 'ев' ,'ин' ,'ын', 'ой', 'ий']; |
||
14 | protected static $womenPostfixes = ['ва', 'на', 'ая', 'яя']; |
||
15 | |||
16 | /** |
||
17 | * @param $name |
||
18 | * @param null $gender |
||
19 | * @return bool |
||
20 | */ |
||
21 | 60 | public static function isMutable($name, $gender = null) |
|
22 | { |
||
23 | 60 | $name = S::lower($name); |
|
24 | 60 | if ($gender === null) { |
|
25 | $gender = self::detectGender($name); |
||
26 | } |
||
27 | // составная фамилия - разбить на части и проверить по отдельности |
||
28 | 60 | if (strpos($name, '-') !== false) { |
|
29 | 3 | foreach (explode('-', $name) as $part) { |
|
30 | 3 | if (static::isMutable($part, $gender)) |
|
|
|||
31 | 3 | return true; |
|
32 | } |
||
33 | return false; |
||
34 | } |
||
35 | |||
36 | 60 | View Code Duplication | if (in_array(S::slice($name, -1), ['а', 'я'], true)) { |
37 | 22 | return true; |
|
38 | } |
||
39 | |||
40 | 39 | if ($gender == self::MALE) { |
|
41 | // Несклоняемые фамилии (Фоминых, Седых / Стецко, Писаренко) |
||
42 | 38 | View Code Duplication | if (in_array(S::slice($name, -2), ['ых', 'ко'], true)) |
43 | 1 | return false; |
|
44 | |||
45 | // Несклоняемые, образованные из родительного падежа личного или прозвищного имени главы семьи |
||
46 | // суффиксы: ово, аго |
||
47 | 37 | View Code Duplication | if (in_array(S::slice($name, -3), ['ово', 'аго'], true)) |
48 | 1 | return false; |
|
49 | |||
50 | // Типичные суффикс мужских фамилий |
||
51 | 36 | if (in_array(S::slice($name, -2), ['ов', 'ев', 'ин', 'ын', 'ий', 'ой'], true)) { |
|
52 | 13 | return true; |
|
53 | } |
||
54 | |||
55 | // Согласная на конце |
||
56 | 23 | if (self::isConsonant(S::slice($name, -1))) { |
|
57 | 15 | return true; |
|
58 | } |
||
59 | |||
60 | // Мягкий знак на конце |
||
61 | 8 | if (S::slice($name, -1) == 'ь') { |
|
62 | 8 | return true; |
|
63 | } |
||
64 | |||
65 | View Code Duplication | } else { |
|
66 | // Типичные суффиксы женских фамилий |
||
67 | 1 | if (in_array(S::slice($name, -2), ['ва', 'на', 'ая'], true)) { |
|
68 | return true; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | 3 | return false; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param $name |
||
77 | * @return null|string |
||
78 | */ |
||
79 | 22 | public static function detectGender($name) |
|
91 | |||
92 | /** |
||
93 | * @param $name |
||
94 | * @param null|string $gender |
||
95 | * @return array |
||
96 | */ |
||
97 | 36 | public static function getCases($name, $gender = null) |
|
226 | |||
227 | /** |
||
228 | * @param $name |
||
229 | * @param $case |
||
230 | * @param null $gender |
||
231 | * @return string |
||
232 | * @throws \Exception |
||
233 | */ |
||
234 | 6 | public static function getCase($name, $case, $gender = null) |
|
244 | } |
||
245 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.