Completed
Pull Request — master (#524)
by thomas
71:45
created

ScriptBranch   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 120
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getFullScript() 0 4 1
A getPath() 0 4 1
A getSegments() 0 4 1
A getOps() 0 8 2
A getNeuteredScript() 0 4 1
A getScript() 0 6 1
A __debugInfo() 0 17 4
A getSignSteps() 0 11 3
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Path;
4
5
use BitWasp\Bitcoin\Script\Parser\Operation;
6
use BitWasp\Bitcoin\Script\ScriptFactory;
7
use BitWasp\Bitcoin\Script\ScriptInterface;
8
9
class ScriptBranch
10
{
11
    /**
12
     * @var ScriptInterface
13
     */
14
    private $fullScript;
15
16
    /**
17
     * @var array|\array[]
18
     */
19
    private $segments;
20
21
    /**
22
     * @var array|\bool[]
23
     */
24
    private $branch;
25
26
    /**
27
     * ScriptBranch constructor.
28
     * @param ScriptInterface $fullScript
29
     * @param array $logicalPath
30
     * @param PathTrace $segments
31
     */
32
    public function __construct(ScriptInterface $fullScript, array $logicalPath, PathTrace $segments)
33
    {
34
        $this->fullScript = $fullScript;
35
        $this->branch = $logicalPath;
36
        $this->segments = $segments;
0 ignored issues
show
Documentation Bug introduced by
It seems like $segments of type object<BitWasp\Bitcoin\Script\Path\PathTrace> is incompatible with the declared type array of property $segments.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
    }
38
39
    /**
40
     * @return ScriptInterface
41
     */
42
    public function getFullScript()
43
    {
44
        return $this->fullScript;
45
    }
46
47
    /**
48
     * @return array|\bool[]
49
     */
50
    public function getPath()
51
    {
52
        return $this->branch;
53
    }
54
55
    /**
56
     * @return array|\array[]|PathTrace
57
     */
58
    public function getSegments()
59
    {
60
        return $this->segments;
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function getOps()
67
    {
68
        $sequence = [];
69
        foreach ($this->segments as $segment) {
70
            $sequence = array_merge($sequence, $segment->all());
71
        }
72
        return $sequence;
73
    }
74
75
    /**
76
     * @return ScriptInterface
77
     */
78
    public function getNeuteredScript()
79
    {
80
        return ScriptFactory::fromOperations($this->getOps());
81
    }
82
83
    /**
84
     * @return ScriptInterface
85
     */
86
    public function getScript()
87
    {
88
        return ScriptFactory::fromOperations(array_filter($this->getOps(), function (Operation $operation) {
89
            return !$operation->isLogical();
90
        }));
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function __debugInfo()
97
    {
98
        $m = [];
99
        foreach ($this->segments as $segment) {
100
            $m[] = ScriptFactory::fromOperations($segment->all());
101
        }
102
103
        $path = [];
104
        foreach ($this->branch as $flag) {
105
            $path[] = $flag ? 'true' : 'false';
106
        }
107
108
        return [
109
            'branch' => implode(", ", $path),
110
            'segments' => $m,
111
        ];
112
    }
113
114
    /**
115
     * @return OperationContainer[]
116
     */
117
    public function getSignSteps()
118
    {
119
        $steps = [];
120
        foreach ($this->segments as $segment) {
121
            if (!$segment->isLoneLogicalOp()) {
122
                $steps[] = $segment;
123
            }
124
        }
125
126
        return $steps;
127
    }
128
}
129