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 S |
||
10 | { |
||
11 | /** @var string Encoding used for string manipulations */ |
||
12 | static protected $encoding; |
||
13 | |||
14 | /** @var string[][] */ |
||
15 | static protected $cyrillicAlphabet = [ |
||
16 | ['Ё', 'Й', 'Ц', 'У', 'К', 'Е', 'Н', 'Г', 'Ш', 'Щ', 'З', 'Х', 'Ъ', 'Ф', 'Ы', 'В', 'А', 'П', 'Р', 'О', 'Л', 'Д', 'Ж', 'Э', 'Я', 'Ч', 'С', 'М', 'И', 'Т', 'Ь', 'Б', 'Ю'], |
||
17 | ['ё', 'й', 'ц', 'у', 'к', 'е', 'н', 'г', 'ш', 'щ', 'з', 'х', 'ъ', 'ф', 'ы', 'в', 'а', 'п', 'р', 'о', 'л', 'д', 'ж', 'э', 'я', 'ч', 'с', 'м', 'и', 'т', 'ь', 'б', 'ю'], |
||
18 | ]; |
||
19 | |||
20 | /** |
||
21 | * Sets encoding for all operations |
||
22 | * @param string $encoding |
||
23 | * @return void |
||
24 | */ |
||
25 | public static function setEncoding($encoding) |
||
29 | |||
30 | /** |
||
31 | * Returns encoding used for all operations |
||
32 | * @return string |
||
33 | */ |
||
34 | 1767 | public static function getEncoding() |
|
38 | |||
39 | /** |
||
40 | * Calculates count of characters in string. |
||
41 | * @param string $string |
||
42 | * @return int|false |
||
43 | */ |
||
44 | 47 | public static function length($string) |
|
56 | |||
57 | /** |
||
58 | * Slices string like python. |
||
59 | * @param string $string |
||
60 | * @param int $start |
||
61 | * @param int|null $end |
||
62 | * @return string |
||
63 | */ |
||
64 | 1503 | public static function slice($string, $start, $end = null) |
|
80 | |||
81 | /** |
||
82 | * Lower case. |
||
83 | * @param string $string |
||
84 | * @return string |
||
85 | */ |
||
86 | 1758 | View Code Duplication | public static function lower($string) |
94 | |||
95 | /** |
||
96 | * Upper case. |
||
97 | * @param string $string |
||
98 | * @return bool|string |
||
99 | */ |
||
100 | 220 | View Code Duplication | public static function upper($string) |
108 | |||
109 | /** |
||
110 | * Name case. (ex: Thomas, Lewis). Works properly with separated by '-' words |
||
111 | * @param string $string |
||
112 | * @return bool|string |
||
113 | */ |
||
114 | 218 | public static function name($string) |
|
122 | |||
123 | /** |
||
124 | * multiple substr_count(). |
||
125 | * @param string $string |
||
126 | * @param string[] $chars |
||
127 | * @return int |
||
128 | */ |
||
129 | public static function countChars($string, array $chars) |
||
141 | |||
142 | /** |
||
143 | * @param string $string |
||
144 | * @param string $char |
||
145 | * @return int|false |
||
146 | */ |
||
147 | 12 | public static function findFirstPosition($string, $char) |
|
155 | |||
156 | /** |
||
157 | * @param string $string |
||
158 | * @param string $char |
||
159 | * @return int|false |
||
160 | */ |
||
161 | 6 | public static function findLastPosition($string, $char) |
|
169 | |||
170 | /** |
||
171 | * @param string $string |
||
172 | * @param string[] $chars |
||
173 | * @return string|false |
||
174 | */ |
||
175 | 79 | public static function findLastPositionForOneOfChars($string, array $chars) |
|
194 | |||
195 | /** |
||
196 | * @param string $string |
||
197 | * @param string $substring |
||
198 | * @param bool $caseSensitive |
||
199 | * @param int $startOffset |
||
200 | * @return int|false |
||
201 | */ |
||
202 | 120 | public static function indexOf($string, $substring, $caseSensitive = false, $startOffset = 0) |
|
212 | |||
213 | /** |
||
214 | * @param string $string |
||
215 | * @param string[] $fromMap |
||
216 | * @param string[] $toMap |
||
217 | * @return string |
||
218 | */ |
||
219 | private static function replaceByMap($string, $fromMap, $toMap) |
||
232 | |||
233 | /** |
||
234 | * Check that string has one of passed substrings |
||
235 | * @param string $string |
||
236 | * @param string[] $variants |
||
237 | * @return bool |
||
238 | */ |
||
239 | 3 | public static function stringContains($string, array $variants) |
|
248 | } |
||
249 |
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.