Completed
Pull Request — master (#335)
by thomas
67:21 queued 64:10
created

Stack::add()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 12.582

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 5
nop 2
dl 0
loc 20
ccs 3
cts 16
cp 0.1875
crap 12.582
rs 9.2
c 2
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Interpreter;
4
5
use BitWasp\Buffertools\BufferInterface;
6
7
class Stack extends \SplDoublyLinkedList implements StackInterface
8
{
9 435
    public function __construct()
10
    {
11 435
        $this->setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO | \SplDoublyLinkedList::IT_MODE_KEEP);
12 435
    }
13
14 24
    public function bottom()
15
    {
16 24
        return parent::offsetGet(count($this) - 1);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (offsetGet() instead of bottom()). Are you sure this is correct? If so, you might want to change this to $this->offsetGet().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
17
    }
18
19
    /**
20
     * @param BufferInterface $value
21
     * @throws \InvalidArgumentException
22
     */
23 21
    private function typeCheck($value)
24
    {
25 21
        if (!$value instanceof BufferInterface) {
26
            throw new \InvalidArgumentException('Value was not of type Buffer');
27
        }
28 21
    }
29
30
    /**
31
     * @see \ArrayAccess::offsetGet()
32
     * @param int $offset
33
     * @return \BitWasp\Buffertools\BufferInterface
34
     */
35 276
    public function offsetGet($offset)
36
    {
37 276
        $offset = count($this) + $offset;
38 276
        return parent::offsetGet($offset);
39
    }
40
41
    /**
42
     * @see \ArrayAccess::offsetSet()
43
     * @param int $offset
44
     * @param BufferInterface $value
45
     * @throws \InvalidArgumentException
46
     */
47 12
    public function offsetSet($offset, $value)
48
    {
49 12
        $this->typeCheck($value);
50 12
        $offset = count($this) + $offset;
51 12
        parent::offsetSet($offset, $value);
52 12
    }
53
54
    /**
55
     * @see \ArrayAccess::offsetExists()
56
     * @param int $offset
57
     * @return bool
58
     */
59 3
    public function offsetExists($offset)
60
    {
61 3
        $offset = count($this) + $offset;
62 3
        return parent::offsetExists($offset);
63
    }
64
65
    /**
66
     * @see \ArrayAccess::offsetUnset()
67
     * @param int $offset
68
     */
69 18
    public function offsetUnset($offset)
70
    {
71 18
        $offset = count($this) + $offset;
72 18
        parent::offsetUnset($offset);
73 15
    }
74
75
    /**
76
     * @param int $first
77
     * @param int $second
78
     */
79 12
    public function swap($first, $second)
80
    {
81 12
        $val1 = $this->offsetGet($first);
82 12
        $val2 = $this->offsetGet($second);
83 12
        $this->offsetSet($second, $val1);
84 12
        $this->offsetSet($first, $val2);
85 12
    }
86
87
    /**
88
     * @param int $offset
89
     * @param BufferInterface $value
90
     */
91 9
    public function add($offset, $value)
92
    {
93 9
        $this->typeCheck($value);
94
        $size = count($this);
95 9
        $index = $size + $offset;
96
        if ($index > $size) {
97
            throw new \RuntimeException('Invalid add position');
98
        }
99
        
100
        // Unwind current values, push provided value, reapply popped values
101
        $values = [];
102
        for ($i = $size; $i > $index; $i--) {
103
            $values[] = $this->pop();
104
        }
105
106
        $this->push($value);
107
        for ($i = count($values); $i > 0; $i--) {
108
            $this->push(array_pop($values));
109
        }
110
    }
111 9
112
    /**
113 6
     * @return int
114
     */
115
    public function end()
116
    {
117
        $count = count($this);
118 405
        if ($count === 0) {
119
            return 0;
120 405
        }
121 405
122 405
        return $count - 1;
123
    }
124
125
    /**
126
     * @param int $length
127
     * @return $this
128
     */
129
    public function resize($length)
130
    {
131
        if ($length > count($this)) {
132 15
            throw new \RuntimeException('Invalid start or length');
133
        }
134 15
135
        while (count($this) > $length) {
136
            $this->pop();
137
        }
138 15
139 15
        return $this;
140 10
    }
141
}
142