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 |
||
| 17 | class Character |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Check value to find if it was serialized. |
||
| 21 | * |
||
| 22 | * @param mixed $string Value to check to see if was serialized |
||
| 23 | * |
||
| 24 | * @return bool |
||
| 25 | */ |
||
| 26 | public static function isSerialized($string) |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Check for invalid UTF8 encoding and invalid byte . |
||
| 33 | * |
||
| 34 | * @param string $string Your string. |
||
| 35 | * |
||
| 36 | * @return boolean |
||
| 37 | */ |
||
| 38 | public static function checkUtf8Encoding($string) |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Ensure that a string is ends with a special string. |
||
| 51 | * |
||
| 52 | * <code> |
||
| 53 | * - ensureTrailing('/', 'http://www.example.com') -> 'http://www.example.com/' |
||
| 54 | * - ensureTrailing('/', 'http://www.example.com/') -> 'http://www.example.com/' |
||
| 55 | * </code> |
||
| 56 | * |
||
| 57 | * @param string $needle The needle. |
||
| 58 | * @param string $haystack The haystack. |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | View Code Duplication | public static function ensureTrailing($needle, $haystack) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Ensure that a string is starts with a special string. |
||
| 77 | * |
||
| 78 | * <code> |
||
| 79 | * - ensureLeading('#', '1#2#3#4#5') -> '#1#2#3#4#5' |
||
| 80 | * - ensureLeading('#', '#1#2#3#4#5') -> '#1#2#3#4#5' |
||
| 81 | * </code> |
||
| 82 | * |
||
| 83 | * @param string $needle The needle. |
||
| 84 | * @param string $haystack The haystack |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | View Code Duplication | public static function ensureLeading($needle, $haystack) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Delete trailing characters. |
||
| 103 | * |
||
| 104 | * <code> |
||
| 105 | * - deleteTrailing('|', '|1|2|3|4|5|') -> '|1|2|3|4|5' |
||
| 106 | * - deleteTrailing(array('|','5'), '|1|2|3|4|5|555') -> '|1|2|3|4' |
||
| 107 | * </code> |
||
| 108 | * |
||
| 109 | * @param string|array $needle The needle. |
||
| 110 | * @param string $haystack The haystack. |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | View Code Duplication | public static function deleteTrailing($needle, $haystack) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Delete leading characters. |
||
| 124 | * |
||
| 125 | * <code> |
||
| 126 | * - deleteTrailing('#', '#1#2#3#4#5') -> '1#2#3#4#5' |
||
| 127 | * - deleteTrailing(array('#', '1'), '##11#2#3#4#5') -> '2#3#4#5' |
||
| 128 | * </code> |
||
| 129 | * |
||
| 130 | * @param string|array $needle The needle. |
||
| 131 | * @param string $haystack The haystack. |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | View Code Duplication | public static function deleteLeading($needle, $haystack) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Wrapper for preg_quote supporting strings and array of strings. |
||
| 145 | * |
||
| 146 | * @param mixed $values The values. |
||
| 147 | * @param null|string $delimiter (Optional) The delimiter. |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public static function pregQuote($values, $delimiter = null) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * An aggressive cleaning - all tags and stuff inside will be removed. |
||
| 167 | * |
||
| 168 | * @param string $string The string. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public static function cleanAggressive($string) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Cleans against XSS. |
||
| 179 | * Info: use it on showing your request data. |
||
| 180 | * |
||
| 181 | * @param string $string String to check |
||
| 182 | * @param string $charset Character set (default ISO-8859-1) |
||
| 183 | * |
||
| 184 | * @return string $value Sanitized string |
||
| 185 | */ |
||
| 186 | public static function cleanXss($string, $charset = 'ISO-8859-1') |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param int $length |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public static function random($length = 32) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Determine if a given string contains a given sub-string. |
||
| 205 | * |
||
| 206 | * @param string $haystack |
||
| 207 | * @param string|array $needle |
||
| 208 | * |
||
| 209 | * @return bool |
||
| 210 | */ |
||
| 211 | public static function contains($haystack, $needle) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Determine if a given string begins with a given value. |
||
| 224 | * |
||
| 225 | * @param string $haystack |
||
| 226 | * @param string|array $needle |
||
| 227 | * |
||
| 228 | * @return bool |
||
| 229 | */ |
||
| 230 | public static function startsWith($haystack, $needle) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Determine if a given string ends with a given value. |
||
| 237 | * |
||
| 238 | * @param string $haystack |
||
| 239 | * @param string|array $needle |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | public static function endsWith($haystack, $needle) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Determine if a given string matches a given pattern. |
||
| 250 | * |
||
| 251 | * Asterisks are translated into zero-or-more regular expression wildcards |
||
| 252 | * to make it convenient to check if string such as "library/*". |
||
| 253 | * |
||
| 254 | * @param string $pattern Pattern or wildcard |
||
| 255 | * @param string $value |
||
| 256 | * |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | public static function is($pattern, $value) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Check if strange things happening. |
||
| 272 | * |
||
| 273 | * @param string $path |
||
| 274 | * |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public static function isEvilPath($path) |
||
| 282 | } |
||
| 283 |