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/Utils.php +75 -44 1.2.0 → 1.4.2 View file →
@@ -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\RequestInterface;
6 7 use Dudlewebs\WPMCS\s3\Psr\Http\Message\ServerRequestInterface;
@@ -10,20 +11,18 @@
10 11 {
11 12 /**
12 13 * Remove the items given by the keys, case insensitively from the data.
13 14 *
14 - * @param iterable<string> $keys
15 - *
16 - * @return array
15 + * @param (string|int)[] $keys
17 16 */
18 - public static function caselessRemove($keys, array $data)
17 + public static function caselessRemove(array $keys, array $data) : array
19 18 {
20 19 $result = [];
21 20 foreach ($keys as &$key) {
22 - $key = \strtolower($key);
21 + $key = \strtolower((string) $key);
23 22 }
24 23 foreach ($data as $k => $v) {
25 - if (!\in_array(\strtolower($k), $keys)) {
24 + if (!\in_array(\strtolower((string) $k), $keys)) {
26 25 $result[$k] = $v;
27 26 }
28 27 }
29 28 return $result;
@@ -38,9 +37,9 @@
38 37 * to read the entire stream.
39 38 *
40 39 * @throws \RuntimeException on error.
41 40 */
42 - public static function copyToStream(StreamInterface $source, StreamInterface $dest, $maxLen = -1)
41 + public static function copyToStream(StreamInterface $source, StreamInterface $dest, int $maxLen = -1) : void
43 42 {
44 43 $bufferSize = 8192;
45 44 if ($maxLen === -1) {
46 45 while (!$source->eof()) {
@@ -68,20 +67,17 @@
68 67 * @param StreamInterface $stream Stream to read
69 68 * @param int $maxLen Maximum number of bytes to read. Pass -1
70 69 * to read the entire stream.
71 70 *
72 - * @return string
73 - *
74 71 * @throws \RuntimeException on error.
75 72 */
76 - public static function copyToString(StreamInterface $stream, $maxLen = -1)
73 + public static function copyToString(StreamInterface $stream, int $maxLen = -1) : string
77 74 {
78 75 $buffer = '';
79 76 if ($maxLen === -1) {
80 77 while (!$stream->eof()) {
81 78 $buf = $stream->read(1048576);
82 - // Using a loose equality here to match on '' and false.
83 - if ($buf == null) {
79 + if ($buf === '') {
84 80 break;
85 81 }
86 82 $buffer .= $buf;
87 83 }
@@ -89,10 +85,9 @@
89 85 }
90 86 $len = 0;
91 87 while (!$stream->eof() && $len < $maxLen) {
92 88 $buf = $stream->read($maxLen - $len);
93 - // Using a loose equality here to match on '' and false.
94 - if ($buf == null) {
89 + if ($buf === '') {
95 90 break;
96 91 }
97 92 $buffer .= $buf;
98 93 $len = \strlen($buffer);
@@ -108,13 +103,11 @@
108 103 * @param StreamInterface $stream Stream to calculate the hash for
109 104 * @param string $algo Hash algorithm (e.g. md5, crc32, etc)
110 105 * @param bool $rawOutput Whether or not to use raw output
111 106 *
112 - * @return string Returns the hash of the stream
113 - *
114 107 * @throws \RuntimeException on error.
115 108 */
116 - public static function hash(StreamInterface $stream, $algo, $rawOutput = \false)
109 + public static function hash(StreamInterface $stream, string $algo, bool $rawOutput = \false) : string
117 110 {
118 111 $pos = $stream->tell();
119 112 if ($pos > 0) {
120 113 $stream->rewind();
@@ -122,9 +115,9 @@
122 115 $ctx = \hash_init($algo);
123 116 while (!$stream->eof()) {
124 117 \hash_update($ctx, $stream->read(1048576));
125 118 }
126 - $out = \hash_final($ctx, (bool) $rawOutput);
119 + $out = \hash_final($ctx, $rawOutput);
127 120 $stream->seek($pos);
128 121 return $out;
129 122 }
130 123 /**
@@ -143,12 +136,10 @@
143 136 * - version: (string) Set the protocol version.
144 137 *
145 138 * @param RequestInterface $request Request to clone and modify.
146 139 * @param array $changes Changes to apply.
147 - *
148 - * @return RequestInterface
149 140 */
150 - public static function modifyRequest(RequestInterface $request, array $changes)
141 + public static function modifyRequest(RequestInterface $request, array $changes) : RequestInterface
151 142 {
152 143 if (!$changes) {
153 144 return $request;
154 145 }
@@ -179,15 +170,15 @@
179 170 if (isset($changes['query'])) {
180 171 $uri = $uri->withQuery($changes['query']);
181 172 }
182 173 if ($request instanceof ServerRequestInterface) {
183 - $new = (new ServerRequest(isset($changes['method']) ? $changes['method'] : $request->getMethod(), $uri, $headers, isset($changes['body']) ? $changes['body'] : $request->getBody(), isset($changes['version']) ? $changes['version'] : $request->getProtocolVersion(), $request->getServerParams()))->withParsedBody($request->getParsedBody())->withQueryParams($request->getQueryParams())->withCookieParams($request->getCookieParams())->withUploadedFiles($request->getUploadedFiles());
174 + $new = (new ServerRequest($changes['method'] ?? $request->getMethod(), $uri, $headers, $changes['body'] ?? $request->getBody(), $changes['version'] ?? $request->getProtocolVersion(), $request->getServerParams()))->withParsedBody($request->getParsedBody())->withQueryParams($request->getQueryParams())->withCookieParams($request->getCookieParams())->withUploadedFiles($request->getUploadedFiles());
184 175 foreach ($request->getAttributes() as $key => $value) {
185 176 $new = $new->withAttribute($key, $value);
186 177 }
187 178 return $new;
188 179 }
189 - return new Request(isset($changes['method']) ? $changes['method'] : $request->getMethod(), $uri, $headers, isset($changes['body']) ? $changes['body'] : $request->getBody(), isset($changes['version']) ? $changes['version'] : $request->getProtocolVersion());
180 + return new Request($changes['method'] ?? $request->getMethod(), $uri, $headers, $changes['body'] ?? $request->getBody(), $changes['version'] ?? $request->getProtocolVersion());
190 181 }
191 182 /**
192 183 * Read a line from the stream up to the maximum allowed buffer length.
193 184 *
@@ -192,18 +183,15 @@
192 183 * Read a line from the stream up to the maximum allowed buffer length.
193 184 *
194 185 * @param StreamInterface $stream Stream to read from
195 186 * @param int|null $maxLength Maximum buffer length
196 - *
197 - * @return string
198 187 */
199 - public static function readLine(StreamInterface $stream, $maxLength = null)
188 + public static function readLine(StreamInterface $stream, ?int $maxLength = null) : string
200 189 {
201 190 $buffer = '';
202 191 $size = 0;
203 192 while (!$stream->eof()) {
204 - // Using a loose equality here to match on '' and false.
205 - if (null == ($byte = $stream->read(1))) {
193 + if ('' === ($byte = $stream->read(1))) {
206 194 return $buffer;
207 195 }
208 196 $buffer .= $byte;
209 197 // Break when a new line is found or the max length - 1 is reached
@@ -213,8 +201,19 @@
213 201 }
214 202 return $buffer;
215 203 }
216 204 /**
205 + * Redact the password in the user info part of a URI.
206 + */
207 + public static function redactUserInfo(UriInterface $uri) : UriInterface
208 + {
209 + $userInfo = $uri->getUserInfo();
210 + if (\false !== ($pos = \strpos($userInfo, ':'))) {
211 + return $uri->withUserInfo(\substr($userInfo, 0, $pos), '***');
212 + }
213 + return $uri;
214 + }
215 + /**
217 216 * Create a new stream based on the input type.
218 217 *
219 218 * Options is an associative array that can contain the following keys:
220 219 * - metadata: Array of custom metadata.
@@ -242,20 +241,18 @@
242 241 * number of requested bytes are available. Any additional bytes will be
243 242 * buffered and used in subsequent reads.
244 243 *
245 244 * @param resource|string|int|float|bool|StreamInterface|callable|\Iterator|null $resource Entity body data
246 - * @param array $options Additional options
245 + * @param array{size?: int, metadata?: array} $options Additional options
247 246 *
248 - * @return StreamInterface
249 - *
250 247 * @throws \InvalidArgumentException if the $resource arg is not valid.
251 248 */
252 - public static function streamFor($resource = '', array $options = [])
249 + public static function streamFor($resource = '', array $options = []) : StreamInterface
253 250 {
254 251 if (\is_scalar($resource)) {
255 252 $stream = self::tryFopen('php://temp', 'r+');
256 253 if ($resource !== '') {
257 - \fwrite($stream, $resource);
254 + \fwrite($stream, (string) $resource);
258 255 \fseek($stream, 0);
259 256 }
260 257 return new Stream($stream, $options);
261 258 }
@@ -264,17 +261,18 @@
264 261 /*
265 262 * The 'php://input' is a special stream with quirks and inconsistencies.
266 263 * We avoid using that stream by reading it into php://temp
267 264 */
268 - $metaData = \stream_get_meta_data($resource);
269 - if (isset($metaData['uri']) && $metaData['uri'] === 'php://input') {
265 + /** @var resource $resource */
266 + if ((\stream_get_meta_data($resource)['uri'] ?? '') === 'php://input') {
270 267 $stream = self::tryFopen('php://temp', 'w+');
271 - \fwrite($stream, \stream_get_contents($resource));
268 + \stream_copy_to_stream($resource, $stream);
272 269 \fseek($stream, 0);
273 270 $resource = $stream;
274 271 }
275 272 return new Stream($resource, $options);
276 273 case 'object':
274 + /** @var object $resource */
277 275 if ($resource instanceof StreamInterface) {
278 276 return $resource;
279 277 } elseif ($resource instanceof \Iterator) {
280 278 return new PumpStream(function () use($resource) {
@@ -285,9 +283,9 @@
285 283 $resource->next();
286 284 return $result;
287 285 }, $options);
288 286 } elseif (\method_exists($resource, '__toString')) {
289 - return Utils::streamFor((string) $resource, $options);
287 + return self::streamFor((string) $resource, $options);
290 288 }
291 289 break;
292 290 case 'NULL':
293 291 return new Stream(self::tryFopen('php://temp', 'r+'), $options);
@@ -309,16 +307,17 @@
309 307 * @return resource
310 308 *
311 309 * @throws \RuntimeException if the file cannot be opened
312 310 */
313 - public static function tryFopen($filename, $mode)
311 + public static function tryFopen(string $filename, string $mode)
314 312 {
315 313 $ex = null;
316 - \set_error_handler(function () use($filename, $mode, &$ex) {
317 - $ex = new \RuntimeException(\sprintf('Unable to open "%s" using mode "%s": %s', $filename, $mode, \func_get_args()[1]));
314 + \set_error_handler(static function (int $errno, string $errstr) use($filename, $mode, &$ex) : bool {
315 + $ex = new \RuntimeException(\sprintf('Unable to open "%s" using mode "%s": %s', $filename, $mode, $errstr));
318 316 return \true;
319 317 });
320 318 try {
319 + /** @var resource $handle */
321 320 $handle = \fopen($filename, $mode);
322 321 } catch (\Throwable $e) {
323 322 $ex = new \RuntimeException(\sprintf('Unable to open "%s" using mode "%s": %s', $filename, $mode, $e->getMessage()), 0, $e);
324 323 }
@@ -323,14 +322,48 @@
323 322 $ex = new \RuntimeException(\sprintf('Unable to open "%s" using mode "%s": %s', $filename, $mode, $e->getMessage()), 0, $e);
324 323 }
325 324 \restore_error_handler();
326 325 if ($ex) {
327 - /** @var $ex \RuntimeException */
326 + /** @var \RuntimeException $ex */
328 327 throw $ex;
329 328 }
330 329 return $handle;
331 330 }
332 331 /**
332 + * Safely gets the contents of a given stream.
333 + *
334 + * When stream_get_contents fails, PHP normally raises a warning. This
335 + * function adds an error handler that checks for errors and throws an
336 + * exception instead.
337 + *
338 + * @param resource $stream
339 + *
340 + * @throws \RuntimeException if the stream cannot be read
341 + */
342 + public static function tryGetContents($stream) : string
343 + {
344 + $ex = null;
345 + \set_error_handler(static function (int $errno, string $errstr) use(&$ex) : bool {
346 + $ex = new \RuntimeException(\sprintf('Unable to read stream contents: %s', $errstr));
347 + return \true;
348 + });
349 + try {
350 + /** @var string|false $contents */
351 + $contents = \stream_get_contents($stream);
352 + if ($contents === \false) {
353 + $ex = new \RuntimeException('Unable to read stream contents');
354 + }
355 + } catch (\Throwable $e) {
356 + $ex = new \RuntimeException(\sprintf('Unable to read stream contents: %s', $e->getMessage()), 0, $e);
357 + }
358 + \restore_error_handler();
359 + if ($ex) {
360 + /** @var \RuntimeException $ex */
361 + throw $ex;
362 + }
363 + return $contents;
364 + }
365 + /**
333 366 * Returns a UriInterface for the given value.
334 367 *
335 368 * This function accepts a string or UriInterface and returns a
336 369 * UriInterface for the given value. If the value is already a
@@ -337,13 +370,11 @@
337 370 * UriInterface, it is returned as-is.
338 371 *
339 372 * @param string|UriInterface $uri
340 373 *
341 - * @return UriInterface
342 - *
343 374 * @throws \InvalidArgumentException
344 375 */
345 - public static function uriFor($uri)
376 + public static function uriFor($uri) : UriInterface
346 377 {
347 378 if ($uri instanceof UriInterface) {
348 379 return $uri;
349 380 }