Completed
Push — master ( 8ef7d4...e1fcb7 )
by Sven
04:01
created

Node   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 130
Duplicated Lines 10.77 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 2
Metric Value
wmc 18
c 4
b 1
f 2
lcom 1
cbo 2
dl 14
loc 130
ccs 0
cts 50
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A setResult() 0 4 1
A hasInstruction() 0 5 1
B applyNodeResults() 14 14 5
A getResult() 0 11 2
A getInstruction() 0 16 3
A apply() 0 14 3

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\DataEnricher;
4
5
use LegalThings\DataEnricher\Processor;
6
use GuzzleHttp\Promise\PromiseInterface;
7
8
/**
9
 * An object with processing instructions
10
 */
11
class Node extends \stdClass
12
{
13
    /**
14
     * The processed data
15
     * @var mixed 
16
     */
17
    protected $i_result;
18
19
    /**
20
     * Class constructor
21
     * 
22
     * @param \stdClass $data
23
     */
24
    public function __construct(\stdClass $data)
25
    {
26
        $this->i_result = $data;
27
        
28
        foreach ($data as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $data of type object<stdClass> is not traversable.
Loading history...
29
            if ($key === 'i_result') {
30
                continue;
31
            }
32
            
33
            $this->$key = $value;
34
        }
35
    }
36
37
    
38
    /**
39
     * Replace nodes with their results
40
     * 
41
     * @param array|object $target
42
     */
43 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...
44
    {
45
        if (!is_array($target) && !is_object($target)) {
46
            return;
47
        }
48
        
49
        foreach ($target as &$value) {
50
            if ($value instanceof self) {
51
                $value = $value->getResult();
52
            }
53
            
54
            $this->applyNodeResults($value);
55
        }
56
    }
57
    
58
    /**
59
     * Get the processed result
60
     * 
61
     * @return mixed
62
     */
63
    public function getResult()
64
    {
65
        if ($this->i_result instanceof PromiseInterface) {
66
            $result = $this->i_result->wait();
67
        } else {
68
            $result = $this->i_result;
69
        }
70
        
71
        $this->applyNodeResults($result);
72
        return $result;
73
    }
74
    
75
    /**
76
     * Set the result after processing
77
     * 
78
     * @param mixed $result
79
     */
80
    public function setResult($result)
81
    {
82
        $this->i_result = $result;
83
    }
84
85
    
86
    /**
87
     * Test if the node has an instruction for a processor
88
     * 
89
     * @param Processor $processor
90
     * @return boolean
91
     */
92
    public function hasInstruction(Processor $processor)
93
    {
94
        $prop = $processor->getProperty();
95
        return isset($this->$prop);
96
    }
97
    
98
    /**
99
     * Get an instruction for a processor
100
     * 
101
     * @param Processor $processor
102
     * @return mixed
103
     */
104
    public function getInstruction(Processor $processor)
105
    {
106
        $prop = $processor->getProperty();
107
        
108
        if (!isset($this->$prop)) {
109
            throw new \LogicException("Node doesn't have instruction property '$prop'");
110
        }
111
        
112
        $value = $this->$prop;
113
        
114
        if ($value instanceof self) {
115
            $value = $value->getResult();
116
        }
117
        
118
        return $value;
119
    }
120
    
121
    /**
122
     * Apply processing to this node
123
     * 
124
     * @param Processor $processor
125
     */
126
    public function apply(Processor $processor)
127
    {
128
        if (!$this->hasInstruction($processor)) {
129
            return;
130
        }
131
        
132
        if ($this->i_result instanceof PromiseInterface) {
133
            $this->i_result->then(function() use ($processor) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
134
                $processor->applyToNode($this);
135
            });
136
        } else {
137
            $processor->applyToNode($this);
138
        }
139
    }
140
}
141