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
|
580 |
|
public function __construct() |
10
|
|
|
{ |
11
|
580 |
|
$this->setIteratorMode(\SplDoublyLinkedList::IT_MODE_FIFO | \SplDoublyLinkedList::IT_MODE_KEEP); |
12
|
580 |
|
} |
13
|
|
|
|
14
|
32 |
|
public function bottom() |
15
|
|
|
{ |
16
|
32 |
|
return parent::offsetGet(count($this) - 1); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param BufferInterface $value |
21
|
|
|
* @throws \InvalidArgumentException |
22
|
|
|
*/ |
23
|
28 |
|
private function typeCheck($value) |
24
|
|
|
{ |
25
|
28 |
|
if (!$value instanceof BufferInterface) { |
26
|
|
|
throw new \InvalidArgumentException('Value was not of type Buffer'); |
27
|
|
|
} |
28
|
28 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @see \ArrayAccess::offsetGet() |
32
|
|
|
* @param int $offset |
33
|
|
|
* @return \BitWasp\Buffertools\BufferInterface |
34
|
|
|
*/ |
35
|
368 |
|
public function offsetGet($offset) |
36
|
|
|
{ |
37
|
368 |
|
$offset = count($this) + $offset; |
38
|
368 |
|
return parent::offsetGet($offset); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @see \ArrayAccess::offsetSet() |
43
|
|
|
* @param int $offset |
44
|
|
|
* @param BufferInterface $value |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
16 |
|
public function offsetSet($offset, $value) |
48
|
|
|
{ |
49
|
16 |
|
$this->typeCheck($value); |
50
|
16 |
|
$offset = count($this) + $offset; |
51
|
16 |
|
parent::offsetSet($offset, $value); |
52
|
16 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @see \ArrayAccess::offsetExists() |
56
|
|
|
* @param int $offset |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
4 |
|
public function offsetExists($offset) |
60
|
|
|
{ |
61
|
4 |
|
$offset = count($this) + $offset; |
62
|
4 |
|
return parent::offsetExists($offset); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @see \ArrayAccess::offsetUnset() |
67
|
|
|
* @param int $offset |
68
|
|
|
*/ |
69
|
24 |
|
public function offsetUnset($offset) |
70
|
|
|
{ |
71
|
24 |
|
$offset = count($this) + $offset; |
72
|
24 |
|
parent::offsetUnset($offset); |
73
|
20 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param int $first |
77
|
|
|
* @param int $second |
78
|
|
|
*/ |
79
|
16 |
|
public function swap($first, $second) |
80
|
|
|
{ |
81
|
16 |
|
$val1 = $this->offsetGet($first); |
82
|
16 |
|
$val2 = $this->offsetGet($second); |
83
|
16 |
|
$this->offsetSet($second, $val1); |
84
|
16 |
|
$this->offsetSet($first, $val2); |
85
|
16 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int $index |
89
|
|
|
* @param BufferInterface $value |
90
|
|
|
*/ |
91
|
12 |
|
public function add($index, $value) |
92
|
|
|
{ |
93
|
12 |
|
$this->typeCheck($value); |
94
|
|
|
|
95
|
12 |
|
if (getenv('HHVM_VERSION') || version_compare(phpversion(), '5.5.0', 'lt')) { |
96
|
|
|
if ($index == $this->count()) { |
97
|
|
|
$this->push($value); |
98
|
|
|
} else { |
99
|
|
|
$size = count($this); |
100
|
|
|
$temp = []; |
101
|
|
|
for ($i = $size; $i > $index; $i--) { |
102
|
|
|
array_unshift($temp, $this->pop()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->push($value); |
106
|
|
|
foreach ($temp as $value) { |
107
|
|
|
$this->push($value); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} else { |
111
|
12 |
|
parent::add($index, $value); |
|
|
|
|
112
|
|
|
} |
113
|
8 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
540 |
|
public function end() |
119
|
|
|
{ |
120
|
540 |
|
$count = count($this); |
121
|
540 |
|
if ($count === 0) { |
122
|
540 |
|
return 0; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $count - 1; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param int $length |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
20 |
|
public function resize($length) |
133
|
|
|
{ |
134
|
20 |
|
if ($length > count($this)) { |
135
|
|
|
throw new \RuntimeException('Invalid start or length'); |
136
|
|
|
} |
137
|
|
|
|
138
|
20 |
|
while (count($this) > $length) { |
139
|
20 |
|
$this->pop(); |
140
|
15 |
|
} |
141
|
|
|
|
142
|
20 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
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:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.