DataEnricher   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 174
Duplicated Lines 5.75 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 10
loc 174
ccs 56
cts 58
cp 0.9655
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hasProcessorProperty() 0 9 1
A __construct() 0 10 3
A getProcessorsFor() 0 10 2
B applyTo() 0 25 6
B findNodes() 0 17 7
A applyNodeResults() 10 10 5

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace LegalThings;
4
5
use LegalThings\DataEnricher\Node;
6
use LegalThings\DataEnricher\Processor;
7
use Jasny\DotKey;
8
9
/**
10
 * Enrich objects by processing special properties.
11
 */
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
        '<eval>' => Processor\Evaluate::class,
22
        '<switch>' => Processor\SwitchChoose::class,
23
        '<merge>' => Processor\Merge::class,
24
        '<tpl>' => Processor\Mustache::class,
25
        '<src>' => Processor\Http::class,
26
        '<jmespath>' => Processor\JmesPath::class,
27
        '<apply>' => Processor\JmesPath::class,
28
        '<transformation>' => Processor\Transform::class,
29
        '<math>' => Processor\Math::class,
30
        '<sum>' => Processor\Sum::class,
31
        '<enrich>' => Processor\Enrich::class,
32
        '<dateformat>' => Processor\DateFormat::class,
33
        '<numberformat>' => Processor\NumberFormat::class,
34
        '<equal>' => Processor\Equal::class,
35
        '<match>' => Processor\Match::class,
36
        '<replace>' => Processor\Replace::class,
37
        '<if>' => Processor\IfElse::class,
38
        '<join>' => Processor\Join::class,
39
        '<encode>' => Processor\Encode::class,
40
        '<decode>' => Processor\Decode::class,
41
        '<serialize>' => Processor\Serialize::class,
42
        '<unserialize>' => Processor\Unserialize::class,
43
        '<hash>' => Processor\Hash::class,
44
        
45
        // Deprecated
46
        '_ref' => Processor\Reference::class,
47
        '_switch' => Processor\SwitchChoose::class,
48
        '_src' => Processor\Http::class,
49
        '_merge' => Processor\Merge::class,
50
        '_jmespath' => Processor\JmesPath::class,
51
        '_tpl' => Processor\Mustache::class,
52
        '_transformation' => Processor\Transform::class
53
    ];
54
    
55
    
56
    /**
57
     * Processors, applied in specified order.
58
     * 
59
     * @var DataEnricher\Processor[]
60
     */
61
    public $processors;
62
    
63
    
64
    /**
65
     * Class constructor
66
     */
67 5
    public function __construct()
68
    {
69 5
        foreach (static::$defaultProcessors as $property => $processor) {
70 5
            if (is_string($processor)) {
71 5
                $processor = new $processor($property);
72 5
            }
73
            
74 5
            $this->processors[] = $processor;
75 5
        }
76 5
    }
77
    
78
    /**
79
     * Create processors
80
     * 
81
     * @param object       $source  Data source
82
     * @param array|object $target  Target or dot key path
83
     * @return Processor[]
84
     */
85 2
    protected function getProcessorsFor($source, $target)
86
    {
87 2
        $processors = [];
88
        
89 2
        foreach ($this->processors as $processor) {
90 2
            $processors[] = $processor->withSourceAndTarget($source, $target);
91 2
        }
92
        
93 2
        return $processors;
94
    }
95
    
96
    
97
    /**
98
     * Apply processing instructions
99
     * 
100
     * @param array|object|string $target  Target or dot key path
101
     * @param object              $source  Data source
102
     */
103 4
    public function applyTo($target, $source = null)
104
    {
105 4
        if (!isset($source)) {
106 3
            $source = $target;
107 3
        }
108
        
109 4
        if (!is_object($source)) {
110 2
            throw new \Exception("Data enricher on works on an object, not on a " . gettype($source));
111
        }
112
        
113 2
        if (is_string($target)) {
114
            $target = DotKey::on($source)->get($target);
115
        }
116
        
117 2
        $nodes = $this->findNodes($target);
118 2
        $processors = $this->getProcessorsFor($source, $target);
119
        
120 2
        foreach ($nodes as $node) {
121 2
            foreach ($processors as $processor) {
122 2
                $node->apply($processor);
123 2
            }
124 2
        }
125
        
126 2
        $this->applyNodeResults($target);
127 2
    }
128
129
    /**
130
     * Find nodes that have processing instructions
131
     * 
132
     * @param array|object $target
133
     * @return array
134
     */
135 2
    public function findNodes(&$target)
136
    {
137 2
        $nodes = [];
138
        
139 2
        foreach ($target as $key => &$value) {
140 2
            if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
141 2
                $nodes = array_merge($nodes, $this->findNodes($value));
142 2
            }
143
            
144 2
            if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) {
145 2
                $value = new Node($value);
146 2
                $nodes[] = $value;
147 2
            }
148 2
        }
149
        
150 2
        return $nodes;
151
    }
152
    
153
    /**
154
     * Check if object has at leas one process property
155
     * 
156
     * @param \stdClass    $value
157
     * @param Processor[]  $processors
0 ignored issues
show
Bug introduced by
There is no parameter named $processors. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
158
     * @return boolean
159
     */
160
    protected function hasProcessorProperty($value)
161
    {
162 2
        $processorProps = array_map(function ($processor) {
163 2
            return $processor->getProperty();
164 2
        }, $this->processors);
165
        
166 2
        $valueProps = array_keys(get_object_vars($value));
167 2
        return count(array_intersect($valueProps, $processorProps)) > 0;
168
    }
169
    
170
    /**
171
     * Replace nodes with their results
172
     * 
173
     * @param array|object $target
174
     */
175 2 View Code Duplication
    protected function applyNodeResults(&$target)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
176
    {
177 2
        foreach ($target as &$value) {
178 2
            if ($value instanceof Node) {
179 2
                $value = $value->getResult();
180 2
            } elseif (is_array($value) || is_object($value)) {
181 2
                $this->applyNodeResults($value);
182 2
            }
183 2
        }
184 2
    }
185
}
186