diff options
Diffstat (limited to 'guzzlehttp/psr7/src/AppendStream.php')
-rw-r--r-- | guzzlehttp/psr7/src/AppendStream.php | 64 |
1 files changed, 33 insertions, 31 deletions
diff --git a/guzzlehttp/psr7/src/AppendStream.php b/guzzlehttp/psr7/src/AppendStream.php index fa9153d7..cbcfaee6 100644 --- a/guzzlehttp/psr7/src/AppendStream.php +++ b/guzzlehttp/psr7/src/AppendStream.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace GuzzleHttp\Psr7; use Psr\Http\Message\StreamInterface; @@ -8,16 +10,19 @@ use Psr\Http\Message\StreamInterface; * Reads from multiple streams, one after the other. * * This is a read-only stream decorator. - * - * @final */ -class AppendStream implements StreamInterface +final class AppendStream implements StreamInterface { /** @var StreamInterface[] Streams being decorated */ private $streams = []; + /** @var bool */ private $seekable = true; + + /** @var int */ private $current = 0; + + /** @var int */ private $pos = 0; /** @@ -31,12 +36,16 @@ class AppendStream implements StreamInterface } } - public function __toString() + public function __toString(): string { try { $this->rewind(); return $this->getContents(); - } catch (\Exception $e) { + } catch (\Throwable $e) { + if (\PHP_VERSION_ID >= 70400) { + throw $e; + } + trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR); return ''; } } @@ -48,7 +57,7 @@ class AppendStream implements StreamInterface * * @throws \InvalidArgumentException if the stream is not readable */ - public function addStream(StreamInterface $stream) + public function addStream(StreamInterface $stream): void { if (!$stream->isReadable()) { throw new \InvalidArgumentException('Each stream must be readable'); @@ -62,17 +71,15 @@ class AppendStream implements StreamInterface $this->streams[] = $stream; } - public function getContents() + public function getContents(): string { return Utils::copyToString($this); } /** * Closes each attached stream. - * - * {@inheritdoc} */ - public function close() + public function close(): void { $this->pos = $this->current = 0; $this->seekable = true; @@ -88,8 +95,6 @@ class AppendStream implements StreamInterface * Detaches each attached stream. * * Returns null as it's not clear which underlying stream resource to return. - * - * {@inheritdoc} */ public function detach() { @@ -105,7 +110,7 @@ class AppendStream implements StreamInterface return null; } - public function tell() + public function tell(): int { return $this->pos; } @@ -115,10 +120,8 @@ class AppendStream implements StreamInterface * * If any of the streams do not return a valid number, then the size of the * append stream cannot be determined and null is returned. - * - * {@inheritdoc} */ - public function getSize() + public function getSize(): ?int { $size = 0; @@ -133,24 +136,22 @@ class AppendStream implements StreamInterface return $size; } - public function eof() + public function eof(): bool { return !$this->streams || ($this->current >= count($this->streams) - 1 && $this->streams[$this->current]->eof()); } - public function rewind() + public function rewind(): void { $this->seek(0); } /** * Attempts to seek to the given position. Only supports SEEK_SET. - * - * {@inheritdoc} */ - public function seek($offset, $whence = SEEK_SET) + public function seek($offset, $whence = SEEK_SET): void { if (!$this->seekable) { throw new \RuntimeException('This AppendStream is not seekable'); @@ -181,10 +182,8 @@ class AppendStream implements StreamInterface /** * Reads from all of the appended streams until the length is met or EOF. - * - * {@inheritdoc} */ - public function read($length) + public function read($length): string { $buffer = ''; $total = count($this->streams) - 1; @@ -192,7 +191,6 @@ class AppendStream implements StreamInterface $progressToNext = false; while ($remaining > 0) { - // Progress to the next stream if needed. if ($progressToNext || $this->streams[$this->current]->eof()) { $progressToNext = false; @@ -204,8 +202,7 @@ class AppendStream implements StreamInterface $result = $this->streams[$this->current]->read($remaining); - // Using a loose comparison here to match on '', false, and null - if ($result == null) { + if ($result === '') { $progressToNext = true; continue; } @@ -219,26 +216,31 @@ class AppendStream implements StreamInterface return $buffer; } - public function isReadable() + public function isReadable(): bool { return true; } - public function isWritable() + public function isWritable(): bool { return false; } - public function isSeekable() + public function isSeekable(): bool { return $this->seekable; } - public function write($string) + public function write($string): int { throw new \RuntimeException('Cannot write to an AppendStream'); } + /** + * {@inheritdoc} + * + * @return mixed + */ public function getMetadata($key = null) { return $key ? null : []; |