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 |
||
| 12 | class DataEnricher |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Default processors |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | public static $defaultProcessors = [ |
||
| 19 | '<ifset>' => Processor\IfSet::class, |
||
| 20 | '<ref>' => Processor\Reference::class, |
||
| 21 | '<switch>' => Processor\SwitchChoose::class, |
||
| 22 | '<merge>' => Processor\Merge::class, |
||
| 23 | '<tpl>' => Processor\Mustache::class, |
||
| 24 | '<src>' => Processor\Http::class, |
||
| 25 | '<jmespath>' => Processor\JmesPath::class, |
||
| 26 | '<transformation>' => Processor\Transform::class, |
||
| 27 | |||
| 28 | // Deprecated |
||
| 29 | '_ref' => Processor\Reference::class, |
||
| 30 | '_switch' => Processor\SwitchChoose::class, |
||
| 31 | '_src' => Processor\Http::class, |
||
| 32 | '_merge' => Processor\Merge::class, |
||
| 33 | '_jmespath' => Processor\JmesPath::class, |
||
| 34 | '_tpl' => Processor\Mustache::class, |
||
| 35 | '_transformation' => Processor\Transform::class |
||
| 36 | ]; |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Processors, applied in specified order. |
||
| 41 | * |
||
| 42 | * @var DataEnricher\Processor[] |
||
| 43 | */ |
||
| 44 | public $processors; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Class constructor |
||
| 49 | */ |
||
| 50 | 2 | public function __construct() |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Create processors |
||
| 63 | * |
||
| 64 | * @param object $source Data source |
||
| 65 | * @param array|object $target Target or dot key path |
||
| 66 | * @return Processor[] |
||
| 67 | */ |
||
| 68 | protected function getProcessorsFor($source, $target) |
||
| 78 | |||
| 79 | |||
| 80 | /** |
||
| 81 | * Apply processing instructions |
||
| 82 | * |
||
| 83 | * @param array|object|string $target Target or dot key path |
||
| 84 | * @param object $source Data source |
||
| 85 | */ |
||
| 86 | 2 | public function applyTo($target, $source = null) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Find nodes that have processing instructions |
||
| 114 | * |
||
| 115 | * @param array|object $target |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | public function findNodes(&$target) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check if object has at leas one process property |
||
| 138 | * |
||
| 139 | * @param \stdClass $value |
||
| 140 | * @param Processor[] $processors |
||
|
|
|||
| 141 | * @return boolean |
||
| 142 | */ |
||
| 143 | protected function hasProcessorProperty($value) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Replace nodes with their results |
||
| 155 | * |
||
| 156 | * @param array|object $target |
||
| 157 | */ |
||
| 158 | View Code Duplication | protected function applyNodeResults(&$target) |
|
| 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.