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 | '<dateformat>' => Processor\DateFormat::class, |
||
30 | |||
31 | // Deprecated |
||
32 | '_ref' => Processor\Reference::class, |
||
33 | '_switch' => Processor\SwitchChoose::class, |
||
34 | '_src' => Processor\Http::class, |
||
35 | '_merge' => Processor\Merge::class, |
||
36 | '_jmespath' => Processor\JmesPath::class, |
||
37 | '_tpl' => Processor\Mustache::class, |
||
38 | '_transformation' => Processor\Transform::class |
||
39 | ]; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Processors, applied in specified order. |
||
44 | * |
||
45 | * @var DataEnricher\Processor[] |
||
46 | */ |
||
47 | public $processors; |
||
48 | |||
49 | |||
50 | /** |
||
51 | * Class constructor |
||
52 | */ |
||
53 | 4 | public function __construct() |
|
63 | |||
64 | /** |
||
65 | * Create processors |
||
66 | * |
||
67 | * @param object $source Data source |
||
68 | * @param array|object $target Target or dot key path |
||
69 | * @return Processor[] |
||
70 | */ |
||
71 | 1 | protected function getProcessorsFor($source, $target) |
|
81 | |||
82 | |||
83 | /** |
||
84 | * Apply processing instructions |
||
85 | * |
||
86 | * @param array|object|string $target Target or dot key path |
||
87 | * @param object $source Data source |
||
88 | */ |
||
89 | 3 | public function applyTo($target, $source = null) |
|
114 | |||
115 | /** |
||
116 | * Find nodes that have processing instructions |
||
117 | * |
||
118 | * @param array|object $target |
||
119 | * @return array |
||
120 | */ |
||
121 | 1 | public function findNodes(&$target) |
|
138 | |||
139 | /** |
||
140 | * Check if object has at leas one process property |
||
141 | * |
||
142 | * @param \stdClass $value |
||
143 | * @param Processor[] $processors |
||
|
|||
144 | * @return boolean |
||
145 | */ |
||
146 | protected function hasProcessorProperty($value) |
||
155 | |||
156 | /** |
||
157 | * Replace nodes with their results |
||
158 | * |
||
159 | * @param array|object $target |
||
160 | */ |
||
161 | 1 | View Code Duplication | protected function applyNodeResults(&$target) |
171 | } |
||
172 |
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.