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 | private static $pluralRules = [ |
||
| 35 | ['/child/', 'ren'], |
||
| 36 | ['/^ox$/', 'en'], |
||
| 37 | ['/(.*)(a|e|i|o|u)(?<remove>y)$/', 'ys'], |
||
| 38 | ['/(.*)(?<remove>y)$/', 'ies'], |
||
| 39 | ['/(foc|alumn|fung|nucle|octop|radi|syllab)(?<remove>us)$/', 'i'], |
||
| 40 | ['/(.*)(d|r)(?<remove>ex|ix)$/', 'ices'], |
||
| 41 | ['/(.*)(s|x)(?<remove>is)$/', 'es'], |
||
| 42 | ['/(.*)(?<remove>sh)$/', 'shes'], |
||
| 43 | ['/(.*)(?<remove>eau)$/', 'eaux'], |
||
| 44 | ['/(.*)(?<remove>um)$/', 'a'], |
||
| 45 | ['/(.*)(?<remove>tooth)$/', 'teeth'], |
||
| 46 | ['/(.*)(?<remove>h)$/', 'hes'], |
||
| 47 | ['/(formul|alumn|nebul)(?<remove>a)$/', 'ae'], |
||
| 48 | ['/(.*)(?<remove>x)$/', 'xes'], |
||
| 49 | ['/(.+)(?<remove>ion)$/', 'ia'], |
||
| 50 | ['/(.*)(?<remove>roof)$/', 'roofs'], |
||
| 51 | ['/(.*)[^f](?<remove>f|fe)$/', 'ves'], |
||
| 52 | ['/(.*)(m|l)(?<remove>ouse)$/', 'ice'], |
||
| 53 | ['/(.*)(?<remove>man)$/', 'men'], |
||
| 54 | ['/(.*)(?<remove>foot)$/', 'feet'], |
||
| 55 | ['/(.*)(disc|phot|pian)(?<remove>o)$/', 'os'], |
||
| 56 | ['/(.*)(?<remove>goose)$/', 'geese'], |
||
| 57 | ['/(.*)(?<remove>person)$/', 'people'], |
||
| 58 | ['/(.*)(?<remove>quiz)$/', 'quizzes'], |
||
| 59 | ['/.*(s|o|z)$/', 'es'], |
||
| 60 | ['/.*/', 's'] |
||
| 61 | ]; |
||
| 62 | |||
| 63 | private static $singularRules = [ |
||
| 64 | ['/^axe(?<remove>s)$/', ''], |
||
| 65 | ['/(.*)(?<remove>a)$/', 'um'], |
||
| 66 | ['/(.*)(dev|v|pr)(?<remove>ices)$/', 'ice'], |
||
| 67 | ['/(.*)(?<remove>ices)$/', 'ix'], |
||
| 68 | ['/(.*)(?<remove>movies)$/', 'movie'], |
||
| 69 | ['/(.*)(?<remove>ies)$/', 'y'], |
||
| 70 | ['/(.*)(?<remove>shoes)$/', 'shoe'], |
||
| 71 | ['/(.*)(?<remove>oes)$/', 'o'], |
||
| 72 | ['/(.*)(?<remove>bases)$/', 'base'], |
||
| 73 | ['/(.*)(?<remove>cheeses)$/', 'cheese'], |
||
| 74 | ['/(.*)(?<remove>children)$/', 'child'], |
||
| 75 | ['/(.*)(?<remove>men)$/', 'man'], |
||
| 76 | ['/(.*)(?<remove>feet)$/', 'foot'], |
||
| 77 | ['/(.*)(?<remove>geese)$/', 'goose'], |
||
| 78 | ['/(.*)(?<remove>atlases)$/', 'atlas'], |
||
| 79 | ['/(.*)(?<remove>people)$/', 'person'], |
||
| 80 | ['/(.*)(?<remove>teeth)$/', 'tooth'], |
||
| 81 | ['/(.*)(iri)(?<remove>ses)$/', 's'], |
||
| 82 | ['/(.*)(h|l|p)(ou)(?<remove>ses)$/', 'se'], |
||
| 83 | ['/(.*)(ro|po|ca)(?<remove>ses)$/', 'se'], |
||
| 84 | ['/(.*)(?<remove>quizzes)$/', 'quiz'], |
||
| 85 | ['/(.*)(?<remove>zes)$/', 'z'], |
||
| 86 | ['/(.*)(y|i|a|o|e)(?<remove>ses)$/', 'sis'], |
||
| 87 | ['/(.*)(?<remove>ses)$/', 's'], |
||
| 88 | ['/(.*)(?<remove>ice)$/', 'ouse'], |
||
| 89 | ['/(.*)(?<remove>xes)$/', 'x'], |
||
| 90 | ['/(.*)(?<remove>eaux)$/', 'eau'], |
||
| 91 | ['/(formul|alumn|nebul)(?<remove>ae)$/', 'a'], |
||
| 92 | ['/(foc|alumn|fung|nucle|octop|radi|syllab)(?<remove>i)$/', 'us'], |
||
| 93 | ['/(.*)(?<remove>hes)$/', 'h'], |
||
| 94 | ['/(.*)(ca|mo|lo)(?<remove>ves)$/', 've'], |
||
| 95 | ['/(.*)(l|r|o|a|e)(?<remove>ves)$/', 'f'], |
||
| 96 | ['/(.*)(li|ni|wi)(?<remove>ves)$/', 'fe'], |
||
| 97 | ['/(.*)(?<remove>s)$/', ''], |
||
| 98 | ]; |
||
| 99 | |||
| 100 | private static $noPlurals = [ |
||
| 101 | 'cod', 'deer', 'feedback', 'fish', 'moose', 'news', 'species', 'series', 'sheep', 'rice' |
||
| 102 | ]; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Converts text separated by a specified separator to camel case. |
||
| 106 | * This function converts the entire text into lower case before performing the |
||
| 107 | * camel case conversion. Due to this the first character would be lowercased. |
||
| 108 | * |
||
| 109 | * @param string $string The text to be converted. |
||
| 110 | * @param string $separator The separator to consider for camel casing |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | 1 | public static function camelize($string, $separator = '_') : string |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Converts text separated by a specified separator to camel case. |
||
| 135 | * This method works just as the Text::camelize method except that it converts |
||
| 136 | * the first character to uppercase. |
||
| 137 | * |
||
| 138 | * @param string $string The text to be converted. |
||
| 139 | * @param string $separator The separator to consider for camel casing |
||
| 140 | * @return string |
||
| 141 | */ |
||
| 142 | 1 | public static function ucamelize($string, $separator = '_') : string |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Converts camel case text into regular text separated with an arbitrary separator. |
||
| 149 | * By default the seperator is an underscore. A space can also be used as the |
||
| 150 | * seperator in cases where the conversion is to an English sentence. |
||
| 151 | * |
||
| 152 | * @param string $string The text to be converted. |
||
| 153 | * @param string $separator The separator to be used. |
||
| 154 | * @return string |
||
| 155 | */ |
||
| 156 | 1 | public static function deCamelize($string, $separator = '_') : string |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Generates the english plural of a given word. |
||
| 170 | * |
||
| 171 | * @param string $text |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | 166 | View Code Duplication | public static function pluralize($text) : string |
| 185 | |||
| 186 | /** |
||
| 187 | * Generates the english singular of a given word. |
||
| 188 | * |
||
| 189 | * @param string $text |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | 154 | View Code Duplication | public static function singularize($text) : string |
| 204 | } |
||
| 205 |
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.