PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
← All changes | includes/sdk/s3/GuzzleHttp/Psr7/Stream.php +47 -35 1.0.0 → 1.4.2 View file →
@@ -1,32 +1,34 @@
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 * PHP stream implementation.
8 - *
9 - * @var $stream
10 9 */
11 10 class Stream implements StreamInterface
12 11 {
13 12 /**
14 - * Resource modes.
15 - *
16 - * @var string
17 - *
18 - * @see http://php.net/manual/function.fopen.php
19 - * @see http://php.net/manual/en/function.gzopen.php
13 + * @see https://www.php.net/manual/en/function.fopen.php
14 + * @see https://www.php.net/manual/en/function.gzopen.php
20 15 */
21 - const READABLE_MODES = '/r|a\\+|ab\\+|w\\+|wb\\+|x\\+|xb\\+|c\\+|cb\\+/';
22 - const WRITABLE_MODES = '/a|w|r\\+|rb\\+|rw|x|c/';
16 + private const READABLE_MODES = '/r|a\\+|ab\\+|w\\+|wb\\+|x\\+|xb\\+|c\\+|cb\\+/';
17 + private const WRITABLE_MODES = '/a|w|r\\+|rb\\+|rw|x|c/';
18 + /** @var resource */
23 19 private $stream;
20 + /** @var int|null */
24 21 private $size;
22 + /** @var bool */
25 23 private $seekable;
24 + /** @var bool */
26 25 private $readable;
26 + /** @var bool */
27 27 private $writable;
28 + /** @var string|null */
28 29 private $uri;
30 + /** @var mixed[] */
29 31 private $customMetadata;
30 32 /**
31 33 * This constructor accepts an associative array of options.
32 34 *
@@ -35,14 +37,14 @@
35 37 * provide that size, in bytes.
36 38 * - metadata: (array) Any additional metadata to return when the metadata
37 39 * of the stream is accessed.
38 40 *
39 - * @param resource $stream Stream resource to wrap.
40 - * @param array $options Associative array of options.
41 + * @param resource $stream Stream resource to wrap.
42 + * @param array{size?: int, metadata?: array} $options Associative array of options.
41 43 *
42 44 * @throws \InvalidArgumentException if the stream is not a stream resource
43 45 */
44 - public function __construct($stream, $options = [])
46 + public function __construct($stream, array $options = [])
45 47 {
46 48 if (!\is_resource($stream)) {
47 49 throw new \InvalidArgumentException('Stream must be a resource');
48 50 }
@@ -48,9 +50,9 @@
48 50 }
49 51 if (isset($options['size'])) {
50 52 $this->size = $options['size'];
51 53 }
52 - $this->customMetadata = isset($options['metadata']) ? $options['metadata'] : [];
54 + $this->customMetadata = $options['metadata'] ?? [];
53 55 $this->stream = $stream;
54 56 $meta = \stream_get_meta_data($this->stream);
55 57 $this->seekable = $meta['seekable'];
56 58 $this->readable = (bool) \preg_match(self::READABLE_MODES, $meta['mode']);
@@ -63,9 +65,9 @@
63 65 public function __destruct()
64 66 {
65 67 $this->close();
66 68 }
67 - public function __toString()
69 + public function __toString() : string
68 70 {
69 71 try {
70 72 if ($this->isSeekable()) {
71 73 $this->seek(0);
@@ -70,24 +72,27 @@
70 72 if ($this->isSeekable()) {
71 73 $this->seek(0);
72 74 }
73 75 return $this->getContents();
74 - } catch (\Exception $e) {
76 + } catch (\Throwable $e) {
77 + if (\PHP_VERSION_ID >= 70400) {
78 + throw $e;
79 + }
80 + \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR);
75 81 return '';
76 82 }
77 83 }
78 - public function getContents()
84 + public function getContents() : string
79 85 {
80 86 if (!isset($this->stream)) {
81 87 throw new \RuntimeException('Stream is detached');
82 88 }
83 - $contents = \stream_get_contents($this->stream);
84 - if ($contents === \false) {
85 - throw new \RuntimeException('Unable to read stream contents');
89 + if (!$this->readable) {
90 + throw new \RuntimeException('Cannot read from non-readable stream');
86 91 }
87 - return $contents;
92 + return Utils::tryGetContents($this->stream);
88 93 }
89 - public function close()
94 + public function close() : void
90 95 {
91 96 if (isset($this->stream)) {
92 97 if (\is_resource($this->stream)) {
93 98 \fclose($this->stream);
@@ -105,9 +110,9 @@
105 110 $this->size = $this->uri = null;
106 111 $this->readable = $this->writable = $this->seekable = \false;
107 112 return $result;
108 113 }
109 - public function getSize()
114 + public function getSize() : ?int
110 115 {
111 116 if ($this->size !== null) {
112 117 return $this->size;
113 118 }
@@ -118,27 +123,27 @@
118 123 if ($this->uri) {
119 124 \clearstatcache(\true, $this->uri);
120 125 }
121 126 $stats = \fstat($this->stream);
122 - if (isset($stats['size'])) {
127 + if (\is_array($stats) && isset($stats['size'])) {
123 128 $this->size = $stats['size'];
124 129 return $this->size;
125 130 }
126 131 return null;
127 132 }
128 - public function isReadable()
133 + public function isReadable() : bool
129 134 {
130 135 return $this->readable;
131 136 }
132 - public function isWritable()
137 + public function isWritable() : bool
133 138 {
134 139 return $this->writable;
135 140 }
136 - public function isSeekable()
141 + public function isSeekable() : bool
137 142 {
138 143 return $this->seekable;
139 144 }
140 - public function eof()
145 + public function eof() : bool
141 146 {
142 147 if (!isset($this->stream)) {
143 148 throw new \RuntimeException('Stream is detached');
144 149 }
@@ -143,9 +148,9 @@
143 148 throw new \RuntimeException('Stream is detached');
144 149 }
145 150 return \feof($this->stream);
146 151 }
147 - public function tell()
152 + public function tell() : int
148 153 {
149 154 if (!isset($this->stream)) {
150 155 throw new \RuntimeException('Stream is detached');
151 156 }
@@ -154,13 +159,13 @@
154 159 throw new \RuntimeException('Unable to determine stream position');
155 160 }
156 161 return $result;
157 162 }
158 - public function rewind()
163 + public function rewind() : void
159 164 {
160 165 $this->seek(0);
161 166 }
162 - public function seek($offset, $whence = \SEEK_SET)
167 + public function seek($offset, $whence = \SEEK_SET) : void
163 168 {
164 169 $whence = (int) $whence;
165 170 if (!isset($this->stream)) {
166 171 throw new \RuntimeException('Stream is detached');
@@ -171,9 +176,9 @@
171 176 if (\fseek($this->stream, $offset, $whence) === -1) {
172 177 throw new \RuntimeException('Unable to seek to stream position ' . $offset . ' with whence ' . \var_export($whence, \true));
173 178 }
174 179 }
175 - public function read($length)
180 + public function read($length) : string
176 181 {
177 182 if (!isset($this->stream)) {
178 183 throw new \RuntimeException('Stream is detached');
179 184 }
@@ -185,15 +190,19 @@
185 190 }
186 191 if (0 === $length) {
187 192 return '';
188 193 }
189 - $string = \fread($this->stream, $length);
194 + try {
195 + $string = \fread($this->stream, $length);
196 + } catch (\Exception $e) {
197 + throw new \RuntimeException('Unable to read from stream', 0, $e);
198 + }
190 199 if (\false === $string) {
191 200 throw new \RuntimeException('Unable to read from stream');
192 201 }
193 202 return $string;
194 203 }
195 - public function write($string)
204 + public function write($string) : int
196 205 {
197 206 if (!isset($this->stream)) {
198 207 throw new \RuntimeException('Stream is detached');
199 208 }
@@ -207,8 +216,11 @@
207 216 throw new \RuntimeException('Unable to write to stream');
208 217 }
209 218 return $result;
210 219 }
220 + /**
221 + * @return mixed
222 + */
211 223 public function getMetadata($key = null)
212 224 {
213 225 if (!isset($this->stream)) {
214 226 return $key ? null : [];
@@ -217,7 +229,7 @@
217 229 } elseif (isset($this->customMetadata[$key])) {
218 230 return $this->customMetadata[$key];
219 231 }
220 232 $meta = \stream_get_meta_data($this->stream);
221 - return isset($meta[$key]) ? $meta[$key] : null;
233 + return $meta[$key] ?? null;
222 234 }
223 235 }