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 | '<eval>' => Processor\Evaluate::class, |
||
| 22 | '<switch>' => Processor\SwitchChoose::class, |
||
| 23 | '<merge>' => Processor\Merge::class, |
||
| 24 | '<tpl>' => Processor\Mustache::class, |
||
| 25 | '<src>' => Processor\Http::class, |
||
| 26 | '<jmespath>' => Processor\JmesPath::class, |
||
| 27 | '<apply>' => Processor\JmesPath::class, |
||
| 28 | '<transformation>' => Processor\Transform::class, |
||
| 29 | '<math>' => Processor\Math::class, |
||
| 30 | '<sum>' => Processor\Sum::class, |
||
| 31 | '<enrich>' => Processor\Enrich::class, |
||
| 32 | '<dateformat>' => Processor\DateFormat::class, |
||
| 33 | '<numberformat>' => Processor\NumberFormat::class, |
||
| 34 | '<equal>' => Processor\Equal::class, |
||
| 35 | '<match>' => Processor\Match::class, |
||
| 36 | '<replace>' => Processor\Replace::class, |
||
| 37 | '<if>' => Processor\IfElse::class, |
||
| 38 | '<join>' => Processor\Join::class, |
||
| 39 | '<encode>' => Processor\Encode::class, |
||
| 40 | '<decode>' => Processor\Decode::class, |
||
| 41 | '<serialize>' => Processor\Serialize::class, |
||
| 42 | '<unserialize>' => Processor\Unserialize::class, |
||
| 43 | '<hash>' => Processor\Hash::class, |
||
| 44 | |||
| 45 | // Deprecated |
||
| 46 | '_ref' => Processor\Reference::class, |
||
| 47 | '_switch' => Processor\SwitchChoose::class, |
||
| 48 | '_src' => Processor\Http::class, |
||
| 49 | '_merge' => Processor\Merge::class, |
||
| 50 | '_jmespath' => Processor\JmesPath::class, |
||
| 51 | '_tpl' => Processor\Mustache::class, |
||
| 52 | '_transformation' => Processor\Transform::class |
||
| 53 | ]; |
||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Processors, applied in specified order. |
||
| 58 | * |
||
| 59 | * @var DataEnricher\Processor[] |
||
| 60 | */ |
||
| 61 | public $processors; |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * Class constructor |
||
| 66 | */ |
||
| 67 | 5 | public function __construct() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Create processors |
||
| 80 | * |
||
| 81 | * @param object $source Data source |
||
| 82 | * @param array|object $target Target or dot key path |
||
| 83 | * @return Processor[] |
||
| 84 | */ |
||
| 85 | 2 | protected function getProcessorsFor($source, $target) |
|
| 95 | |||
| 96 | |||
| 97 | /** |
||
| 98 | * Apply processing instructions |
||
| 99 | * |
||
| 100 | * @param array|object|string $target Target or dot key path |
||
| 101 | * @param object $source Data source |
||
| 102 | */ |
||
| 103 | 4 | public function applyTo($target, $source = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Find nodes that have processing instructions |
||
| 131 | * |
||
| 132 | * @param array|object $target |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 2 | public function findNodes(&$target) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Check if object has at leas one process property |
||
| 155 | * |
||
| 156 | * @param \stdClass $value |
||
| 157 | * @param Processor[] $processors |
||
|
|
|||
| 158 | * @return boolean |
||
| 159 | */ |
||
| 160 | protected function hasProcessorProperty($value) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Replace nodes with their results |
||
| 172 | * |
||
| 173 | * @param array|object $target |
||
| 174 | */ |
||
| 175 | 2 | View Code Duplication | protected function applyNodeResults(&$target) |
| 185 | } |
||
| 186 |
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.