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