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 |
||
| 32 | class Text |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * Converts text separated by a specified separator to camel case. This |
||
| 36 | * function converts the entire text into lower case before performing the |
||
| 37 | * camel case conversion. Due to this the first character would be |
||
| 38 | * lower cased. |
||
| 39 | * |
||
| 40 | * @param string $string The text to be converted. |
||
| 41 | * @param string $separator The separator to consider for camel casing |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | 1 | public static function camelize($string, $separator = '_') |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Converts text separated by a specified separator to camel case. This |
||
| 66 | * method works just as the Text::camelize method except that it converts |
||
| 67 | * the first character to uppercase. |
||
| 68 | * |
||
| 69 | * @param string $string The text to be converted. |
||
| 70 | * @param string $separator The separator to consider for camel casing |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | 1 | public static function ucamelize($string, $separator = '_') |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Converts camel case text into text separated with an arbitrary separator. |
||
| 80 | * |
||
| 81 | * @param string $string The text to be converted. |
||
| 82 | * @param string $separator The separator to be used. |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | 1 | public static function deCamelize($string, $separator = '_') |
|
| 96 | |||
| 97 | public static function pluralize($text) |
||
| 105 | |||
| 106 | public static function singularize($text) |
||
| 115 | } |
||
| 116 |
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.