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:
Complex classes like StringUtil often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StringUtil, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class StringUtil |
||
16 | { |
||
17 | /** |
||
18 | * Addslashes for any string|array, recursive |
||
19 | * |
||
20 | * @param mixed $source |
||
21 | * @return mixed |
||
22 | */ |
||
23 | View Code Duplication | public function addSlashesRecursive($source) |
|
42 | |||
43 | |||
44 | /** |
||
45 | * Encode string for html output |
||
46 | * |
||
47 | * @param string $str |
||
48 | * @param boolean $stripSlashes |
||
49 | * @param boolean $nl2br |
||
50 | * @param boolean $optimizeSpaces |
||
51 | * @return string |
||
52 | */ |
||
53 | public function encodeHtml( |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Encode array of string for html output |
||
84 | * |
||
85 | * @param array $stringArray |
||
86 | * @param boolean $stripSlashes |
||
87 | * @param boolean $nl2br |
||
88 | * @param boolean $optimizeSpaces |
||
89 | * @return string |
||
90 | */ |
||
91 | public function encodeHtmls( |
||
109 | |||
110 | |||
111 | /** |
||
112 | * Indent a string |
||
113 | * |
||
114 | * The first line will also be indented. |
||
115 | * |
||
116 | * Commonly used in generate and combine html, fix indents. |
||
117 | * |
||
118 | * The $spacer should have width 1, if not, the real indent width is |
||
119 | * mb_strwidth($spacer) * $width . |
||
120 | * |
||
121 | * @param string $str |
||
122 | * @param int $width Must > 0 |
||
123 | * @param string $spacer Which char is used to indent |
||
124 | * @param string $lineEnding Original string's line ending |
||
125 | * @param bool $fillEmptyLine Add spacer to empty line ? |
||
126 | * @return string |
||
127 | */ |
||
128 | public function indent( |
||
149 | |||
150 | |||
151 | /** |
||
152 | * Indent a html string, except value of some html tag like textarea |
||
153 | * |
||
154 | * Html string should have both start and end tag of html tag. |
||
155 | * |
||
156 | * Works for html tag: |
||
157 | * - textarea |
||
158 | |||
159 | * @param string $html |
||
160 | * @param int $width Must > 0 |
||
161 | * @param string $spacer Which char is used to indent |
||
162 | * @param string $lineEnding Original string's line ending |
||
163 | * @param bool $fillEmptyLine Add spacer to empty line ? |
||
164 | * @return string |
||
165 | */ |
||
166 | public function indentHtml( |
||
223 | |||
224 | |||
225 | /** |
||
226 | * Match content using preg, return result array or string |
||
227 | * |
||
228 | * Return value maybe string or array, use with caution. |
||
229 | * |
||
230 | * @param string $preg |
||
231 | * @param string $str |
||
232 | * @param boolean $simple Convert single result to str(array -> str) ? |
||
233 | * @return string|array|null |
||
234 | */ |
||
235 | public function matchRegex($preg, $str = '', $simple = true) |
||
265 | |||
266 | |||
267 | /** |
||
268 | * Match a string with rule including wildcard |
||
269 | * |
||
270 | * Wildcard '*' means any number of chars, and '?' means EXACTLY one char. |
||
271 | * |
||
272 | * Eg: 'duck' match rule '*c?' |
||
273 | * |
||
274 | * @param string $str |
||
275 | * @param string $rule |
||
276 | * @return boolean |
||
277 | */ |
||
278 | View Code Duplication | public function matchWildcard($str, $rule) |
|
294 | |||
295 | |||
296 | /** |
||
297 | * Generate random string |
||
298 | * |
||
299 | * In $mode: |
||
300 | * a means include a-z |
||
301 | * A means include A-Z |
||
302 | * 0 means include 0-9 |
||
303 | * |
||
304 | * @param int $len |
||
305 | * @param string $mode |
||
306 | * @return string |
||
307 | */ |
||
308 | public function random($len, $mode = 'a0') |
||
332 | |||
333 | |||
334 | /** |
||
335 | * Get substr by display width, and ignore html tag's length |
||
336 | * |
||
337 | * Using mb_strimwidth() |
||
338 | * |
||
339 | * Notice: No consider of html complement, all html tag treat as zero |
||
340 | * length. |
||
341 | * |
||
342 | * Notice: Self close tag need use style as <br />, not <br>, for correct |
||
343 | * html tag depth compute. |
||
344 | * |
||
345 | * @param string $str Source string |
||
346 | * @param int $length Length |
||
347 | * @param string $marker If str length exceed, cut & fill with this |
||
348 | * @param string $encoding Default is utf-8 |
||
349 | * @return string |
||
350 | * @link http://www.fwolf.com/blog/post/133 |
||
351 | */ |
||
352 | public function substrIgnoreHtml( |
||
445 | |||
446 | |||
447 | /** |
||
448 | * Convert string to array by splitter |
||
449 | * |
||
450 | * @param string $source |
||
451 | * @param string $splitter |
||
452 | * @param boolean $trim |
||
453 | * @param boolean $removeEmpty |
||
454 | * @return array |
||
455 | */ |
||
456 | public function toArray( |
||
487 | |||
488 | |||
489 | /** |
||
490 | * Convert to camelCase |
||
491 | * |
||
492 | * @param string $source |
||
493 | * @return string |
||
494 | */ |
||
495 | public function toCamelCase($source) |
||
499 | |||
500 | |||
501 | /** |
||
502 | * Convert to snake case |
||
503 | * |
||
504 | * @param string $source |
||
505 | * @param string $separator |
||
506 | * @param boolean $ucfirstWords |
||
507 | * @return string |
||
508 | */ |
||
509 | public function toSnakeCase( |
||
534 | |||
535 | |||
536 | /** |
||
537 | * Convert to StudlyCaps |
||
538 | * |
||
539 | * @param string $source |
||
540 | * @return string |
||
541 | */ |
||
542 | public function toStudlyCaps($source) |
||
546 | } |
||
547 |
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.