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 | 26 | public function __construct( $field, $possibilities ) |
|
| 20 | |||
| 21 | /** |
||
| 22 | */ |
||
| 23 | 14 | public function isNormalizationAllowed(array $contextual_options=[]) |
|
| 32 | |||
| 33 | /** |
||
| 34 | */ |
||
| 35 | 25 | public function getField() |
|
| 36 | { |
||
| 37 | 25 | if (! $this->getOperandAt(0)) { |
|
| 38 | // TODO a NotRule with no operand should be simplified as |
||
| 39 | // a TrueRule |
||
| 40 | 1 | throw new \LogicException( |
|
| 41 | "Trying to get the field of an InRule negation missing its operand" |
||
| 42 | 1 | ); |
|
| 43 | } |
||
| 44 | |||
| 45 | 24 | return $this->getOperandAt(0)->getField(); |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | */ |
||
| 50 | 2 | public function setField($field) |
|
| 51 | { |
||
| 52 | 2 | if (! $this->getOperandAt(0)) { |
|
| 53 | // TODO a NotRule with no operand should be simplified as |
||
| 54 | // a TrueRule |
||
| 55 | 1 | throw new \LogicException( |
|
| 56 | "Trying to set the field of an InRule negation missing its operand" |
||
| 57 | 1 | ); |
|
| 58 | } |
||
| 59 | |||
| 60 | 1 | return $this->getOperandAt(0)->setField($field); |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | 23 | public function getPossibilities() |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | 7 | public function setPossibilities($possibilities) |
|
| 105 | |||
| 106 | /** |
||
| 107 | */ |
||
| 108 | 3 | public function getValues() |
|
| 112 | |||
| 113 | /** |
||
| 114 | */ |
||
| 115 | 3 | public function hasSolution(array $contextual_options=[]) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param array $options + show_instance=false Display the operator of the rule or its instance id |
||
| 122 | * |
||
| 123 | * @return array |
||
| 124 | */ |
||
| 125 | 23 | public function toArray(array $options=[]) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @todo cache support |
||
| 150 | */ |
||
| 151 | 1 | View Code Duplication | public function toString(array $options=[]) |
| 166 | |||
| 167 | /**/ |
||
| 168 | } |
||
| 169 |
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.