Completed
Pull Request — master (#333)
by thomas
43:53 queued 39:08
created

Stack::swap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
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 3681
    public function __construct()
10
    {
11 3681
        $this->setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO | \SplDoublyLinkedList::IT_MODE_KEEP);
12 3681
    }
13
14 111
    public function bottom()
15
    {
16 111
        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 75
    private function typeCheck($value)
24
    {
25 75
        if (!$value instanceof BufferInterface) {
26
            throw new \InvalidArgumentException('Value was not of type Buffer');
27
        }
28 75
    }
29
30
    /**
31
     * @see \ArrayAccess::offsetGet()
32
     * @param int $offset
33
     * @return \BitWasp\Buffertools\BufferInterface
34
     */
35 2733
    public function offsetGet($offset)
36
    {
37 2733
        $offset = count($this) + $offset;
38 2733
        return parent::offsetGet($offset);
39
    }
40
41
    /**
42
     * @see \ArrayAccess::offsetSet()
43
     * @param int $offset
44
     * @param BufferInterface $value
45
     * @throws \InvalidArgumentException
46
     */
47 63
    public function offsetSet($offset, $value)
48
    {
49 63
        $this->typeCheck($value);
50 63
        $offset = count($this) + $offset;
51 63
        parent::offsetSet($offset, $value);
52 63
    }
53
54
    /**
55
     * @see \ArrayAccess::offsetExists()
56
     * @param int $offset
57
     * @return bool
58
     */
59
    public function offsetExists($offset)
60
    {
61
        $offset = count($this) + $offset;
62
        return parent::offsetExists($offset);
63
    }
64
65
    /**
66
     * @see \ArrayAccess::offsetUnset()
67
     * @param int $offset
68
     */
69 81
    public function offsetUnset($offset)
70
    {
71 81
        $offset = count($this) + $offset;
72 81
        parent::offsetUnset($offset);
73 78
    }
74
75
    /**
76
     * @param int $first
77
     * @param int $second
78
     */
79 63
    public function swap($first, $second)
80
    {
81 63
        $val1 = $this->offsetGet($first);
82 63
        $val2 = $this->offsetGet($second);
83 63
        $this->offsetSet($second, $val1);
84 63
        $this->offsetSet($first, $val2);
85 63
    }
86
87
    /**
88
     * @param int $index
89
     * @param BufferInterface $value
90
     */
91 18
    public function add($index, $value)
92
    {
93 18
        $this->typeCheck($value);
94
95 18
        if ($this->count() === 0 || $index === $this->count()) {
96 3
            $this->push($value);
97 2
        }
98
99 18
        $temp = [];
100 18
        for ($i = count($this); $i > $index; $i--) {
101 18
            echo $i.PHP_EOL;
102 18
            array_unshift($temp, $this->pop());
103 12
        }
104
105 18
        $this->push($value);
106 18
        foreach ($temp as $value) {
107 18
            $this->push($value);
108 12
        }
109 18
    }
110
111
    /**
112
     * @return int
113
     */
114
    public function end()
115
    {
116
        $count = count($this);
117
        if ($count === 0) {
118
            return 0;
119
        }
120
121
        return $count - 1;
122
    }
123
124
    /**
125
     * @param int $length
126
     * @return $this
127
     */
128 27
    public function resize($length)
129
    {
130 27
        if ($length > count($this)) {
131
            throw new \RuntimeException('Invalid start or length');
132
        }
133
134 27
        while (count($this) > $length) {
135 27
            $this->pop();
136 18
        }
137
138 27
        return $this;
139
    }
140
}
141