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 |
||
| 16 | class SimpleHydratingHandler implements HydratingHandlerInterface |
||
| 17 | { |
||
| 18 | /** @var string */ |
||
| 19 | private $key; |
||
| 20 | public function getKey() { return $this->key; } |
||
| 22 | |||
| 23 | /** @var ParserInterface */ |
||
| 24 | private $parser; |
||
| 25 | public function getParser() { return $this->parser; } |
||
| 27 | |||
| 28 | /** @var ValidatorInterface[] */ |
||
| 29 | private $validators; |
||
| 30 | public function getValidators() { return $this->validators; } |
||
| 33 | |||
| 34 | /** @var mixed */ |
||
| 35 | private $defaultValue; |
||
| 36 | public function getDefaultValue() { return $this->defaultValue; } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * SimpleHydratingHandler constructor. |
||
| 41 | * @param string $key |
||
| 42 | * @param ParserInterface $parser |
||
| 43 | * @param ValidatorInterface[] $validators |
||
| 44 | * @param mixed $default |
||
|
|
|||
| 45 | */ |
||
| 46 | public function __construct(string $key, ParserInterface $parser, array $validators = [], $defaultValue = null) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param array $data |
||
| 56 | * @param array $targetData |
||
| 57 | * @param $object |
||
| 58 | * |
||
| 59 | * @throws HydratingException |
||
| 60 | */ |
||
| 61 | public function handle(array $data, array &$targetData, $object = null) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param mixed $parsedValue |
||
| 84 | * @param mixed $contextObject |
||
| 85 | * |
||
| 86 | * @throws HydratingException |
||
| 87 | */ |
||
| 88 | View Code Duplication | private function validate($parsedValue, $contextObject = null) |
|
| 98 | } |
||
| 99 |
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.