| @@ -1,13 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | declare (strict_types=1); |
| 4 | -namespace Dudlewebs\WPMCS\GuzzleHttp\Psr7; | |
| 4 | +namespace Dudlewebs\WPMCS\GCP\GuzzleHttp\Psr7; | |
| 5 | 5 | |
| 6 | -use Dudlewebs\WPMCS\Psr\Http\Message\RequestInterface; | |
| 7 | -use Dudlewebs\WPMCS\Psr\Http\Message\ServerRequestInterface; | |
| 8 | -use Dudlewebs\WPMCS\Psr\Http\Message\StreamInterface; | |
| 9 | -use Dudlewebs\WPMCS\Psr\Http\Message\UriInterface; | |
| 6 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\RequestInterface; | |
| 7 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\ServerRequestInterface; | |
| 8 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\StreamInterface; | |
| 9 | +use Dudlewebs\WPMCS\GCP\Psr\Http\Message\UriInterface; | |
| 10 | 10 | final class Utils |
| 11 | 11 | { |
| 12 | 12 | /** |
| 13 | 13 | * Remove the items given by the keys, case insensitively from the data. |
| @@ -13,16 +13,16 @@ | ||
| 13 | 13 | * Remove the items given by the keys, case insensitively from the data. |
| 14 | 14 | * |
| 15 | 15 | * @param (string|int)[] $keys |
| 16 | 16 | */ |
| 17 | - public static function caselessRemove(array $keys, array $data): array | |
| 17 | + public static function caselessRemove(array $keys, array $data) : array | |
| 18 | 18 | { |
| 19 | 19 | $result = []; |
| 20 | 20 | foreach ($keys as &$key) { |
| 21 | - $key = strtolower((string) $key); | |
| 21 | + $key = \strtolower((string) $key); | |
| 22 | 22 | } |
| 23 | 23 | foreach ($data as $k => $v) { |
| 24 | - if (!in_array(strtolower((string) $k), $keys)) { | |
| 24 | + if (!\in_array(\strtolower((string) $k), $keys)) { | |
| 25 | 25 | $result[$k] = $v; |
| 26 | 26 | } |
| 27 | 27 | } |
| 28 | 28 | return $result; |
| @@ -37,9 +37,9 @@ | ||
| 37 | 37 | * to read the entire stream. |
| 38 | 38 | * |
| 39 | 39 | * @throws \RuntimeException on error. |
| 40 | 40 | */ |
| 41 | - public static function copyToStream(StreamInterface $source, StreamInterface $dest, int $maxLen = -1): void | |
| 41 | + public static function copyToStream(StreamInterface $source, StreamInterface $dest, int $maxLen = -1) : void | |
| 42 | 42 | { |
| 43 | 43 | $bufferSize = 8192; |
| 44 | 44 | if ($maxLen === -1) { |
| 45 | 45 | while (!$source->eof()) { |
| @@ -49,10 +49,10 @@ | ||
| 49 | 49 | } |
| 50 | 50 | } else { |
| 51 | 51 | $remaining = $maxLen; |
| 52 | 52 | while ($remaining > 0 && !$source->eof()) { |
| 53 | - $buf = $source->read(min($bufferSize, $remaining)); | |
| 54 | - $len = strlen($buf); | |
| 53 | + $buf = $source->read(\min($bufferSize, $remaining)); | |
| 54 | + $len = \strlen($buf); | |
| 55 | 55 | if (!$len) { |
| 56 | 56 | break; |
| 57 | 57 | } |
| 58 | 58 | $remaining -= $len; |
| @@ -69,9 +69,9 @@ | ||
| 69 | 69 | * to read the entire stream. |
| 70 | 70 | * |
| 71 | 71 | * @throws \RuntimeException on error. |
| 72 | 72 | */ |
| 73 | - public static function copyToString(StreamInterface $stream, int $maxLen = -1): string | |
| 73 | + public static function copyToString(StreamInterface $stream, int $maxLen = -1) : string | |
| 74 | 74 | { |
| 75 | 75 | $buffer = ''; |
| 76 | 76 | if ($maxLen === -1) { |
| 77 | 77 | while (!$stream->eof()) { |
| @@ -89,9 +89,9 @@ | ||
| 89 | 89 | if ($buf === '') { |
| 90 | 90 | break; |
| 91 | 91 | } |
| 92 | 92 | $buffer .= $buf; |
| 93 | - $len = strlen($buffer); | |
| 93 | + $len = \strlen($buffer); | |
| 94 | 94 | } |
| 95 | 95 | return $buffer; |
| 96 | 96 | } |
| 97 | 97 | /** |
| @@ -105,19 +105,19 @@ | ||
| 105 | 105 | * @param bool $rawOutput Whether or not to use raw output |
| 106 | 106 | * |
| 107 | 107 | * @throws \RuntimeException on error. |
| 108 | 108 | */ |
| 109 | - public static function hash(StreamInterface $stream, string $algo, bool $rawOutput = \false): string | |
| 109 | + public static function hash(StreamInterface $stream, string $algo, bool $rawOutput = \false) : string | |
| 110 | 110 | { |
| 111 | 111 | $pos = $stream->tell(); |
| 112 | 112 | if ($pos > 0) { |
| 113 | 113 | $stream->rewind(); |
| 114 | 114 | } |
| 115 | - $ctx = hash_init($algo); | |
| 115 | + $ctx = \hash_init($algo); | |
| 116 | 116 | while (!$stream->eof()) { |
| 117 | - hash_update($ctx, $stream->read(1048576)); | |
| 117 | + \hash_update($ctx, $stream->read(1048576)); | |
| 118 | 118 | } |
| 119 | - $out = hash_final($ctx, $rawOutput); | |
| 119 | + $out = \hash_final($ctx, $rawOutput); | |
| 120 | 120 | $stream->seek($pos); |
| 121 | 121 | return $out; |
| 122 | 122 | } |
| 123 | 123 | /** |
| @@ -137,9 +137,9 @@ | ||
| 137 | 137 | * |
| 138 | 138 | * @param RequestInterface $request Request to clone and modify. |
| 139 | 139 | * @param array $changes Changes to apply. |
| 140 | 140 | */ |
| 141 | - public static function modifyRequest(RequestInterface $request, array $changes): RequestInterface | |
| 141 | + public static function modifyRequest(RequestInterface $request, array $changes) : RequestInterface | |
| 142 | 142 | { |
| 143 | 143 | if (!$changes) { |
| 144 | 144 | return $request; |
| 145 | 145 | } |
| @@ -163,9 +163,9 @@ | ||
| 163 | 163 | if (!empty($changes['remove_headers'])) { |
| 164 | 164 | $headers = self::caselessRemove($changes['remove_headers'], $headers); |
| 165 | 165 | } |
| 166 | 166 | if (!empty($changes['set_headers'])) { |
| 167 | - $headers = self::caselessRemove(array_keys($changes['set_headers']), $headers); | |
| 167 | + $headers = self::caselessRemove(\array_keys($changes['set_headers']), $headers); | |
| 168 | 168 | $headers = $changes['set_headers'] + $headers; |
| 169 | 169 | } |
| 170 | 170 | if (isset($changes['query'])) { |
| 171 | 171 | $uri = $uri->withQuery($changes['query']); |
| @@ -184,14 +184,14 @@ | ||
| 184 | 184 | * |
| 185 | 185 | * @param StreamInterface $stream Stream to read from |
| 186 | 186 | * @param int|null $maxLength Maximum buffer length |
| 187 | 187 | */ |
| 188 | - public static function readLine(StreamInterface $stream, ?int $maxLength = null): string | |
| 188 | + public static function readLine(StreamInterface $stream, ?int $maxLength = null) : string | |
| 189 | 189 | { |
| 190 | 190 | $buffer = ''; |
| 191 | 191 | $size = 0; |
| 192 | 192 | while (!$stream->eof()) { |
| 193 | - if ('' === $byte = $stream->read(1)) { | |
| 193 | + if ('' === ($byte = $stream->read(1))) { | |
| 194 | 194 | return $buffer; |
| 195 | 195 | } |
| 196 | 196 | $buffer .= $byte; |
| 197 | 197 | // Break when a new line is found or the max length - 1 is reached |
| @@ -203,12 +203,12 @@ | ||
| 203 | 203 | } |
| 204 | 204 | /** |
| 205 | 205 | * Redact the password in the user info part of a URI. |
| 206 | 206 | */ |
| 207 | - public static function redactUserInfo(UriInterface $uri): UriInterface | |
| 207 | + public static function redactUserInfo(UriInterface $uri) : UriInterface | |
| 208 | 208 | { |
| 209 | 209 | $userInfo = $uri->getUserInfo(); |
| 210 | - if (\false !== $pos = \strpos($userInfo, ':')) { | |
| 210 | + if (\false !== ($pos = \strpos($userInfo, ':'))) { | |
| 211 | 211 | return $uri->withUserInfo(\substr($userInfo, 0, $pos), '***'); |
| 212 | 212 | } |
| 213 | 213 | return $uri; |
| 214 | 214 | } |
| @@ -245,19 +245,19 @@ | ||
| 245 | 245 | * @param array{size?: int, metadata?: array} $options Additional options |
| 246 | 246 | * |
| 247 | 247 | * @throws \InvalidArgumentException if the $resource arg is not valid. |
| 248 | 248 | */ |
| 249 | - public static function streamFor($resource = '', array $options = []): StreamInterface | |
| 249 | + public static function streamFor($resource = '', array $options = []) : StreamInterface | |
| 250 | 250 | { |
| 251 | - if (is_scalar($resource)) { | |
| 251 | + if (\is_scalar($resource)) { | |
| 252 | 252 | $stream = self::tryFopen('php://temp', 'r+'); |
| 253 | 253 | if ($resource !== '') { |
| 254 | - fwrite($stream, (string) $resource); | |
| 255 | - fseek($stream, 0); | |
| 254 | + \fwrite($stream, (string) $resource); | |
| 255 | + \fseek($stream, 0); | |
| 256 | 256 | } |
| 257 | 257 | return new Stream($stream, $options); |
| 258 | 258 | } |
| 259 | - switch (gettype($resource)) { | |
| 259 | + switch (\gettype($resource)) { | |
| 260 | 260 | case 'resource': |
| 261 | 261 | /* |
| 262 | 262 | * The 'php://input' is a special stream with quirks and inconsistencies. |
| 263 | 263 | * We avoid using that stream by reading it into php://temp |
| @@ -264,10 +264,10 @@ | ||
| 264 | 264 | */ |
| 265 | 265 | /** @var resource $resource */ |
| 266 | 266 | if ((\stream_get_meta_data($resource)['uri'] ?? '') === 'php://input') { |
| 267 | 267 | $stream = self::tryFopen('php://temp', 'w+'); |
| 268 | - stream_copy_to_stream($resource, $stream); | |
| 269 | - fseek($stream, 0); | |
| 268 | + \stream_copy_to_stream($resource, $stream); | |
| 269 | + \fseek($stream, 0); | |
| 270 | 270 | $resource = $stream; |
| 271 | 271 | } |
| 272 | 272 | return new Stream($resource, $options); |
| 273 | 273 | case 'object': |
| @@ -274,9 +274,9 @@ | ||
| 274 | 274 | /** @var object $resource */ |
| 275 | 275 | if ($resource instanceof StreamInterface) { |
| 276 | 276 | return $resource; |
| 277 | 277 | } elseif ($resource instanceof \Iterator) { |
| 278 | - return new PumpStream(function () use ($resource) { | |
| 278 | + return new PumpStream(function () use($resource) { | |
| 279 | 279 | if (!$resource->valid()) { |
| 280 | 280 | return \false; |
| 281 | 281 | } |
| 282 | 282 | $result = $resource->current(); |
| @@ -282,9 +282,9 @@ | ||
| 282 | 282 | $result = $resource->current(); |
| 283 | 283 | $resource->next(); |
| 284 | 284 | return $result; |
| 285 | 285 | }, $options); |
| 286 | - } elseif (method_exists($resource, '__toString')) { | |
| 286 | + } elseif (\method_exists($resource, '__toString')) { | |
| 287 | 287 | return self::streamFor((string) $resource, $options); |
| 288 | 288 | } |
| 289 | 289 | break; |
| 290 | 290 | case 'NULL': |
| @@ -289,12 +289,12 @@ | ||
| 289 | 289 | break; |
| 290 | 290 | case 'NULL': |
| 291 | 291 | return new Stream(self::tryFopen('php://temp', 'r+'), $options); |
| 292 | 292 | } |
| 293 | - if (is_callable($resource)) { | |
| 293 | + if (\is_callable($resource)) { | |
| 294 | 294 | return new PumpStream($resource, $options); |
| 295 | 295 | } |
| 296 | - throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource)); | |
| 296 | + throw new \InvalidArgumentException('Invalid resource type: ' . \gettype($resource)); | |
| 297 | 297 | } |
| 298 | 298 | /** |
| 299 | 299 | * Safely opens a PHP stream resource using a filename. |
| 300 | 300 | * |
| @@ -310,21 +310,21 @@ | ||
| 310 | 310 | */ |
| 311 | 311 | public static function tryFopen(string $filename, string $mode) |
| 312 | 312 | { |
| 313 | 313 | $ex = null; |
| 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)); | |
| 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)); | |
| 316 | 316 | return \true; |
| 317 | 317 | }); |
| 318 | 318 | try { |
| 319 | 319 | /** @var resource $handle */ |
| 320 | - $handle = fopen($filename, $mode); | |
| 320 | + $handle = \fopen($filename, $mode); | |
| 321 | 321 | } catch (\Throwable $e) { |
| 322 | - $ex = new \RuntimeException(sprintf('Unable to open "%s" using mode "%s": %s', $filename, $mode, $e->getMessage()), 0, $e); | |
| 322 | + $ex = new \RuntimeException(\sprintf('Unable to open "%s" using mode "%s": %s', $filename, $mode, $e->getMessage()), 0, $e); | |
| 323 | 323 | } |
| 324 | - restore_error_handler(); | |
| 324 | + \restore_error_handler(); | |
| 325 | 325 | if ($ex) { |
| 326 | - /** @var $ex \RuntimeException */ | |
| 326 | + /** @var \RuntimeException $ex */ | |
| 327 | 327 | throw $ex; |
| 328 | 328 | } |
| 329 | 329 | return $handle; |
| 330 | 330 | } |
| @@ -338,27 +338,27 @@ | ||
| 338 | 338 | * @param resource $stream |
| 339 | 339 | * |
| 340 | 340 | * @throws \RuntimeException if the stream cannot be read |
| 341 | 341 | */ |
| 342 | - public static function tryGetContents($stream): string | |
| 342 | + public static function tryGetContents($stream) : string | |
| 343 | 343 | { |
| 344 | 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)); | |
| 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 | 347 | return \true; |
| 348 | 348 | }); |
| 349 | 349 | try { |
| 350 | 350 | /** @var string|false $contents */ |
| 351 | - $contents = stream_get_contents($stream); | |
| 351 | + $contents = \stream_get_contents($stream); | |
| 352 | 352 | if ($contents === \false) { |
| 353 | 353 | $ex = new \RuntimeException('Unable to read stream contents'); |
| 354 | 354 | } |
| 355 | 355 | } catch (\Throwable $e) { |
| 356 | - $ex = new \RuntimeException(sprintf('Unable to read stream contents: %s', $e->getMessage()), 0, $e); | |
| 356 | + $ex = new \RuntimeException(\sprintf('Unable to read stream contents: %s', $e->getMessage()), 0, $e); | |
| 357 | 357 | } |
| 358 | - restore_error_handler(); | |
| 358 | + \restore_error_handler(); | |
| 359 | 359 | if ($ex) { |
| 360 | - /** @var $ex \RuntimeException */ | |
| 360 | + /** @var \RuntimeException $ex */ | |
| 361 | 361 | throw $ex; |
| 362 | 362 | } |
| 363 | 363 | return $contents; |
| 364 | 364 | } |
| @@ -372,14 +372,14 @@ | ||
| 372 | 372 | * @param string|UriInterface $uri |
| 373 | 373 | * |
| 374 | 374 | * @throws \InvalidArgumentException |
| 375 | 375 | */ |
| 376 | - public static function uriFor($uri): UriInterface | |
| 376 | + public static function uriFor($uri) : UriInterface | |
| 377 | 377 | { |
| 378 | 378 | if ($uri instanceof UriInterface) { |
| 379 | 379 | return $uri; |
| 380 | 380 | } |
| 381 | - if (is_string($uri)) { | |
| 381 | + if (\is_string($uri)) { | |
| 382 | 382 | return new Uri($uri); |
| 383 | 383 | } |
| 384 | 384 | throw new \InvalidArgumentException('URI must be a string or UriInterface'); |
| 385 | 385 | } |