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