Completed
Pull Request — master (#525)
by thomas
26:53
created

PathTracer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
dl 0
loc 81
ccs 22
cts 24
cp 0.9167
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A makeSegment() 0 5 1
A addToCurrent() 0 4 1
A operation() 0 18 4
A done() 0 14 3
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Path;
4
5
use BitWasp\Bitcoin\Script\Parser\Operation;
6
7
class PathTracer
8
{
9
    /**
10
     * Disable new operations when set
11
     *
12
     * @var bool
13
     */
14
    private $done = false;
15
16
    /**
17
     * Store segments of scripts
18
     *
19
     * @var OperationContainer[]
20
     */
21
    private $segments = [];
22
23
    /**
24
     * Temporary storage for current segment
25
     *
26
     * @var Operation[]
27
     */
28
    private $current = [];
29
30
    /**
31
     * Make a segment from whatever's in current
32
     */
33 154
    private function makeSegment()
34
    {
35 154
        $this->segments[] = new OperationContainer($this->current);
36 154
        $this->current = [];
37 154
    }
38
39
    /**
40
     * Add an operation to current segment
41
     * @param Operation $operation
42
     */
43 154
    private function addToCurrent(Operation $operation)
44
    {
45 154
        $this->current[] = $operation;
46 154
    }
47
48
    /**
49
     * @param Operation $operation
50
     */
51 154
    public function operation(Operation $operation)
52
    {
53 154
        if ($this->done) {
54
            throw new \RuntimeException("Cannot add operation to finished PathTracer");
55
        }
56
57 154
        if ($operation->isLogical()) {
58
            // Logical opcodes mean the end of a segment
59 32
            if (count($this->current) > 0) {
60 32
                $this->makeSegment();
61
            }
62
63 32
            $this->addToCurrent($operation);
64 32
            $this->makeSegment();
65
        } else {
66 154
            $this->addToCurrent($operation);
67
        }
68 154
    }
69
70
    /**
71
     * @return PathTrace
72
     */
73 154
    public function done()
74
    {
75 154
        if ($this->done) {
76
            return new PathTrace($this->segments);
77
        }
78
79 154
        if (count($this->current) > 0) {
80 132
            $this->makeSegment();
81
        }
82
83 154
        $this->done = true;
84
85 154
        return new PathTrace($this->segments);
86
    }
87
}
88