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

PathTracer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 81
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
    private function makeSegment()
34
    {
35
        $this->segments[] = new OperationContainer($this->current);
36
        $this->current = [];
37
    }
38
39
    /**
40
     * Add an operation to current segment
41
     * @param Operation $operation
42
     */
43
    private function addToCurrent(Operation $operation)
44
    {
45
        $this->current[] = $operation;
46
    }
47
48
    /**
49
     * @param Operation $operation
50
     */
51
    public function operation(Operation $operation)
52
    {
53
        if ($this->done) {
54
            throw new \RuntimeException("Cannot add operation to finished PathTracer");
55
        }
56
57
        if ($operation->isLogical()) {
58
            // Logical opcodes mean the end of a segment
59
            if (count($this->current) > 0) {
60
                $this->makeSegment();
61
            }
62
63
            $this->addToCurrent($operation);
64
            $this->makeSegment();
65
        } else {
66
            $this->addToCurrent($operation);
67
        }
68
    }
69
70
    /**
71
     * @return PathTrace
72
     */
73
    public function done()
74
    {
75
        if ($this->done) {
76
            return new PathTrace($this->segments);
77
        }
78
79
        if (count($this->current) > 0) {
80
            $this->makeSegment();
81
        }
82
83
        $this->done = true;
84
85
        return new PathTrace($this->segments);
86
    }
87
}
88