Skip to content

Commit c23a21a

Browse files
author
Anton Shabouta
committed
Refactor to static constructs
1 parent 76ffeaa commit c23a21a

File tree

2 files changed

+43
-47
lines changed

2 files changed

+43
-47
lines changed

src/Binary.php

+39-43
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,20 @@ public function __construct(string $buffer = '')
4949
}
5050

5151
/**
52-
* Returns number of bytes in buffer.
52+
* @param string|self $value
5353
*
54-
* @return int
54+
* @return self
5555
*/
56-
public function size(): int
56+
public function append($value): self
5757
{
58-
return $this->size;
59-
}
58+
if ($value instanceof Binary) {
59+
$value = $value->data;
60+
}
6061

61-
/**
62-
* @return boolean
63-
*/
64-
public function empty(): bool
65-
{
66-
return $this->size === 0;
62+
$this->data .= $value;
63+
$this->size += \strlen($value);
64+
65+
return $this;
6766
}
6867

6968
/**
@@ -108,19 +107,6 @@ public function consume(int $n): string
108107
return $buffer;
109108
}
110109
}
111-
112-
/**
113-
* @return string
114-
*/
115-
public function flush(): string
116-
{
117-
$data = $this->data;
118-
119-
$this->data = '';
120-
$this->size = 0;
121-
122-
return $data;
123-
}
124110

125111
/**
126112
* @param int $n
@@ -147,25 +133,25 @@ public function discard(int $n): self
147133
/**
148134
* @param int $n
149135
*
150-
* @return self
136+
* @return static
151137
*/
152138
public function slice(int $n): self
153139
{
154140
if ($this->size < $n) {
155141
throw new Exception\BufferUnderflow;
156142
} elseif ($this->size === $n) {
157-
return new self($this->data);
143+
return new static($this->data);
158144
} else {
159-
return new self(\substr($this->data, 0, $n));
145+
return new static(\substr($this->data, 0, $n));
160146
}
161147
}
162148

163149
/**
164150
* @param int $n
165151
*
166-
* @return self
152+
* @return static
167153
*/
168-
public function consumeSlice(int $n): self
154+
public function shift(int $n): self
169155
{
170156
if ($this->size < $n) {
171157
throw new Exception\BufferUnderflow;
@@ -175,35 +161,45 @@ public function consumeSlice(int $n): self
175161
$this->data = '';
176162
$this->size = 0;
177163

178-
return new self($buffer);
164+
return new static($buffer);
179165

180166
} else {
181167
$buffer = \substr($this->data, 0, $n);
182168

183169
$this->data = \substr($this->data, $n);
184170
$this->size -= $n;
185171

186-
return new self($buffer);
172+
return new static($buffer);
187173
}
188174
}
189175

190176
/**
191-
* Appends bytes at the end of the buffer.
192-
*
193-
* @param string|self $value
194-
*
195-
* @return self
177+
* @return string
196178
*/
197-
public function append($value): self
179+
public function flush(): string
198180
{
199-
if ($value instanceof Binary) {
200-
$value = $value->data;
201-
}
181+
$data = $this->data;
202182

203-
$this->data .= $value;
204-
$this->size += \strlen($value);
183+
$this->data = '';
184+
$this->size = 0;
205185

206-
return $this;
186+
return $data;
187+
}
188+
189+
/**
190+
* @return int
191+
*/
192+
public function size(): int
193+
{
194+
return $this->size;
195+
}
196+
197+
/**
198+
* @return boolean
199+
*/
200+
public function empty(): bool
201+
{
202+
return $this->size === 0;
207203
}
208204

209205
/**

tests/BinaryTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ public function testConsumeSlice()
174174
{
175175
$buf = new Binary('abcdef');
176176

177-
$slice1 = $buf->consumeSlice(1);
177+
$slice1 = $buf->shift(1);
178178
$this->assertEquals('a', $slice1->read($slice1->size()));
179179
$this->assertEquals(5, $buf->size());
180180

181-
$slice2 = $buf->consumeSlice(2);
181+
$slice2 = $buf->shift(2);
182182
$this->assertEquals('bc', $slice2->read($slice2->size()));
183183
$this->assertEquals(3, $buf->size());
184184

185-
$slice3 = $buf->consumeSlice(3);
185+
$slice3 = $buf->shift(3);
186186
$this->assertEquals('def', $slice3->read($slice3->size()));
187187
$this->assertEquals(0, $buf->size());
188188
}
@@ -193,7 +193,7 @@ public function testConsumeSlice()
193193
public function testConsumeSliceThrows()
194194
{
195195
$buf = new Binary;
196-
$buf->consumeSlice(1);
196+
$buf->shift(1);
197197
}
198198

199199
public function testAppend()

0 commit comments

Comments
 (0)