Completed
Pull Request — master (#10)
by Arnold
05:19
created

DataEnricher   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 157
Duplicated Lines 6.37 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 17.78%

Importance

Changes 10
Bugs 1 Features 5
Metric Value
wmc 24
c 10
b 1
f 5
lcom 1
cbo 2
dl 10
loc 157
ccs 8
cts 45
cp 0.1778
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A getProcessorsFor() 0 10 2
B applyTo() 0 25 6
B findNodes() 0 17 7
A hasProcessorProperty() 0 9 1
B 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
        '<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
        
28
        // Deprecated
29
        '_ref' => Processor\Reference::class,
30
        '_switch' => Processor\SwitchChoose::class,
31
        '_src' => Processor\Http::class,
32
        '_merge' => Processor\Merge::class,
33
        '_jmespath' => Processor\JmesPath::class,
34
        '_tpl' => Processor\Mustache::class,
35
        '_transformation' => Processor\Transform::class
36
    ];
37
    
38
    
39
    /**
40
     * Processors, applied in specified order.
41
     * 
42
     * @var DataEnricher\Processor[]
43
     */
44
    public $processors;
45
    
46
    
47
    /**
48
     * Class constructor
49
     */
50 2
    public function __construct()
51
    {
52 2
        foreach (static::$defaultProcessors as $property => $processor) {
53
            if (is_string($processor)) {
54
                $processor = new $processor($property);
55
            }
56
            
57
            $this->processors[] = $processor;
58
        }
59 2
    }
60
    
61
    /**
62
     * Create processors
63
     * 
64
     * @param object       $source  Data source
65
     * @param array|object $target  Target or dot key path
66
     * @return Processor[]
67
     */
68
    protected function getProcessorsFor($source, $target)
69
    {
70
        $processors = [];
71
        
72
        foreach ($this->processors as $processor) {
73
            $processors[] = $processor->withSourceAndTarget($source, $target);
74
        }
75
        
76
        return $processors;
77
    }
78
    
79
    
80
    /**
81
     * Apply processing instructions
82
     * 
83
     * @param array|object|string $target  Target or dot key path
84
     * @param object              $source  Data source
85
     */
86 2
    public function applyTo($target, $source = null)
87
    {
88 2
        if (!isset($source)) {
89 2
            $source = $target;
90
        }
91
        
92 2
        if (!is_object($source)) {
93 2
            throw new \Exception("Data enricher on works on an object, not on a " . gettype($source));
94
        }
95
        
96
        if (is_string($target)) {
97
            $target = DotKey::on($source)->get($target);
98
        }
99
        
100
        $nodes = $this->findNodes($target);
101
        $processors = $this->getProcessorsFor($source, $target);
102
        
103
        foreach ($nodes as $node) {
104
            foreach ($processors as $processor) {
105
                $node->apply($processor);
106
            }
107
        }
108
        
109
        $this->applyNodeResults($target);
110
    }
111
112
    /**
113
     * Find nodes that have processing instructions
114
     * 
115
     * @param array|object $target
116
     * @return array
117
     */
118
    public function findNodes(&$target)
119
    {
120
        $nodes = [];
121
        
122
        foreach ($target as $key => &$value) {
123
            if (is_array($value) || (is_object($value) && !$value instanceof Node)) {
124
                $nodes = array_merge($nodes, $this->findNodes($value));
125
            }
126
            
127
            if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) {
128
                $value = new Node($value);
129
                $nodes[] = $value;
130
            }
131
        }
132
        
133
        return $nodes;
134
    }
135
    
136
    /**
137
     * Check if object has at leas one process property
138
     * 
139
     * @param \stdClass    $value
140
     * @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...
141
     * @return boolean
142
     */
143
    protected function hasProcessorProperty($value)
144
    {
145
        $processorProps = array_map(function ($processor) {
146
            return $processor->getProperty();
147
        }, $this->processors);
148
        
149
        $valueProps = array_keys(get_object_vars($value));
150
        return count(array_intersect($valueProps, $processorProps)) > 0;
151
    }
152
    
153
    /**
154
     * Replace nodes with their results
155
     * 
156
     * @param array|object $target
157
     */
158 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...
159
    {
160
        foreach ($target as &$value) {
161
            if ($value instanceof Node) {
162
                $value = $value->getResult();
163
            } elseif (is_array($value) || is_object($value)) {
164
                $this->applyNodeResults($value);
165
            }
166
        }
167
    }
168
}
169