Completed
Push — master ( 595279...40a4e1 )
by thomas
27:02
created

PathTracer::operation()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 18
ccs 9
cts 10
cp 0.9
crap 4.016
rs 9.2
c 0
b 0
f 0
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