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 |
||
| 7 | class NotInRule extends NotRule |
||
| 8 | { |
||
| 9 | /** @var string operator */ |
||
| 10 | const operator = '!in'; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @param string $field The field to apply the rule on. |
||
| 14 | * @param array $value The value the field can equal to. |
||
|
|
|||
| 15 | */ |
||
| 16 | 15 | public function __construct( $field, $possibilities ) |
|
| 20 | |||
| 21 | /** |
||
| 22 | */ |
||
| 23 | 14 | public function isNormalizationAllowed(array $contextual_options=[]) |
|
| 32 | |||
| 33 | /** |
||
| 34 | */ |
||
| 35 | 16 | public function getField() |
|
| 47 | |||
| 48 | /** |
||
| 49 | */ |
||
| 50 | public function setField($field) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | 16 | public function getPossibilities() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | 5 | public function setPossibilities($possibilities) |
|
| 106 | |||
| 107 | /** |
||
| 108 | */ |
||
| 109 | 3 | public function getValues() |
|
| 113 | |||
| 114 | /** |
||
| 115 | */ |
||
| 116 | 2 | public function hasSolution(array $contextual_options=[]) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param array $options + show_instance=false Display the operator of the rule or its instance id |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | 16 | public function toArray(array $options=[]) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @todo cache support |
||
| 151 | */ |
||
| 152 | 1 | View Code Duplication | public function toString(array $options=[]) |
| 167 | |||
| 168 | /**/ |
||
| 169 | } |
||
| 170 |
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.