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 |
||
7 | class S |
||
8 | { |
||
9 | /** @var string Encoding used for string manipulations */ |
||
10 | static protected $encoding; |
||
11 | |||
12 | static protected $cyrillicAlphabet = [ |
||
13 | ['Ё', 'Й', 'Ц', 'У', 'К', 'Е', 'Н', 'Г', 'Ш', 'Щ', 'З', 'Х', 'Ъ', 'Ф', 'Ы', 'В', 'А', 'П', 'Р', 'О', 'Л', 'Д', 'Ж', 'Э', 'Я', 'Ч', 'С', 'М', 'И', 'Т', 'Ь', 'Б', 'Ю'], |
||
14 | ['ё', 'й', 'ц', 'у', 'к', 'е', 'н', 'г', 'ш', 'щ', 'з', 'х', 'ъ', 'ф', 'ы', 'в', 'а', 'п', 'р', 'о', 'л', 'д', 'ж', 'э', 'я', 'ч', 'с', 'м', 'и', 'т', 'ь', 'б', 'ю'], |
||
15 | ]; |
||
16 | |||
17 | /** |
||
18 | * Sets encoding for all operations |
||
19 | * @param string $encoding |
||
20 | * @return bool |
||
21 | */ |
||
22 | public static function setEncoding($encoding) |
||
26 | |||
27 | /** |
||
28 | * Returns encoding used for all operations |
||
29 | * @return string |
||
30 | */ |
||
31 | 1754 | public static function getEncoding() |
|
35 | |||
36 | /** |
||
37 | * Calculates count of characters in string. |
||
38 | * @param $string |
||
39 | * @return bool|int |
||
40 | */ |
||
41 | 38 | public static function length($string) |
|
53 | |||
54 | /** |
||
55 | * Slices string like python. |
||
56 | * @param string $string |
||
57 | * @param int $start |
||
58 | * @param int|null $end |
||
59 | * @return bool|string |
||
60 | */ |
||
61 | 1490 | public static function slice($string, $start, $end = null) |
|
77 | |||
78 | /** |
||
79 | * Lower case. |
||
80 | * @param $string |
||
81 | * @return bool|string |
||
82 | */ |
||
83 | 1746 | View Code Duplication | public static function lower($string) |
91 | |||
92 | /** |
||
93 | * Upper case. |
||
94 | * @param $string |
||
95 | * @return bool|string |
||
96 | */ |
||
97 | 208 | View Code Duplication | public static function upper($string) |
105 | |||
106 | /** |
||
107 | * Name case. (ex: Thomas, Lewis). Works properly with separated by '-' words |
||
108 | * @param $string |
||
109 | * @return bool|string |
||
110 | */ |
||
111 | 206 | public static function name($string) |
|
119 | |||
120 | /** |
||
121 | * multiple substr_count(). |
||
122 | * @param $string |
||
123 | * @param array $chars |
||
124 | * @return bool|int |
||
125 | */ |
||
126 | public static function countChars($string, array $chars) |
||
138 | |||
139 | /** |
||
140 | * @param string $string |
||
141 | * @param string $char |
||
142 | * @return bool|string |
||
143 | */ |
||
144 | 9 | public static function findFirstPosition($string, $char) |
|
152 | |||
153 | /** |
||
154 | * @param string $string |
||
155 | * @param string $char |
||
156 | * @return bool|string |
||
157 | */ |
||
158 | 6 | public static function findLastPosition($string, $char) |
|
166 | |||
167 | /** |
||
168 | * @param $string |
||
169 | * @param array $chars |
||
170 | * @return bool|string |
||
171 | */ |
||
172 | 65 | public static function findLastPositionForOneOfChars($string, array $chars) |
|
173 | { |
||
174 | 65 | if (function_exists('mb_strrpos')) { |
|
175 | 65 | $last_position = false; |
|
176 | 65 | foreach ($chars as $char) { |
|
177 | 65 | if (($pos = mb_strrpos($string, $char, 0, static::getEncoding())) !== false) { |
|
178 | 65 | if ($pos > $last_position) { |
|
179 | 65 | $last_position = $pos; |
|
180 | } |
||
181 | } |
||
182 | } |
||
183 | 65 | if ($last_position !== false) { |
|
184 | 65 | return mb_substr($string, $last_position, null, static::getEncoding()); |
|
185 | } |
||
186 | 10 | return false; |
|
187 | } |
||
188 | |||
189 | return false; |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * @param $string |
||
194 | * @param $substring |
||
195 | * @param bool $caseSensetive |
||
196 | * @param int $startOffset |
||
197 | * @return string|false |
||
198 | */ |
||
199 | 83 | public static function indexOf($string, $substring, $caseSensetive = false, $startOffset = 0) |
|
209 | |||
210 | /** |
||
211 | * @param $string |
||
212 | * @param $fromMap |
||
213 | * @param $toMap |
||
214 | * @return string |
||
215 | */ |
||
216 | private static function replaceByMap($string, $fromMap, $toMap) |
||
229 | } |
||
230 |
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.