| @@ -1,6 +1,7 @@ | ||
| 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 | /** |
| @@ -6,17 +7,18 @@ | ||
| 6 | 7 | /** |
| 7 | 8 | * Reads from multiple streams, one after the other. |
| 8 | 9 | * |
| 9 | 10 | * This is a read-only stream decorator. |
| 10 | - * | |
| 11 | - * @final | |
| 12 | 11 | */ |
| 13 | -class AppendStream implements StreamInterface | |
| 12 | +final class AppendStream implements StreamInterface | |
| 14 | 13 | { |
| 15 | 14 | /** @var StreamInterface[] Streams being decorated */ |
| 16 | 15 | private $streams = []; |
| 16 | + /** @var bool */ | |
| 17 | 17 | private $seekable = \true; |
| 18 | + /** @var int */ | |
| 18 | 19 | private $current = 0; |
| 20 | + /** @var int */ | |
| 19 | 21 | private $pos = 0; |
| 20 | 22 | /** |
| 21 | 23 | * @param StreamInterface[] $streams Streams to decorate. Each stream must |
| 22 | 24 | * be readable. |
| @@ -26,14 +28,18 @@ | ||
| 26 | 28 | foreach ($streams as $stream) { |
| 27 | 29 | $this->addStream($stream); |
| 28 | 30 | } |
| 29 | 31 | } |
| 30 | - public function __toString() | |
| 32 | + public function __toString() : string | |
| 31 | 33 | { |
| 32 | 34 | try { |
| 33 | 35 | $this->rewind(); |
| 34 | 36 | return $this->getContents(); |
| 35 | - } catch (\Exception $e) { | |
| 37 | + } catch (\Throwable $e) { | |
| 38 | + if (\PHP_VERSION_ID >= 70400) { | |
| 39 | + throw $e; | |
| 40 | + } | |
| 41 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); | |
| 36 | 42 | return ''; |
| 37 | 43 | } |
| 38 | 44 | } |
| 39 | 45 | /** |
| @@ -42,9 +48,9 @@ | ||
| 42 | 48 | * @param StreamInterface $stream Stream to append. Must be readable. |
| 43 | 49 | * |
| 44 | 50 | * @throws \InvalidArgumentException if the stream is not readable |
| 45 | 51 | */ |
| 46 | - public function addStream(StreamInterface $stream) | |
| 52 | + public function addStream(StreamInterface $stream) : void | |
| 47 | 53 | { |
| 48 | 54 | if (!$stream->isReadable()) { |
| 49 | 55 | throw new \InvalidArgumentException('Each stream must be readable'); |
| 50 | 56 | } |
| @@ -53,18 +59,16 @@ | ||
| 53 | 59 | $this->seekable = \false; |
| 54 | 60 | } |
| 55 | 61 | $this->streams[] = $stream; |
| 56 | 62 | } |
| 57 | - public function getContents() | |
| 63 | + public function getContents() : string | |
| 58 | 64 | { |
| 59 | 65 | return Utils::copyToString($this); |
| 60 | 66 | } |
| 61 | 67 | /** |
| 62 | 68 | * Closes each attached stream. |
| 63 | - * | |
| 64 | - * {@inheritdoc} | |
| 65 | 69 | */ |
| 66 | - public function close() | |
| 70 | + public function close() : void | |
| 67 | 71 | { |
| 68 | 72 | $this->pos = $this->current = 0; |
| 69 | 73 | $this->seekable = \true; |
| 70 | 74 | foreach ($this->streams as $stream) { |
| @@ -75,10 +79,8 @@ | ||
| 75 | 79 | /** |
| 76 | 80 | * Detaches each attached stream. |
| 77 | 81 | * |
| 78 | 82 | * Returns null as it's not clear which underlying stream resource to return. |
| 79 | - * | |
| 80 | - * {@inheritdoc} | |
| 81 | 83 | */ |
| 82 | 84 | public function detach() |
| 83 | 85 | { |
| 84 | 86 | $this->pos = $this->current = 0; |
| @@ -88,9 +90,9 @@ | ||
| 88 | 90 | } |
| 89 | 91 | $this->streams = []; |
| 90 | 92 | return null; |
| 91 | 93 | } |
| 92 | - public function tell() | |
| 94 | + public function tell() : int | |
| 93 | 95 | { |
| 94 | 96 | return $this->pos; |
| 95 | 97 | } |
| 96 | 98 | /** |
| @@ -97,12 +99,10 @@ | ||
| 97 | 99 | * Tries to calculate the size by adding the size of each stream. |
| 98 | 100 | * |
| 99 | 101 | * If any of the streams do not return a valid number, then the size of the |
| 100 | 102 | * append stream cannot be determined and null is returned. |
| 101 | - * | |
| 102 | - * {@inheritdoc} | |
| 103 | 103 | */ |
| 104 | - public function getSize() | |
| 104 | + public function getSize() : ?int | |
| 105 | 105 | { |
| 106 | 106 | $size = 0; |
| 107 | 107 | foreach ($this->streams as $stream) { |
| 108 | 108 | $s = $stream->getSize(); |
| @@ -112,22 +112,20 @@ | ||
| 112 | 112 | $size += $s; |
| 113 | 113 | } |
| 114 | 114 | return $size; |
| 115 | 115 | } |
| 116 | - public function eof() | |
| 116 | + public function eof() : bool | |
| 117 | 117 | { |
| 118 | 118 | return !$this->streams || $this->current >= \count($this->streams) - 1 && $this->streams[$this->current]->eof(); |
| 119 | 119 | } |
| 120 | - public function rewind() | |
| 120 | + public function rewind() : void | |
| 121 | 121 | { |
| 122 | 122 | $this->seek(0); |
| 123 | 123 | } |
| 124 | 124 | /** |
| 125 | 125 | * Attempts to seek to the given position. Only supports SEEK_SET. |
| 126 | - * | |
| 127 | - * {@inheritdoc} | |
| 128 | 126 | */ |
| 129 | - public function seek($offset, $whence = \SEEK_SET) | |
| 127 | + public function seek($offset, $whence = \SEEK_SET) : void | |
| 130 | 128 | { |
| 131 | 129 | if (!$this->seekable) { |
| 132 | 130 | throw new \RuntimeException('This AppendStream is not seekable'); |
| 133 | 131 | } elseif ($whence !== \SEEK_SET) { |
| @@ -151,12 +149,10 @@ | ||
| 151 | 149 | } |
| 152 | 150 | } |
| 153 | 151 | /** |
| 154 | 152 | * Reads from all of the appended streams until the length is met or EOF. |
| 155 | - * | |
| 156 | - * {@inheritdoc} | |
| 157 | 153 | */ |
| 158 | - public function read($length) | |
| 154 | + public function read($length) : string | |
| 159 | 155 | { |
| 160 | 156 | $buffer = ''; |
| 161 | 157 | $total = \count($this->streams) - 1; |
| 162 | 158 | $remaining = $length; |
| @@ -167,13 +163,12 @@ | ||
| 167 | 163 | $progressToNext = \false; |
| 168 | 164 | if ($this->current === $total) { |
| 169 | 165 | break; |
| 170 | 166 | } |
| 171 | - $this->current++; | |
| 167 | + ++$this->current; | |
| 172 | 168 | } |
| 173 | 169 | $result = $this->streams[$this->current]->read($remaining); |
| 174 | - // Using a loose comparison here to match on '', false, and null | |
| 175 | - if ($result == null) { | |
| 170 | + if ($result === '') { | |
| 176 | 171 | $progressToNext = \true; |
| 177 | 172 | continue; |
| 178 | 173 | } |
| 179 | 174 | $buffer .= $result; |
| @@ -181,24 +176,27 @@ | ||
| 181 | 176 | } |
| 182 | 177 | $this->pos += \strlen($buffer); |
| 183 | 178 | return $buffer; |
| 184 | 179 | } |
| 185 | - public function isReadable() | |
| 180 | + public function isReadable() : bool | |
| 186 | 181 | { |
| 187 | 182 | return \true; |
| 188 | 183 | } |
| 189 | - public function isWritable() | |
| 184 | + public function isWritable() : bool | |
| 190 | 185 | { |
| 191 | 186 | return \false; |
| 192 | 187 | } |
| 193 | - public function isSeekable() | |
| 188 | + public function isSeekable() : bool | |
| 194 | 189 | { |
| 195 | 190 | return $this->seekable; |
| 196 | 191 | } |
| 197 | - public function write($string) | |
| 192 | + public function write($string) : int | |
| 198 | 193 | { |
| 199 | 194 | throw new \RuntimeException('Cannot write to an AppendStream'); |
| 200 | 195 | } |
| 196 | + /** | |
| 197 | + * @return mixed | |
| 198 | + */ | |
| 201 | 199 | public function getMetadata($key = null) |
| 202 | 200 | { |
| 203 | 201 | return $key ? null : []; |
| 204 | 202 | } |