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 StringLiteral implements ValueObjectInterface |
||
| 9 | { |
||
| 10 | protected $value; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Returns a StringLiteral object given a PHP native string as parameter. |
||
| 14 | * |
||
| 15 | * @param string $value |
||
|
|
|||
| 16 | * @return StringLiteral |
||
| 17 | */ |
||
| 18 | 1 | public static function fromNative() |
|
| 24 | |||
| 25 | /** |
||
| 26 | * Returns a StringLiteral object given a PHP native string as parameter. |
||
| 27 | * |
||
| 28 | * @param string $value |
||
| 29 | */ |
||
| 30 | 6 | public function __construct($value) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Returns the value of the string |
||
| 41 | * |
||
| 42 | * @return string |
||
| 43 | */ |
||
| 44 | 32 | public function toNative() |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Tells whether two string literals are equal by comparing their values |
||
| 51 | * |
||
| 52 | * @param ValueObjectInterface $stringLiteral |
||
| 53 | * @return bool |
||
| 54 | */ |
||
| 55 | 2 | View Code Duplication | public function sameValueAs(ValueObjectInterface $stringLiteral) |
| 63 | |||
| 64 | /** |
||
| 65 | * Tells whether the StringLiteral is empty |
||
| 66 | * |
||
| 67 | * @return bool |
||
| 68 | */ |
||
| 69 | 1 | public function isEmpty() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Returns the string value itself |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | 1 | public function __toString() |
|
| 83 | } |
||
| 84 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.