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 |
||
5 | class StringUtils extends UtilsBase |
||
6 | { |
||
7 | const CASE_INSENSITIVE = 1; |
||
8 | const ACCENT_INSENSITIVE = 2; |
||
9 | |||
10 | private $accentsMap = array( |
||
11 | 'Š' => 'S', |
||
12 | 'š' => 's', |
||
13 | 'Ž' => 'Z', |
||
14 | 'ž' => 'z', |
||
15 | 'À' => 'A', |
||
16 | 'Á' => 'A', |
||
17 | 'Â' => 'A', |
||
18 | 'Ã' => 'A', |
||
19 | 'Ä' => 'A', |
||
20 | 'Å' => 'A', |
||
21 | 'Æ' => 'A', |
||
22 | 'Ç' => 'C', |
||
23 | 'È' => 'E', |
||
24 | 'É' => 'E', |
||
25 | 'Ê' => 'E', |
||
26 | 'Ë' => 'E', |
||
27 | 'Ì' => 'I', |
||
28 | 'Í' => 'I', |
||
29 | 'Î' => 'I', |
||
30 | 'Ï' => 'I', |
||
31 | 'Ñ' => 'N', |
||
32 | 'Ò' => 'O', |
||
33 | 'Ó' => 'O', |
||
34 | 'Ô' => 'O', |
||
35 | 'Õ' => 'O', |
||
36 | 'Ö' => 'O', |
||
37 | 'Ø' => 'O', |
||
38 | 'Ù' => 'U', |
||
39 | 'Ú' => 'U', |
||
40 | 'Û' => 'U', |
||
41 | 'Ü' => 'U', |
||
42 | 'Ý' => 'Y', |
||
43 | 'Þ' => 'B', |
||
44 | 'ß' => 'Ss', |
||
45 | 'à' => 'a', |
||
46 | 'á' => 'a', |
||
47 | 'â' => 'a', |
||
48 | 'ã' => 'a', |
||
49 | 'ä' => 'a', |
||
50 | 'å' => 'a', |
||
51 | 'æ' => 'a', |
||
52 | 'ç' => 'c', |
||
53 | 'è' => 'e', |
||
54 | 'é' => 'e', |
||
55 | 'ê' => 'e', |
||
56 | 'ë' => 'e', |
||
57 | 'ì' => 'i', |
||
58 | 'í' => 'i', |
||
59 | 'î' => 'i', |
||
60 | 'ï' => 'i', |
||
61 | 'ð' => 'o', |
||
62 | 'ñ' => 'n', |
||
63 | 'ò' => 'o', |
||
64 | 'ó' => 'o', |
||
65 | 'ô' => 'o', |
||
66 | 'õ' => 'o', |
||
67 | 'ö' => 'o', |
||
68 | 'ø' => 'o', |
||
69 | 'ù' => 'u', |
||
70 | 'ú' => 'u', |
||
71 | 'û' => 'u', |
||
72 | 'ý' => 'y', |
||
73 | 'þ' => 'b', |
||
74 | 'ÿ' => 'y' |
||
75 | ); |
||
76 | |||
77 | private $urlRegExp; |
||
78 | private $emailRegExp; |
||
79 | |||
80 | protected function __construct() |
||
89 | |||
90 | /** |
||
91 | * @param string $haystack |
||
92 | * @param string $needle |
||
93 | * @param int|null $modifiers |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | View Code Duplication | public function beginsWith($haystack, $needle, $modifiers = null) |
|
106 | |||
107 | /** |
||
108 | * @param string $string |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function removeAccents($string) |
||
116 | |||
117 | /** |
||
118 | * @param string $haystack |
||
119 | * @param string $needle |
||
120 | * @param int|null $modifiers |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | View Code Duplication | public function endsWith($haystack, $needle, $modifiers = null) |
|
133 | |||
134 | /** |
||
135 | * @param string $string |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | public function removeExtraSpaces($string) |
||
143 | |||
144 | /** |
||
145 | * @param string $string |
||
146 | * |
||
147 | * @return bool |
||
148 | */ |
||
149 | public function isUrl($string) |
||
153 | |||
154 | /** |
||
155 | * @param string $string |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function isEmail($string) |
||
163 | |||
164 | private function parseModifiers($modifiers) |
||
174 | |||
175 | private function isAccentInsensitiveModifier($modifiers) |
||
179 | |||
180 | private function prepareHaystackAndNeedle(&$haystack, &$needle, $modifiers) |
||
187 | } |
||
188 |
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.