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 | '<math>' => Processor\Math::class, |
||
28 | '<enrich>' => Processor\Enrich::class, |
||
29 | |||
30 | // Deprecated |
||
31 | '_ref' => Processor\Reference::class, |
||
32 | '_switch' => Processor\SwitchChoose::class, |
||
33 | '_src' => Processor\Http::class, |
||
34 | '_merge' => Processor\Merge::class, |
||
35 | '_jmespath' => Processor\JmesPath::class, |
||
36 | '_tpl' => Processor\Mustache::class, |
||
37 | '_transformation' => Processor\Transform::class |
||
38 | ]; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * Processors, applied in specified order. |
||
43 | * |
||
44 | * @var DataEnricher\Processor[] |
||
45 | */ |
||
46 | public $processors; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Class constructor |
||
51 | */ |
||
52 | 2 | public function __construct() |
|
62 | |||
63 | /** |
||
64 | * Create processors |
||
65 | * |
||
66 | * @param object $source Data source |
||
67 | * @param array|object $target Target or dot key path |
||
68 | * @return Processor[] |
||
69 | */ |
||
70 | protected function getProcessorsFor($source, $target) |
||
80 | |||
81 | |||
82 | /** |
||
83 | * Apply processing instructions |
||
84 | * |
||
85 | * @param array|object|string $target Target or dot key path |
||
86 | * @param object $source Data source |
||
87 | */ |
||
88 | 2 | public function applyTo($target, $source = null) |
|
113 | |||
114 | /** |
||
115 | * Find nodes that have processing instructions |
||
116 | * |
||
117 | * @param array|object $target |
||
118 | * @return array |
||
119 | */ |
||
120 | public function findNodes(&$target) |
||
137 | |||
138 | /** |
||
139 | * Check if object has at leas one process property |
||
140 | * |
||
141 | * @param \stdClass $value |
||
142 | * @param Processor[] $processors |
||
|
|||
143 | * @return boolean |
||
144 | */ |
||
145 | protected function hasProcessorProperty($value) |
||
154 | |||
155 | /** |
||
156 | * Replace nodes with their results |
||
157 | * |
||
158 | * @param array|object $target |
||
159 | */ |
||
160 | View Code Duplication | protected function applyNodeResults(&$target) |
|
170 | } |
||
171 |
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.