Skip to content

Commit a3b662c

Browse files
author
Anton Shabouta
committed
Early return patter & new tests
1 parent c23a21a commit a3b662c

File tree

2 files changed

+251
-189
lines changed

2 files changed

+251
-189
lines changed

src/Binary.php

+51-56
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public function __construct(string $buffer = '')
4949
}
5050

5151
/**
52-
* @param string|self $value
52+
* @param string|static $value
5353
*
54-
* @return self
54+
* @return static
5555
*/
5656
public function append($value): self
5757
{
@@ -75,11 +75,13 @@ public function read(int $n, int $offset = 0): string
7575
{
7676
if ($this->size < $offset + $n) {
7777
throw new Exception\BufferUnderflow;
78-
} elseif ($offset === 0 && $this->size === $offset + $n) {
78+
}
79+
80+
if ($offset === 0 && $this->size === $offset + $n) {
7981
return $this->data;
80-
} else {
81-
return \substr($this->data, $offset, $n);
8282
}
83+
84+
return \substr($this->data, $offset, $n);
8385
}
8486

8587
/**
@@ -91,43 +93,43 @@ public function consume(int $n): string
9193
{
9294
if ($this->size < $n) {
9395
throw new Exception\BufferUnderflow;
94-
} elseif ($this->size === $n) {
96+
}
97+
98+
if ($this->size === $n) {
9599
$buffer = $this->data;
96100

97101
$this->data = '';
98102
$this->size = 0;
99-
100-
return $buffer;
101103
} else {
102104
$buffer = \substr($this->data, 0, $n);
103105

104106
$this->data = \substr($this->data, $n);
105107
$this->size -= $n;
106-
107-
return $buffer;
108108
}
109+
110+
return $buffer;
109111
}
110112

111113
/**
112114
* @param int $n
113115
*
114-
* @return self
116+
* @return static
115117
*/
116118
public function discard(int $n): self
117119
{
118120
if ($this->size < $n) {
119121
throw new Exception\BufferUnderflow;
120-
} elseif ($this->size === $n) {
122+
}
123+
124+
if ($this->size === $n) {
121125
$this->data = '';
122126
$this->size = 0;
123-
124-
return $this;
125127
} else {
126128
$this->data = \substr($this->data, $n);
127129
$this->size -= $n;
128-
129-
return $this;
130130
}
131+
132+
return $this;
131133
}
132134

133135
/**
@@ -139,11 +141,9 @@ public function slice(int $n): self
139141
{
140142
if ($this->size < $n) {
141143
throw new Exception\BufferUnderflow;
142-
} elseif ($this->size === $n) {
143-
return new static($this->data);
144-
} else {
145-
return new static(\substr($this->data, 0, $n));
146144
}
145+
146+
return $this->size === $n ? new static($this->data) : new static(\substr($this->data, 0, $n));
147147
}
148148

149149
/**
@@ -155,22 +155,21 @@ public function shift(int $n): self
155155
{
156156
if ($this->size < $n) {
157157
throw new Exception\BufferUnderflow;
158-
} elseif ($this->size === $n) {
158+
}
159+
160+
if ($this->size === $n) {
159161
$buffer = $this->data;
160162

161163
$this->data = '';
162164
$this->size = 0;
163-
164-
return new static($buffer);
165-
166165
} else {
167166
$buffer = \substr($this->data, 0, $n);
168167

169168
$this->data = \substr($this->data, $n);
170169
$this->size -= $n;
171-
172-
return new static($buffer);
173170
}
171+
172+
return new static($buffer);
174173
}
175174

176175
/**
@@ -205,7 +204,7 @@ public function empty(): bool
205204
/**
206205
* @param int $value
207206
*
208-
* @return self
207+
* @return static
209208
*/
210209
public function appendInt8(int $value): self
211210
{
@@ -233,7 +232,7 @@ public function consumeInt8(): int
233232
/**
234233
* @param int $value
235234
*
236-
* @return self
235+
* @return static
237236
*/
238237
public function appendInt16(int $value): self
239238
{
@@ -261,7 +260,7 @@ public function consumeInt16(): int
261260
/**
262261
* @param int $value
263262
*
264-
* @return self
263+
* @return static
265264
*/
266265
public function appendInt32(int $value): self
267266
{
@@ -289,7 +288,7 @@ public function consumeInt32(): int
289288
/**
290289
* @param int $value
291290
*
292-
* @return self
291+
* @return static
293292
*/
294293
public function appendInt64(int $value): self
295294
{
@@ -313,13 +312,12 @@ public function readInt64(int $offset = 0): int
313312
$s = $this->read(8, $offset);
314313

315314
if (self::$native64BitPack) {
316-
$r = \unpack("q", self::swapEndian64($s))[1];
317-
} else {
318-
$d = \unpack("Lh/Ll", self::swapHalvedEndian64($s));
319-
$r = $d["h"] << 32 | $d["l"];
315+
return \unpack("q", self::swapEndian64($s))[1];
320316
}
317+
318+
$d = \unpack("Lh/Ll", self::swapHalvedEndian64($s));
321319

322-
return $r;
320+
return $d["h"] << 32 | $d["l"];
323321
}
324322

325323
/**
@@ -330,19 +328,18 @@ public function consumeInt64(): int
330328
$s = $this->consume(8);
331329

332330
if (self::$native64BitPack) {
333-
$r = \unpack("q", self::swapEndian64($s))[1];
334-
} else {
335-
$d = \unpack("Lh/Ll", self::swapHalvedEndian64($s));
336-
$r = $d["h"] << 32 | $d["l"];
331+
return \unpack("q", self::swapEndian64($s))[1];
337332
}
333+
334+
$d = \unpack("Lh/Ll", self::swapHalvedEndian64($s));
338335

339-
return $r;
336+
return $d["h"] << 32 | $d["l"];
340337
}
341338

342339
/**
343340
* @param int $value
344341
*
345-
* @return self
342+
* @return static
346343
*/
347344
public function appendUint8(int $value): self
348345
{
@@ -374,7 +371,7 @@ public function consumeUint8(): int
374371
/**
375372
* @param int $value
376373
*
377-
* @return self
374+
* @return static
378375
*/
379376
public function appendUint16(int $value): self
380377
{
@@ -406,7 +403,7 @@ public function consumeUint16(): int
406403
/**
407404
* @param int $value
408405
*
409-
* @return self
406+
* @return static
410407
*/
411408
public function appendUint32(int $value): self
412409
{
@@ -438,7 +435,7 @@ public function consumeUint32(): int
438435
/**
439436
* @param int $value
440437
*
441-
* @return self
438+
* @return static
442439
*/
443440
public function appendUint64(int $value): self
444441
{
@@ -462,13 +459,12 @@ public function readUint64(int $offset = 0): int
462459
$s = $this->read(8, $offset);
463460

464461
if (self::$native64BitPack) {
465-
$r = \unpack("Q", self::swapEndian64($s))[1];
466-
} else {
467-
$d = \unpack("Lh/Ll", self::swapHalvedEndian64($s));
468-
$r = $d["h"] << 32 | $d["l"];
462+
return \unpack("Q", self::swapEndian64($s))[1];
469463
}
464+
465+
$d = \unpack("Lh/Ll", self::swapHalvedEndian64($s));
470466

471-
return $r;
467+
return $d["h"] << 32 | $d["l"];
472468
}
473469

474470
/**
@@ -479,19 +475,18 @@ public function consumeUint64(): int
479475
$s = $this->consume(8);
480476

481477
if (self::$native64BitPack) {
482-
$r = \unpack("Q", self::swapEndian64($s))[1];
483-
} else {
484-
$d = \unpack("Lh/Ll", self::swapHalvedEndian64($s));
485-
$r = $d["h"] << 32 | $d["l"];
478+
return \unpack("Q", self::swapEndian64($s))[1];
486479
}
487480

488-
return $r;
481+
$d = \unpack("Lh/Ll", self::swapHalvedEndian64($s));
482+
483+
return $d["h"] << 32 | $d["l"];
489484
}
490485

491486
/**
492487
* @param float $value
493488
*
494-
* @return self
489+
* @return static
495490
*/
496491
public function appendFloat(float $value): self
497492
{
@@ -519,7 +514,7 @@ public function consumeFloat(): float
519514
/**
520515
* @param float $value
521516
*
522-
* @return self
517+
* @return static
523518
*/
524519
public function appendDouble($value): self
525520
{

0 commit comments

Comments
 (0)