Skip to content

Commit 6a2df0d

Browse files
authored
Merge pull request #145 from php-http/more-typing
add missing type declarations
2 parents 0bf5422 + 7d80627 commit 6a2df0d

File tree

4 files changed

+18
-30
lines changed

4 files changed

+18
-30
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"sebastian/comparator": ">=2"
2626
},
2727
"suggest": {
28+
"ext-json": "To detect JSON responses with the ContentTypePlugin",
29+
"ext-libxml": "To detect XML responses with the ContentTypePlugin",
2830
"php-http/logger-plugin": "PSR-3 Logger plugin",
2931
"php-http/cache-plugin": "PSR-6 Cache plugin",
3032
"php-http/stopwatch-plugin": "Symfony Stopwatch plugin"

src/Plugin/ContentTypePlugin.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
9494
return $next($request);
9595
}
9696

97-
/**
98-
* @param $stream StreamInterface
99-
*/
100-
private function isJson($stream): bool
97+
private function isJson(StreamInterface $stream): bool
10198
{
99+
if (!function_exists('json_decode')) {
100+
return false;
101+
}
102102
$stream->rewind();
103103

104104
json_decode($stream->getContents());
@@ -108,17 +108,18 @@ private function isJson($stream): bool
108108

109109
/**
110110
* @param $stream StreamInterface
111-
*
112-
* @return \SimpleXMLElement|false
113111
*/
114-
private function isXml($stream)
112+
private function isXml(StreamInterface $stream): bool
115113
{
114+
if (!function_exists('simplexml_load_string')) {
115+
return false;
116+
}
116117
$stream->rewind();
117118

118119
$previousValue = libxml_use_internal_errors(true);
119120
$isXml = simplexml_load_string($stream->getContents());
120121
libxml_use_internal_errors($previousValue);
121122

122-
return $isXml;
123+
return false !== $isXml;
123124
}
124125
}

src/Plugin/CookiePlugin.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,13 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
9191
/**
9292
* Creates a cookie from a string.
9393
*
94-
* @param $setCookie
95-
*
9694
* @return Cookie|null
9795
*
9896
* @throws TransferException
9997
*/
100-
private function createCookie(RequestInterface $request, $setCookie)
98+
private function createCookie(RequestInterface $request, string $setCookieHeader)
10199
{
102-
$parts = array_map('trim', explode(';', $setCookie));
100+
$parts = array_map('trim', explode(';', $setCookieHeader));
103101

104102
if (empty($parts) || !strpos($parts[0], '=')) {
105103
return null;
@@ -169,11 +167,9 @@ private function createCookie(RequestInterface $request, $setCookie)
169167
/**
170168
* Separates key/value pair from cookie.
171169
*
172-
* @param $part
173-
*
174-
* @return array
170+
* @param string $part A single cookie value in format key=value
175171
*/
176-
private function createValueKey($part)
172+
private function createValueKey(string $part): array
177173
{
178174
$parts = explode('=', $part, 2);
179175
$key = trim($parts[0]);

src/Plugin/DecoderPlugin.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6868

6969
/**
7070
* Decode a response body given its Transfer-Encoding or Content-Encoding value.
71-
*
72-
* @param ResponseInterface $response Response to decode
73-
*
74-
* @return ResponseInterface New response decoded
7571
*/
76-
private function decodeResponse(ResponseInterface $response)
72+
private function decodeResponse(ResponseInterface $response): ResponseInterface
7773
{
7874
$response = $this->decodeOnEncodingHeader('Transfer-Encoding', $response);
7975

@@ -86,13 +82,8 @@ private function decodeResponse(ResponseInterface $response)
8682

8783
/**
8884
* Decode a response on a specific header (content encoding or transfer encoding mainly).
89-
*
90-
* @param string $headerName Name of the header
91-
* @param ResponseInterface $response Response
92-
*
93-
* @return ResponseInterface A new instance of the response decoded
9485
*/
95-
private function decodeOnEncodingHeader($headerName, ResponseInterface $response)
86+
private function decodeOnEncodingHeader(string $headerName, ResponseInterface $response): ResponseInterface
9687
{
9788
if ($response->hasHeader($headerName)) {
9889
$encodings = $response->getHeader($headerName);
@@ -123,11 +114,9 @@ private function decodeOnEncodingHeader($headerName, ResponseInterface $response
123114
/**
124115
* Decorate a stream given an encoding.
125116
*
126-
* @param string $encoding
127-
*
128117
* @return StreamInterface|false A new stream interface or false if encoding is not supported
129118
*/
130-
private function decorateStream($encoding, StreamInterface $stream)
119+
private function decorateStream(string $encoding, StreamInterface $stream)
131120
{
132121
if ('chunked' === strtolower($encoding)) {
133122
return new Encoding\DechunkStream($stream);

0 commit comments

Comments
 (0)