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 |
||
| 8 | class StringPrimitive implements PrimitiveInterface, StringableInterface |
||
| 9 | { |
||
| 10 | private $value; |
||
| 11 | |||
| 12 | 2 | public function __construct($value) |
|
| 20 | |||
| 21 | /** |
||
| 22 | * {@inheritdoc} |
||
| 23 | */ |
||
| 24 | 1 | public function toPrimitive() |
|
| 28 | |||
| 29 | /** |
||
| 30 | * {@inheritdoc} |
||
| 31 | */ |
||
| 32 | 1 | public function __toString() |
|
| 36 | |||
| 37 | /** |
||
| 38 | * Split the string into a collection of ones |
||
| 39 | * |
||
| 40 | * @param string $delimiter |
||
| 41 | * |
||
| 42 | * @return TypedCollection |
||
| 43 | */ |
||
| 44 | public function split($delimiter) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Returns a collection of the string splitted by the given chunk size |
||
| 57 | * |
||
| 58 | * @param int $size |
||
| 59 | * |
||
| 60 | * @return TypedCollection |
||
| 61 | */ |
||
| 62 | public function chunk($size = 1) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Returns the position of the first occurence of the string |
||
| 74 | * |
||
| 75 | * @param string $needle |
||
| 76 | * @param int $offset |
||
| 77 | * |
||
| 78 | * @throws Exception If the string is not found |
||
| 79 | * |
||
| 80 | * @return IntegerPrimitive |
||
| 81 | */ |
||
| 82 | public function pos($needle, $offset = 0) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Replace all occurences of the search string with the replacement one |
||
| 95 | * |
||
| 96 | * @param string $search |
||
| 97 | * @param string $replacement |
||
| 98 | * |
||
| 99 | * @return StringPrimitive |
||
| 100 | */ |
||
| 101 | public function replace($search, $replacement) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Returns the string following the given delimiter |
||
| 112 | * |
||
| 113 | * @param string $delimiter |
||
| 114 | * |
||
| 115 | * @throws Exception If the string is not found |
||
| 116 | * |
||
| 117 | * @return StringInterface |
||
| 118 | */ |
||
| 119 | public function str($delimiter) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Return the string in upper case |
||
| 132 | * |
||
| 133 | * @return StringPrimitive |
||
| 134 | */ |
||
| 135 | public function toUpper() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Return the string in lower case |
||
| 142 | * |
||
| 143 | * @return StringPrimitive |
||
| 144 | */ |
||
| 145 | public function toLower() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return the string length |
||
| 152 | * |
||
| 153 | * @return IntegerPrimitive |
||
| 154 | */ |
||
| 155 | public function length() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Reverse the string |
||
| 162 | * |
||
| 163 | * @return StringPrimitive |
||
| 164 | */ |
||
| 165 | public function reverse() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Pad the string |
||
| 172 | * |
||
| 173 | * @param int $length |
||
| 174 | * @param string $character |
||
| 175 | * @param int $direction |
||
| 176 | * |
||
| 177 | * @return StringPrimitive |
||
| 178 | */ |
||
| 179 | public function pad($length, $character = ' ', $direction = STR_PAD_RIGHT) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Pad to the right |
||
| 191 | * |
||
| 192 | * @param int $length |
||
| 193 | * @param string $charater |
||
|
|
|||
| 194 | * |
||
| 195 | * @return StringPrimitive |
||
| 196 | */ |
||
| 197 | public function rightPad($length, $character = ' ') |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Pad to the left |
||
| 204 | * |
||
| 205 | * @param int $length |
||
| 206 | * @param string $charater |
||
| 207 | * |
||
| 208 | * @return StringPrimitive |
||
| 209 | */ |
||
| 210 | public function leftPad($length, $character = ' ') |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Pad both sides |
||
| 217 | * |
||
| 218 | * @param int $length |
||
| 219 | * @param string $charater |
||
| 220 | * |
||
| 221 | * @return StringPrimitive |
||
| 222 | */ |
||
| 223 | public function uniPad($length, $character = ' ') |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Find length of initial segment not matching mask |
||
| 230 | * |
||
| 231 | * @param string $mask |
||
| 232 | * @param int $start |
||
| 233 | * @param int $length |
||
| 234 | * |
||
| 235 | * @return IntegerPrimitive |
||
| 236 | */ |
||
| 237 | public function cspn($mask, $start = 0, $length = null) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Repeat the string n times |
||
| 257 | * |
||
| 258 | * @param int $repeat |
||
| 259 | * |
||
| 260 | * @return StringPrimitive |
||
| 261 | */ |
||
| 262 | public function repeat($repeat) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Shuffle the string |
||
| 269 | * |
||
| 270 | * @return StringPrimitive |
||
| 271 | */ |
||
| 272 | public function shuffle() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Strip slashes |
||
| 279 | * |
||
| 280 | * @return StringPrimitive |
||
| 281 | */ |
||
| 282 | public function stripSlashes() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Strip C-like slashes |
||
| 289 | * |
||
| 290 | * @return StringPrimitive |
||
| 291 | */ |
||
| 292 | public function stripCSlashes() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return the word count |
||
| 299 | * |
||
| 300 | * @param string $charlist |
||
| 301 | * |
||
| 302 | * @return IntegerPrimitive |
||
| 303 | */ |
||
| 304 | public function wordCount($charlist = '') |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Return the collection of words |
||
| 315 | * |
||
| 316 | * @param string $charlist |
||
| 317 | * |
||
| 318 | * @return TypedCollection |
||
| 319 | */ |
||
| 320 | public function words($charlist = '') |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Split the string using a regular expression |
||
| 330 | * |
||
| 331 | * @param string $regex |
||
| 332 | * @param int $limit |
||
| 333 | * @param int $flags |
||
| 334 | * |
||
| 335 | * @return TypedCollection |
||
| 336 | */ |
||
| 337 | public function pregSplit($regex, $limit = -1, $flags = 0) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Check if the string match the given regular expression |
||
| 347 | * |
||
| 348 | * @param string $regex |
||
| 349 | * @param int $flags |
||
| 350 | * @param int $offset |
||
| 351 | * |
||
| 352 | * @throws Exception If the regex failed |
||
| 353 | * |
||
| 354 | * @return BooleanPrimitive |
||
| 355 | */ |
||
| 356 | View Code Duplication | public function match($regex, $flags = 0, $offset = 0) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Return a collection of the elements matching the regex |
||
| 369 | * |
||
| 370 | * @param string $regex |
||
| 371 | * @param int $flags |
||
| 372 | * @param int $offset |
||
| 373 | * |
||
| 374 | * @throws Exception If the regex failed |
||
| 375 | * |
||
| 376 | * @return TypedCollection |
||
| 377 | */ |
||
| 378 | View Code Duplication | public function getMatches($regex, $flags = 0, $offset = 0) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Replace part of the string by using a regular expression |
||
| 398 | * |
||
| 399 | * @param string $regex |
||
| 400 | * @param string $replacement |
||
| 401 | * @param int $limit |
||
| 402 | * |
||
| 403 | * @throws Exception If the regex failed |
||
| 404 | * |
||
| 405 | * @return StringPrimitive |
||
| 406 | */ |
||
| 407 | public function pregReplace($regex, $replacement, $limit = -1) |
||
| 422 | } |
||
| 423 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.