From c23a21a0e86b6f5f3bd8f6b7ac4b42ce88a7693d Mon Sep 17 00:00:00 2001 From: Anton Shabouta Date: Wed, 16 Jan 2019 14:18:23 +0300 Subject: [PATCH] Refactor to static constructs --- src/Binary.php | 82 +++++++++++++++++++++----------------------- tests/BinaryTest.php | 8 ++--- 2 files changed, 43 insertions(+), 47 deletions(-) diff --git a/src/Binary.php b/src/Binary.php index e6ac8f0..1d18047 100644 --- a/src/Binary.php +++ b/src/Binary.php @@ -49,21 +49,20 @@ public function __construct(string $buffer = '') } /** - * Returns number of bytes in buffer. + * @param string|self $value * - * @return int + * @return self */ - public function size(): int + public function append($value): self { - return $this->size; - } + if ($value instanceof Binary) { + $value = $value->data; + } - /** - * @return boolean - */ - public function empty(): bool - { - return $this->size === 0; + $this->data .= $value; + $this->size += \strlen($value); + + return $this; } /** @@ -108,19 +107,6 @@ public function consume(int $n): string return $buffer; } } - - /** - * @return string - */ - public function flush(): string - { - $data = $this->data; - - $this->data = ''; - $this->size = 0; - - return $data; - } /** * @param int $n @@ -147,25 +133,25 @@ public function discard(int $n): self /** * @param int $n * - * @return self + * @return static */ public function slice(int $n): self { if ($this->size < $n) { throw new Exception\BufferUnderflow; } elseif ($this->size === $n) { - return new self($this->data); + return new static($this->data); } else { - return new self(\substr($this->data, 0, $n)); + return new static(\substr($this->data, 0, $n)); } } /** * @param int $n * - * @return self + * @return static */ - public function consumeSlice(int $n): self + public function shift(int $n): self { if ($this->size < $n) { throw new Exception\BufferUnderflow; @@ -175,7 +161,7 @@ public function consumeSlice(int $n): self $this->data = ''; $this->size = 0; - return new self($buffer); + return new static($buffer); } else { $buffer = \substr($this->data, 0, $n); @@ -183,27 +169,37 @@ public function consumeSlice(int $n): self $this->data = \substr($this->data, $n); $this->size -= $n; - return new self($buffer); + return new static($buffer); } } /** - * Appends bytes at the end of the buffer. - * - * @param string|self $value - * - * @return self + * @return string */ - public function append($value): self + public function flush(): string { - if ($value instanceof Binary) { - $value = $value->data; - } + $data = $this->data; - $this->data .= $value; - $this->size += \strlen($value); + $this->data = ''; + $this->size = 0; - return $this; + return $data; + } + + /** + * @return int + */ + public function size(): int + { + return $this->size; + } + + /** + * @return boolean + */ + public function empty(): bool + { + return $this->size === 0; } /** diff --git a/tests/BinaryTest.php b/tests/BinaryTest.php index d086631..11bd661 100644 --- a/tests/BinaryTest.php +++ b/tests/BinaryTest.php @@ -174,15 +174,15 @@ public function testConsumeSlice() { $buf = new Binary('abcdef'); - $slice1 = $buf->consumeSlice(1); + $slice1 = $buf->shift(1); $this->assertEquals('a', $slice1->read($slice1->size())); $this->assertEquals(5, $buf->size()); - $slice2 = $buf->consumeSlice(2); + $slice2 = $buf->shift(2); $this->assertEquals('bc', $slice2->read($slice2->size())); $this->assertEquals(3, $buf->size()); - $slice3 = $buf->consumeSlice(3); + $slice3 = $buf->shift(3); $this->assertEquals('def', $slice3->read($slice3->size())); $this->assertEquals(0, $buf->size()); } @@ -193,7 +193,7 @@ public function testConsumeSlice() public function testConsumeSliceThrows() { $buf = new Binary; - $buf->consumeSlice(1); + $buf->shift(1); } public function testAppend()