| @@ -1,15 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| 7 | 8 | * Decorator used to return only a subset of a stream. |
| 8 | - * | |
| 9 | - * @final | |
| 10 | 9 | */ |
| 11 | -class LimitStream implements StreamInterface | |
| 10 | +final class LimitStream implements StreamInterface | |
| 12 | 11 | { |
| 13 | 12 | use StreamDecoratorTrait; |
| 14 | 13 | /** @var int Offset to start reading from */ |
| 15 | 14 | private $offset; |
| @@ -14,8 +13,10 @@ | ||
| 14 | 13 | /** @var int Offset to start reading from */ |
| 15 | 14 | private $offset; |
| 16 | 15 | /** @var int Limit the number of bytes that can be read */ |
| 17 | 16 | private $limit; |
| 17 | + /** @var StreamInterface */ | |
| 18 | + private $stream; | |
| 18 | 19 | /** |
| 19 | 20 | * @param StreamInterface $stream Stream to wrap |
| 20 | 21 | * @param int $limit Total number of bytes to allow to be read |
| 21 | 22 | * from the stream. Pass -1 for no limit. |
| @@ -21,15 +22,15 @@ | ||
| 21 | 22 | * from the stream. Pass -1 for no limit. |
| 22 | 23 | * @param int $offset Position to seek to before reading (only |
| 23 | 24 | * works on seekable streams). |
| 24 | 25 | */ |
| 25 | - public function __construct(StreamInterface $stream, $limit = -1, $offset = 0) | |
| 26 | + public function __construct(StreamInterface $stream, int $limit = -1, int $offset = 0) | |
| 26 | 27 | { |
| 27 | 28 | $this->stream = $stream; |
| 28 | 29 | $this->setLimit($limit); |
| 29 | 30 | $this->setOffset($offset); |
| 30 | 31 | } |
| 31 | - public function eof() | |
| 32 | + public function eof() : bool | |
| 32 | 33 | { |
| 33 | 34 | // Always return true if the underlying stream is EOF |
| 34 | 35 | if ($this->stream->eof()) { |
| 35 | 36 | return \true; |
| @@ -34,9 +35,9 @@ | ||
| 34 | 35 | if ($this->stream->eof()) { |
| 35 | 36 | return \true; |
| 36 | 37 | } |
| 37 | 38 | // No limit and the underlying stream is not at EOF |
| 38 | - if ($this->limit == -1) { | |
| 39 | + if ($this->limit === -1) { | |
| 39 | 40 | return \false; |
| 40 | 41 | } |
| 41 | 42 | return $this->stream->tell() >= $this->offset + $this->limit; |
| 42 | 43 | } |
| @@ -41,15 +42,14 @@ | ||
| 41 | 42 | return $this->stream->tell() >= $this->offset + $this->limit; |
| 42 | 43 | } |
| 43 | 44 | /** |
| 44 | 45 | * Returns the size of the limited subset of data |
| 45 | - * {@inheritdoc} | |
| 46 | 46 | */ |
| 47 | - public function getSize() | |
| 47 | + public function getSize() : ?int | |
| 48 | 48 | { |
| 49 | 49 | if (null === ($length = $this->stream->getSize())) { |
| 50 | 50 | return null; |
| 51 | - } elseif ($this->limit == -1) { | |
| 51 | + } elseif ($this->limit === -1) { | |
| 52 | 52 | return $length - $this->offset; |
| 53 | 53 | } else { |
| 54 | 54 | return \min($this->limit, $length - $this->offset); |
| 55 | 55 | } |
| @@ -55,11 +55,10 @@ | ||
| 55 | 55 | } |
| 56 | 56 | } |
| 57 | 57 | /** |
| 58 | 58 | * Allow for a bounded seek on the read limited stream |
| 59 | - * {@inheritdoc} | |
| 60 | 59 | */ |
| 61 | - public function seek($offset, $whence = \SEEK_SET) | |
| 60 | + public function seek($offset, $whence = \SEEK_SET) : void | |
| 62 | 61 | { |
| 63 | 62 | if ($whence !== \SEEK_SET || $offset < 0) { |
| 64 | 63 | throw new \RuntimeException(\sprintf('Cannot seek to offset %s with whence %s', $offset, $whence)); |
| 65 | 64 | } |
| @@ -72,11 +71,10 @@ | ||
| 72 | 71 | $this->stream->seek($offset); |
| 73 | 72 | } |
| 74 | 73 | /** |
| 75 | 74 | * Give a relative tell() |
| 76 | - * {@inheritdoc} | |
| 77 | 75 | */ |
| 78 | - public function tell() | |
| 76 | + public function tell() : int | |
| 79 | 77 | { |
| 80 | 78 | return $this->stream->tell() - $this->offset; |
| 81 | 79 | } |
| 82 | 80 | /** |
| @@ -85,9 +83,9 @@ | ||
| 85 | 83 | * @param int $offset Offset to seek to and begin byte limiting from |
| 86 | 84 | * |
| 87 | 85 | * @throws \RuntimeException if the stream cannot be seeked. |
| 88 | 86 | */ |
| 89 | - public function setOffset($offset) | |
| 87 | + public function setOffset(int $offset) : void | |
| 90 | 88 | { |
| 91 | 89 | $current = $this->stream->tell(); |
| 92 | 90 | if ($current !== $offset) { |
| 93 | 91 | // If the stream cannot seek to the offset position, then read to it |
| @@ -107,15 +105,15 @@ | ||
| 107 | 105 | * |
| 108 | 106 | * @param int $limit Number of bytes to allow to be read from the stream. |
| 109 | 107 | * Use -1 for no limit. |
| 110 | 108 | */ |
| 111 | - public function setLimit($limit) | |
| 109 | + public function setLimit(int $limit) : void | |
| 112 | 110 | { |
| 113 | 111 | $this->limit = $limit; |
| 114 | 112 | } |
| 115 | - public function read($length) | |
| 113 | + public function read($length) : string | |
| 116 | 114 | { |
| 117 | - if ($this->limit == -1) { | |
| 115 | + if ($this->limit === -1) { | |
| 118 | 116 | return $this->stream->read($length); |
| 119 | 117 | } |
| 120 | 118 | // Check if the current position is less than the total allowed |
| 121 | 119 | // bytes + original offset |