diff --git a/composer.json b/composer.json index 8a6e9f4..8701a52 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,8 @@ "sebastian/comparator": ">=2" }, "suggest": { + "ext-json": "To detect JSON responses with the ContentTypePlugin", + "ext-libxml": "To detect XML responses with the ContentTypePlugin", "php-http/logger-plugin": "PSR-3 Logger plugin", "php-http/cache-plugin": "PSR-6 Cache plugin", "php-http/stopwatch-plugin": "Symfony Stopwatch plugin" diff --git a/src/Plugin/ContentTypePlugin.php b/src/Plugin/ContentTypePlugin.php index f6dcddd..f3944dd 100644 --- a/src/Plugin/ContentTypePlugin.php +++ b/src/Plugin/ContentTypePlugin.php @@ -94,11 +94,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl return $next($request); } - /** - * @param $stream StreamInterface - */ - private function isJson($stream): bool + private function isJson(StreamInterface $stream): bool { + if (!function_exists('json_decode')) { + return false; + } $stream->rewind(); json_decode($stream->getContents()); @@ -108,17 +108,18 @@ private function isJson($stream): bool /** * @param $stream StreamInterface - * - * @return \SimpleXMLElement|false */ - private function isXml($stream) + private function isXml(StreamInterface $stream): bool { + if (!function_exists('simplexml_load_string')) { + return false; + } $stream->rewind(); $previousValue = libxml_use_internal_errors(true); $isXml = simplexml_load_string($stream->getContents()); libxml_use_internal_errors($previousValue); - return $isXml; + return false !== $isXml; } } diff --git a/src/Plugin/CookiePlugin.php b/src/Plugin/CookiePlugin.php index 8f8d5b0..8399210 100644 --- a/src/Plugin/CookiePlugin.php +++ b/src/Plugin/CookiePlugin.php @@ -91,15 +91,13 @@ public function handleRequest(RequestInterface $request, callable $next, callabl /** * Creates a cookie from a string. * - * @param $setCookie - * * @return Cookie|null * * @throws TransferException */ - private function createCookie(RequestInterface $request, $setCookie) + private function createCookie(RequestInterface $request, string $setCookieHeader) { - $parts = array_map('trim', explode(';', $setCookie)); + $parts = array_map('trim', explode(';', $setCookieHeader)); if (empty($parts) || !strpos($parts[0], '=')) { return null; @@ -169,11 +167,9 @@ private function createCookie(RequestInterface $request, $setCookie) /** * Separates key/value pair from cookie. * - * @param $part - * - * @return array + * @param string $part A single cookie value in format key=value */ - private function createValueKey($part) + private function createValueKey(string $part): array { $parts = explode('=', $part, 2); $key = trim($parts[0]); diff --git a/src/Plugin/DecoderPlugin.php b/src/Plugin/DecoderPlugin.php index b1ede5d..271ad3c 100644 --- a/src/Plugin/DecoderPlugin.php +++ b/src/Plugin/DecoderPlugin.php @@ -68,12 +68,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl /** * Decode a response body given its Transfer-Encoding or Content-Encoding value. - * - * @param ResponseInterface $response Response to decode - * - * @return ResponseInterface New response decoded */ - private function decodeResponse(ResponseInterface $response) + private function decodeResponse(ResponseInterface $response): ResponseInterface { $response = $this->decodeOnEncodingHeader('Transfer-Encoding', $response); @@ -86,13 +82,8 @@ private function decodeResponse(ResponseInterface $response) /** * Decode a response on a specific header (content encoding or transfer encoding mainly). - * - * @param string $headerName Name of the header - * @param ResponseInterface $response Response - * - * @return ResponseInterface A new instance of the response decoded */ - private function decodeOnEncodingHeader($headerName, ResponseInterface $response) + private function decodeOnEncodingHeader(string $headerName, ResponseInterface $response): ResponseInterface { if ($response->hasHeader($headerName)) { $encodings = $response->getHeader($headerName); @@ -123,11 +114,9 @@ private function decodeOnEncodingHeader($headerName, ResponseInterface $response /** * Decorate a stream given an encoding. * - * @param string $encoding - * * @return StreamInterface|false A new stream interface or false if encoding is not supported */ - private function decorateStream($encoding, StreamInterface $stream) + private function decorateStream(string $encoding, StreamInterface $stream) { if ('chunked' === strtolower($encoding)) { return new Encoding\DechunkStream($stream);