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>' => Processer\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 | 2 | public function __construct() |
|
54 | { |
||
55 | 2 | foreach (static::$defaultProcessors as $property => $processor) { |
|
56 | if (is_string($processor)) { |
||
57 | $processor = new $processor($property); |
||
58 | } |
||
59 | |||
60 | $this->processors[] = $processor; |
||
61 | } |
||
62 | 2 | } |
|
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 | protected function getProcessorsFor($source, $target) |
||
72 | { |
||
73 | $processors = []; |
||
74 | |||
75 | foreach ($this->processors as $processor) { |
||
76 | $processors[] = $processor->withSourceAndTarget($source, $target); |
||
77 | } |
||
78 | |||
79 | return $processors; |
||
80 | } |
||
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 | 2 | public function applyTo($target, $source = null) |
|
90 | { |
||
91 | 2 | if (!isset($source)) { |
|
92 | 2 | $source = $target; |
|
93 | } |
||
94 | |||
95 | 2 | if (!is_object($source)) { |
|
96 | 2 | throw new \Exception("Data enricher on works on an object, not on a " . gettype($source)); |
|
97 | } |
||
98 | |||
99 | if (is_string($target)) { |
||
100 | $target = DotKey::on($source)->get($target); |
||
101 | } |
||
102 | |||
103 | $nodes = $this->findNodes($target); |
||
104 | $processors = $this->getProcessorsFor($source, $target); |
||
105 | |||
106 | foreach ($nodes as $node) { |
||
107 | foreach ($processors as $processor) { |
||
108 | $node->apply($processor); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | $this->applyNodeResults($target); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Find nodes that have processing instructions |
||
117 | * |
||
118 | * @param array|object $target |
||
119 | * @return array |
||
120 | */ |
||
121 | public function findNodes(&$target) |
||
122 | { |
||
123 | $nodes = []; |
||
124 | |||
125 | foreach ($target as $key => &$value) { |
||
126 | if (is_array($value) || (is_object($value) && !$value instanceof Node)) { |
||
127 | $nodes = array_merge($nodes, $this->findNodes($value)); |
||
128 | } |
||
129 | |||
130 | if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) { |
||
131 | $value = new Node($value); |
||
132 | $nodes[] = $value; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return $nodes; |
||
137 | } |
||
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 | 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.