Failed Conditions
Push — master ( 04820f...7dec56 )
by Moesjarraf
03:10
created

DataEnricher   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 160
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 1
cbo 2
dl 10
loc 160
ccs 56
cts 58
cp 0.9655
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
        '<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 4
            }
59
            
60 4
            $this->processors[] = $processor;
61 4
        }
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 1
        }
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 3
        }
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 1
            }
110 1
        }
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 1
            }
129
            
130 1
            if ($value instanceof \stdClass && $this->hasProcessorProperty($value)) {
131 1
                $value = new Node($value);
132 1
                $nodes[] = $value;
133 1
            }
134 1
        }
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
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...
144
     * @return boolean
145
     */
146
    protected function hasProcessorProperty($value)
147
    {
148 1
        $processorProps = array_map(function ($processor) {
149 1
            return $processor->getProperty();
150 1
        }, $this->processors);
151
        
152 1
        $valueProps = array_keys(get_object_vars($value));
153 1
        return count(array_intersect($valueProps, $processorProps)) > 0;
154
    }
155
    
156
    /**
157
     * Replace nodes with their results
158
     * 
159
     * @param array|object $target
160
     */
161 1 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...
162
    {
163 1
        foreach ($target as &$value) {
164 1
            if ($value instanceof Node) {
165 1
                $value = $value->getResult();
166 1
            } elseif (is_array($value) || is_object($value)) {
167 1
                $this->applyNodeResults($value);
168 1
            }
169 1
        }
170 1
    }
171
}
172