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 |
||
15 | class StringHelper { |
||
16 | |||
17 | /** |
||
18 | * Returns a string with the first character of str capitalized, if that |
||
19 | * character is alphabetic. |
||
20 | * |
||
21 | * @param string The input string. |
||
22 | * @param string $encoding The encoding parameter is the character encoding. |
||
23 | * If it is omitted, the internal character encoding value will |
||
24 | * be used. |
||
25 | * |
||
26 | * @return string Returns the resulting string. |
||
27 | */ |
||
28 | View Code Duplication | public static function upperCaseFirst($str, $encoding = 'utf8') { |
|
39 | |||
40 | /** |
||
41 | * Returns a string with the first character of str lowercased, if that |
||
42 | * character is alphabetic. |
||
43 | * |
||
44 | * @param string The input string. |
||
45 | * @param string $encoding The encoding parameter is the character encoding. |
||
46 | * If it is omitted, the internal character encoding value will |
||
47 | * be used. |
||
48 | * |
||
49 | * @return string Returns the resulting string. |
||
50 | */ |
||
51 | View Code Duplication | public static function lowerCaseFirst($str, $encoding = 'utf8') { |
|
62 | |||
63 | /** |
||
64 | * Returns a string with the first character of each word in str capitalized, |
||
65 | * if that character is alphabetic. |
||
66 | * |
||
67 | * @param string The input string. |
||
68 | * @param string $encoding The encoding parameter is the character encoding. |
||
69 | * If it is omitted, the internal character encoding value will |
||
70 | * be used. |
||
71 | * |
||
72 | * @return string Returns the resulting string. |
||
73 | */ |
||
74 | public static function upperCaseWords($str, $encoding = 'utf8') { |
||
81 | |||
82 | /** |
||
83 | * Returns string with all alphabetic characters converted to uppercase. |
||
84 | * |
||
85 | * @param string The input string. |
||
86 | * @param string $encoding The encoding parameter is the character encoding. |
||
87 | * If it is omitted, the internal character encoding value will |
||
88 | * be used. |
||
89 | * |
||
90 | * @return string Returns the resulting string. |
||
91 | */ |
||
92 | public static function upperCase($str, $encoding = 'utf8') { |
||
99 | |||
100 | /** |
||
101 | * Returns string with all alphabetic characters converted to lowercase. |
||
102 | * |
||
103 | * @param string The input string. |
||
104 | * @param string $encoding The encoding parameter is the character encoding. |
||
105 | * If it is omitted, the internal character encoding value will |
||
106 | * be used. |
||
107 | * |
||
108 | * @return string Returns the resulting string. |
||
109 | */ |
||
110 | public static function lowerCase($str, $encoding = 'utf8') { |
||
117 | |||
118 | } |
||
119 |
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.