| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +declare (strict_types=1); | |
| 3 | 4 | namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 4 | 5 | |
| 5 | 6 | use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| @@ -5,22 +6,23 @@ | ||
| 5 | 6 | use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; |
| 6 | 7 | /** |
| 7 | 8 | * Provides a read only stream that pumps data from a PHP callable. |
| 8 | 9 | * |
| 9 | - * When invoking the provided callable, the PumpStream will pass the amount of | |
| 10 | - * data requested to read to the callable. The callable can choose to ignore | |
| 10 | + * When invoking the provided callable, the PumpStream will pass the suggested | |
| 11 | + * number of bytes to read to the callable. The callable can choose to ignore | |
| 11 | 12 | * this value and return fewer or more bytes than requested. Any extra data |
| 12 | - * returned by the provided callable is buffered internally until drained using | |
| 13 | - * the read() function of the PumpStream. The provided callable MUST return | |
| 14 | - * false when there is no more data to read. | |
| 13 | + * returned by the callable is buffered internally until drained using the | |
| 14 | + * read() function of the PumpStream. The callable MUST return false or null | |
| 15 | + * when there is no more data to read. | |
| 15 | 16 | * |
| 16 | - * @final | |
| 17 | + * Userland callables that declare no parameters are tolerated by PHP, but | |
| 18 | + * length-aware callables remain the recommended formal shape. | |
| 17 | 19 | */ |
| 18 | -class PumpStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface | |
| 20 | +final class PumpStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface | |
| 19 | 21 | { |
| 20 | - /** @var callable */ | |
| 22 | + /** @var callable|null */ | |
| 21 | 23 | private $source; |
| 22 | - /** @var int */ | |
| 24 | + /** @var int|null */ | |
| 23 | 25 | private $size; |
| 24 | 26 | /** @var int */ |
| 25 | 27 | private $tellPos = 0; |
| 26 | 28 | /** @var array */ |
| @@ -27,80 +29,99 @@ | ||
| 27 | 29 | private $metadata; |
| 28 | 30 | /** @var BufferStream */ |
| 29 | 31 | private $buffer; |
| 30 | 32 | /** |
| 31 | - * @param callable $source Source of the stream data. The callable MAY | |
| 32 | - * accept an integer argument used to control the | |
| 33 | - * amount of data to return. The callable MUST | |
| 34 | - * return a string when called, or false on error | |
| 35 | - * or EOF. | |
| 36 | - * @param array $options Stream options: | |
| 37 | - * - metadata: Hash of metadata to use with stream. | |
| 38 | - * - size: Size of the stream, if known. | |
| 33 | + * @param (callable(): (string|false|null))|(callable(int): (string|false|null)) $source Source of the stream data. The callable receives | |
| 34 | + * the suggested number of bytes to read, may ignore | |
| 35 | + * that value, and may return fewer or more bytes. | |
| 36 | + * Extra bytes are buffered. The callable MUST return | |
| 37 | + * a string when called, or false|null on error or EOF. | |
| 38 | + * Userland callables that declare no parameters are | |
| 39 | + * tolerated by PHP, but length-aware callables remain | |
| 40 | + * the recommended formal shape. | |
| 41 | + * @param array{size?: int, metadata?: array} $options Stream options: | |
| 42 | + * - metadata: Hash of metadata to use with stream. | |
| 43 | + * - size: Size of the stream, if known. | |
| 39 | 44 | */ |
| 40 | 45 | public function __construct(callable $source, array $options = []) |
| 41 | 46 | { |
| 42 | 47 | $this->source = $source; |
| 43 | - $this->size = isset($options['size']) ? $options['size'] : null; | |
| 44 | - $this->metadata = isset($options['metadata']) ? $options['metadata'] : []; | |
| 48 | + $this->size = $options['size'] ?? null; | |
| 49 | + $this->metadata = $options['metadata'] ?? []; | |
| 45 | 50 | $this->buffer = new \YoastSEO_Vendor\GuzzleHttp\Psr7\BufferStream(); |
| 46 | 51 | } |
| 47 | - public function __toString() | |
| 52 | + public function __toString() : string | |
| 48 | 53 | { |
| 49 | 54 | try { |
| 50 | 55 | return \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::copyToString($this); |
| 51 | - } catch (\Exception $e) { | |
| 56 | + } catch (\Throwable $e) { | |
| 57 | + if (\PHP_VERSION_ID >= 70400) { | |
| 58 | + throw $e; | |
| 59 | + } | |
| 60 | + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); | |
| 52 | 61 | return ''; |
| 53 | 62 | } |
| 54 | 63 | } |
| 55 | - public function close() | |
| 64 | + public function close() : void | |
| 56 | 65 | { |
| 57 | 66 | $this->detach(); |
| 58 | 67 | } |
| 59 | 68 | public function detach() |
| 60 | 69 | { |
| 61 | - $this->tellPos = \false; | |
| 70 | + $this->tellPos = 0; | |
| 62 | 71 | $this->source = null; |
| 63 | 72 | return null; |
| 64 | 73 | } |
| 65 | - public function getSize() | |
| 74 | + public function getSize() : ?int | |
| 66 | 75 | { |
| 67 | 76 | return $this->size; |
| 68 | 77 | } |
| 69 | - public function tell() | |
| 78 | + public function tell() : int | |
| 70 | 79 | { |
| 71 | 80 | return $this->tellPos; |
| 72 | 81 | } |
| 73 | - public function eof() | |
| 82 | + public function eof() : bool | |
| 74 | 83 | { |
| 75 | - return !$this->source; | |
| 84 | + return $this->source === null; | |
| 76 | 85 | } |
| 77 | - public function isSeekable() | |
| 86 | + public function isSeekable() : bool | |
| 78 | 87 | { |
| 79 | 88 | return \false; |
| 80 | 89 | } |
| 81 | - public function rewind() | |
| 90 | + public function rewind() : void | |
| 82 | 91 | { |
| 83 | 92 | $this->seek(0); |
| 84 | 93 | } |
| 85 | - public function seek($offset, $whence = \SEEK_SET) | |
| 94 | + public function seek($offset, $whence = \SEEK_SET) : void | |
| 86 | 95 | { |
| 96 | + if (!\is_int($offset)) { | |
| 97 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.', \get_debug_type($offset)); | |
| 98 | + } | |
| 99 | + if (!\is_int($whence)) { | |
| 100 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.', \get_debug_type($whence)); | |
| 101 | + } | |
| 87 | 102 | throw new \RuntimeException('Cannot seek a PumpStream'); |
| 88 | 103 | } |
| 89 | - public function isWritable() | |
| 104 | + public function isWritable() : bool | |
| 90 | 105 | { |
| 91 | 106 | return \false; |
| 92 | 107 | } |
| 93 | - public function write($string) | |
| 108 | + public function write($string) : int | |
| 94 | 109 | { |
| 110 | + if (!\is_string($string)) { | |
| 111 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::write() is deprecated; guzzlehttp/psr7 3.0 requires string for $string.', \get_debug_type($string)); | |
| 112 | + } | |
| 95 | 113 | throw new \RuntimeException('Cannot write to a PumpStream'); |
| 96 | 114 | } |
| 97 | - public function isReadable() | |
| 115 | + public function isReadable() : bool | |
| 98 | 116 | { |
| 99 | 117 | return \true; |
| 100 | 118 | } |
| 101 | - public function read($length) | |
| 119 | + public function read($length) : string | |
| 102 | 120 | { |
| 121 | + if (!\is_int($length)) { | |
| 122 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.', \get_debug_type($length)); | |
| 123 | + } | |
| 103 | 124 | $data = $this->buffer->read($length); |
| 104 | 125 | $readLen = \strlen($data); |
| 105 | 126 | $this->tellPos += $readLen; |
| 106 | 127 | $remaining = $length - $readLen; |
| @@ -110,9 +131,9 @@ | ||
| 110 | 131 | $this->tellPos += \strlen($data) - $readLen; |
| 111 | 132 | } |
| 112 | 133 | return $data; |
| 113 | 134 | } |
| 114 | - public function getContents() | |
| 135 | + public function getContents() : string | |
| 115 | 136 | { |
| 116 | 137 | $result = ''; |
| 117 | 138 | while (!$this->eof()) { |
| 118 | 139 | $result .= $this->read(1000000); |
| @@ -118,20 +139,26 @@ | ||
| 118 | 139 | $result .= $this->read(1000000); |
| 119 | 140 | } |
| 120 | 141 | return $result; |
| 121 | 142 | } |
| 143 | + /** | |
| 144 | + * @return mixed | |
| 145 | + */ | |
| 122 | 146 | public function getMetadata($key = null) |
| 123 | 147 | { |
| 148 | + if ($key !== null && !\is_string($key)) { | |
| 149 | + \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::getMetadata() is deprecated; guzzlehttp/psr7 3.0 requires string|null for $key.', \get_debug_type($key)); | |
| 150 | + } | |
| 124 | 151 | if (!$key) { |
| 125 | 152 | return $this->metadata; |
| 126 | 153 | } |
| 127 | - return isset($this->metadata[$key]) ? $this->metadata[$key] : null; | |
| 154 | + return $this->metadata[$key] ?? null; | |
| 128 | 155 | } |
| 129 | - private function pump($length) | |
| 156 | + private function pump(int $length) : void | |
| 130 | 157 | { |
| 131 | - if ($this->source) { | |
| 158 | + if ($this->source !== null) { | |
| 132 | 159 | do { |
| 133 | - $data = \call_user_func($this->source, $length); | |
| 160 | + $data = ($this->source)($length); | |
| 134 | 161 | if ($data === \false || $data === null) { |
| 135 | 162 | $this->source = null; |
| 136 | 163 | return; |
| 137 | 164 | } |