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() |
|
| 54 | { |
||
| 55 | 4 | foreach (static::$defaultProcessors as $property => $processor) { |
|
| 56 | 4 | if (is_string($processor)) { |
|
| 57 | 4 | $processor = new $processor($property); |
|
| 58 | } |
||
| 59 | |||
| 60 | 4 | $this->processors[] = $processor; |
|
| 61 | } |
||
| 62 | 4 | } |
|
| 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) |
|
| 72 | { |
||
| 73 | 1 | $processors = []; |
|
| 74 | |||
| 75 | 1 | foreach ($this->processors as $processor) { |
|
| 76 | 1 | $processors[] = $processor->withSourceAndTarget($source, $target); |
|
| 77 | } |
||
| 78 | |||
| 79 | 1 | 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 | 3 | public function applyTo($target, $source = null) |
|
| 90 | { |
||
| 91 | 3 | if (!isset($source)) { |
|
| 92 | 3 | $source = $target; |
|
| 93 | } |
||
| 94 | |||
| 95 | 3 | if (!is_object($source)) { |
|
| 96 | 2 | throw new \Exception("Data enricher on works on an object, not on a " . gettype($source)); |
|
| 97 | } |
||
| 98 | |||
| 99 | 1 | if (is_string($target)) { |
|
| 100 | $target = DotKey::on($source)->get($target); |
||
| 101 | } |
||
| 102 | |||
| 103 | 1 | $nodes = $this->findNodes($target); |
|
| 104 | 1 | $processors = $this->getProcessorsFor($source, $target); |
|
| 105 | |||
| 106 | 1 | foreach ($nodes as $node) { |
|
| 107 | 1 | foreach ($processors as $processor) { |
|
| 108 | 1 | $node->apply($processor); |
|
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | 1 | $this->applyNodeResults($target); |
|
| 113 | 1 | } |
|
| 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) |
|
| 122 | { |
||
| 123 | 1 | $nodes = []; |
|
| 124 | |||
| 125 | 1 | foreach ($target as $key => &$value) { |
|
| 126 | 1 | if (is_array($value) || (is_object($value) && !$value instanceof Node)) { |
|
| 127 | 1 | $nodes = array_merge($nodes, $this->findNodes($value)); |
|
| 128 | } |
||
| 129 | |||
| 130 | 1 | if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) { |
|
| 131 | 1 | $value = new Node($value); |
|
| 132 | 1 | $nodes[] = $value; |
|
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | 1 | 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 | 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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.