Completed
Push — master ( 40a4e1...e12674 )
by thomas
37:14 queued 34:53
created

PathTracer::makeSegment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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