Skip to content

Commit 454b419

Browse files
committed
:octocat: explicit nullable types
1 parent 3e31cde commit 454b419

15 files changed

+74
-43
lines changed

src/HeaderUtil.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTP\Utils;
1214

1315
use function array_keys, array_values, count, explode, implode,
@@ -63,7 +65,7 @@ public static function normalize(iterable $headers):array{
6365
}
6466

6567
$key = self::normalizeHeaderName($key);
66-
$val = trim($val);
68+
$val = trim((string)($val ?? ''));
6769

6870
// skip if the header already exists but the current value is empty
6971
if(isset($normalized[$key]) && empty($val)){

src/MessageUtil.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTP\Utils;
1214

13-
use Psr\Http\Message\{MessageInterface, RequestInterface, ResponseInterface};
15+
use Psr\Http\Message\{MessageInterface, RequestInterface, ResponseInterface, ServerRequestInterface};
1416
use RuntimeException, Throwable;
1517
use function call_user_func, extension_loaded, function_exists, gzdecode, gzinflate, gzuncompress, implode,
1618
in_array, json_decode, json_encode, simplexml_load_string, sprintf, strtolower, trim;
@@ -39,14 +41,14 @@ public static function getContents(MessageInterface $message):string{
3941
/**
4042
* @throws \JsonException
4143
*/
42-
public static function decodeJSON(MessageInterface $message, bool $assoc = null):mixed{
44+
public static function decodeJSON(MessageInterface $message, bool|null $assoc = null):mixed{
4345
return json_decode(self::getContents($message), ($assoc ?? false), 512, JSON_THROW_ON_ERROR);
4446
}
4547

4648
/**
4749
* @return \SimpleXMLElement|\stdClass|mixed
4850
*/
49-
public static function decodeXML(MessageInterface $message, bool $assoc = null):mixed{
51+
public static function decodeXML(MessageInterface $message, bool|null $assoc = null):mixed{
5052
$data = simplexml_load_string(self::getContents($message));
5153

5254
return $assoc === true
@@ -57,7 +59,7 @@ public static function decodeXML(MessageInterface $message, bool $assoc = null):
5759
/**
5860
* Returns the string representation of an HTTP message. (from Guzzle)
5961
*/
60-
public static function toString(MessageInterface $message, bool $appendBody = null):string{
62+
public static function toString(MessageInterface $message, bool|null $appendBody = null):string{
6163
$appendBody ??= true;
6264
$msg = '';
6365

@@ -144,7 +146,9 @@ protected static function call_decompress_func(string $func, string $data):strin
144146
/**
145147
* Sets a Content-Length header in the given message in case it does not exist and body size is not null
146148
*/
147-
public static function setContentLengthHeader(MessageInterface $message):MessageInterface{
149+
public static function setContentLengthHeader(
150+
MessageInterface $message
151+
):MessageInterface|RequestInterface|ResponseInterface|ServerRequestInterface{
148152
$bodySize = $message->getBody()->getSize();
149153

150154
if(!$message->hasHeader('Content-Length') && $bodySize !== null && $bodySize > 0){
@@ -162,9 +166,9 @@ public static function setContentLengthHeader(MessageInterface $message):Message
162166
*/
163167
public static function setContentTypeHeader(
164168
MessageInterface $message,
165-
string $filename = null,
166-
string $extension = null
167-
):MessageInterface{
169+
string|null $filename = null,
170+
string|null $extension = null,
171+
):MessageInterface|RequestInterface|ResponseInterface|ServerRequestInterface{
168172
$mime = (
169173
MimeTypeUtil::getFromExtension(trim(($extension ?? ''), ".\t\n\r\0\x0B"))
170174
?? MimeTypeUtil::getFromFilename(($filename ?? ''))

src/MimeTypeUtil.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTP\Utils;
1214

1315
use finfo;
@@ -1306,21 +1308,21 @@ final class MimeTypeUtil{
13061308
/**
13071309
* Get the mime type for the given file extension
13081310
*/
1309-
public static function getFromExtension(string $extension):?string{
1311+
public static function getFromExtension(string $extension):string|null{
13101312
return (self::MIMETYPES[strtolower($extension)] ?? null);
13111313
}
13121314

13131315
/**
13141316
* Get the mime type from a file name
13151317
*/
1316-
public static function getFromFilename(string $filename):?string{
1318+
public static function getFromFilename(string $filename):string|null{
13171319
return self::getFromExtension(pathinfo($filename, PATHINFO_EXTENSION));
13181320
}
13191321

13201322
/**
13211323
* Get the mime type from the given content
13221324
*/
1323-
public static function getFromContent(string $content):?string{
1325+
public static function getFromContent(string $content):string|null{
13241326
$finfo = new finfo(FILEINFO_MIME_TYPE);
13251327
$mime = $finfo->buffer($content);
13261328

src/QueryUtil.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTP\Utils;
1214

1315
use InvalidArgumentException;
@@ -44,7 +46,11 @@ final class QueryUtil{
4446
*
4547
* @return array
4648
*/
47-
public static function cleanParams(iterable $params, int $bool_cast = null, bool $remove_empty = null):array{
49+
public static function cleanParams(
50+
iterable $params,
51+
int|null $bool_cast = null,
52+
bool|null $remove_empty = null,
53+
):array{
4854
$bool_cast ??= self::BOOLEANS_AS_BOOL;
4955
$remove_empty ??= true;
5056

@@ -97,7 +103,12 @@ public static function cleanParams(iterable $params, int $bool_cast = null, bool
97103
* @link https://github.com/abraham/twitteroauth/blob/57108b31f208d0066ab90a23257cdd7bb974c67d/src/Util.php#L84-L122
98104
* @link https://github.com/guzzle/psr7/blob/c0dcda9f54d145bd4d062a6d15f54931a67732f9/src/Query.php#L59-L113
99105
*/
100-
public static function build(array $params, int $encoding = null, string $delimiter = null, string $enclosure = null):string{
106+
public static function build(
107+
array $params,
108+
int|null $encoding = null,
109+
string|null $delimiter = null,
110+
string|null $enclosure = null,
111+
):string{
101112

102113
if(empty($params)){
103114
return '';
@@ -174,7 +185,7 @@ public static function merge(string $uri, array $query):string{
174185
*
175186
* @link https://github.com/guzzle/psr7/blob/c0dcda9f54d145bd4d062a6d15f54931a67732f9/src/Query.php#L9-L57
176187
*/
177-
public static function parse(string $querystring, int $urlEncoding = null):array{
188+
public static function parse(string $querystring, int|null $urlEncoding = null):array{
178189
$querystring = trim($querystring, '?'); // handle leftover question marks (e.g. Twitter API "next_results")
179190

180191
if($querystring === ''){

src/ServerUtil.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTP\Utils;
1214

1315
use InvalidArgumentException;

src/StreamUtil.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTP\Utils;
1214

1315
use InvalidArgumentException;
@@ -111,7 +113,7 @@ public static function validateMode(string $mode):string{
111113
*
112114
* Returns the stream content as a string, null if an error occurs, e.g. the StreamInterface throws.
113115
*/
114-
public static function getContents(StreamInterface $stream):?string{
116+
public static function getContents(StreamInterface $stream):string|null{
115117

116118
// rewind before read...
117119
if($stream->isSeekable()){
@@ -148,7 +150,7 @@ public static function getContents(StreamInterface $stream):?string{
148150
*
149151
* @throws \RuntimeException
150152
*/
151-
public static function copyToStream(StreamInterface $source, StreamInterface $destination, int $maxLength = null):int{
153+
public static function copyToStream(StreamInterface $source, StreamInterface $destination, int|null $maxLength = null):int{
152154

153155
if(!$source->isReadable() || !$destination->isWritable()){
154156
throw new RuntimeException('$source must be readable and $destination must be writable');
@@ -185,7 +187,7 @@ public static function copyToStream(StreamInterface $source, StreamInterface $de
185187
* @return resource
186188
* @throws \RuntimeException
187189
*/
188-
public static function tryFopen(string $filename, string $mode, $context = null){
190+
public static function tryFopen(string $filename, string $mode, mixed $context = null){
189191
$mode = self::validateMode($mode);
190192
$exception = null;
191193
$message = 'Unable to open "%s" using mode "%s": %s';
@@ -227,7 +229,7 @@ public static function tryFopen(string $filename, string $mode, $context = null)
227229
* @return string
228230
* @throws \RuntimeException
229231
*/
230-
public static function tryGetContents($stream, int $length = null, int $offset = -1):string{
232+
public static function tryGetContents($stream, int|null $length = null, int $offset = -1):string{
231233
$exception = null;
232234
$message = 'Unable to read stream contents: %s';
233235

src/UriUtil.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTP\Utils;
1214

1315
use Psr\Http\Message\UriInterface;
@@ -119,7 +121,7 @@ public static function withoutQueryValue(UriInterface $uri, string $key):UriInte
119121
*
120122
* A value of null will set the query string key without a value, e.g. "key" instead of "key=value".
121123
*/
122-
public static function withQueryValue(UriInterface $uri, string $key, string $value = null):UriInterface{
124+
public static function withQueryValue(UriInterface $uri, string $key, string|null $value = null):UriInterface{
123125
$current = $uri->getQuery();
124126

125127
$result = ($current !== '')
@@ -154,7 +156,7 @@ public static function withQueryValue(UriInterface $uri, string $key, string $va
154156
*
155157
* @link https://github.com/guzzle/psr7/blob/c0dcda9f54d145bd4d062a6d15f54931a67732f9/src/Uri.php#L89-L130
156158
*/
157-
public static function parseUrl(string $url):?array{
159+
public static function parseUrl(string $url):array|null{
158160
// If IPv6
159161
$prefix = '';
160162
/** @noinspection RegExpRedundantEscape */

tests/FactoryTrait.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTPTest\Utils;
1214

1315
use chillerlan\HTTP\Utils\ServerUtil;
@@ -16,11 +18,7 @@
1618
RequestFactoryInterface, ResponseFactoryInterface, ServerRequestFactoryInterface,
1719
StreamFactoryInterface, UploadedFileFactoryInterface, UriFactoryInterface
1820
};
19-
use function class_exists;
20-
use function constant;
21-
use function defined;
22-
use function method_exists;
23-
use function sprintf;
21+
use function class_exists, constant, defined, method_exists, sprintf;
2422

2523
trait FactoryTrait{
2624

tests/HeaderUtilTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTPTest\Utils;
1214

1315
use chillerlan\HTTP\Utils\HeaderUtil;

tests/MessageUtilTest.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTPTest\Utils;
1214

1315
use chillerlan\HTTP\Utils\MessageUtil;
1416
use PHPUnit\Framework\Attributes\DataProvider;
1517
use PHPUnit\Framework\TestCase;
1618
use RuntimeException;
17-
use function extension_loaded;
18-
use function file_get_contents;
19-
use function function_exists;
20-
use function sprintf;
21-
use function str_repeat;
19+
use function extension_loaded, file_get_contents, function_exists, sprintf, str_repeat;
2220

2321
/**
2422
*
@@ -175,7 +173,12 @@ public static function contentTypeProvider():array{
175173
}
176174

177175
#[DataProvider('contentTypeProvider')]
178-
public function testSetContentTypeHeader(string $content, ?string $filename, ?string $extension, string $expectedMIME):void{
176+
public function testSetContentTypeHeader(
177+
string $content,
178+
string|null $filename,
179+
string|null $extension,
180+
string $expectedMIME,
181+
):void{
179182

180183
$message = $this->requestFactory
181184
->createRequest('GET', 'https://example.com')

tests/MimeTypeUtilTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTPTest\Utils;
1214

1315
use chillerlan\HTTP\Utils\MimeTypeUtil;

tests/QueryUtilTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
* @license MIT
1111
*/
1212

13+
declare(strict_types=1);
14+
1315
namespace chillerlan\HTTPTest\Utils;
1416

1517
use chillerlan\HTTP\Utils\QueryUtil;
1618
use InvalidArgumentException;
1719
use PHPUnit\Framework\Attributes\DataProvider;
1820
use PHPUnit\Framework\TestCase;
19-
use const PHP_QUERY_RFC1738;
20-
use const PHP_QUERY_RFC3986;
21+
use const PHP_QUERY_RFC1738, PHP_QUERY_RFC3986;
2122

2223
/**
2324
*

tests/ServerUtilTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
* @noinspection PhpArrayWriteIsNotUsedInspection
1111
*/
1212

13+
declare(strict_types=1);
14+
1315
namespace chillerlan\HTTPTest\Utils;
1416

1517
use InvalidArgumentException;
1618
use PHPUnit\Framework\Attributes\DataProvider;
1719
use PHPUnit\Framework\TestCase;
18-
use function microtime;
19-
use function time;
20-
use const UPLOAD_ERR_OK;
21-
use const UPLOAD_ERR_PARTIAL;
20+
use function microtime, time;
21+
use const UPLOAD_ERR_OK, UPLOAD_ERR_PARTIAL;
2222

2323
class ServerUtilTest extends TestCase{
2424
use FactoryTrait;

tests/StreamUtilTest.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTPTest\Utils;
1214

1315
use chillerlan\HTTP\Utils\StreamUtil;
1416
use InvalidArgumentException;
1517
use PHPUnit\Framework\TestCase;
1618
use RuntimeException;
17-
use function fclose;
18-
use function fopen;
19-
use function stream_get_meta_data;
20-
use function strlen;
21-
use function substr;
19+
use function fclose, fopen, stream_get_meta_data, strlen, substr;
2220

2321
/**
2422
*

tests/UriUtilTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @license MIT
99
*/
1010

11+
declare(strict_types=1);
12+
1113
namespace chillerlan\HTTPTest\Utils;
1214

1315
use chillerlan\HTTP\Utils\UriUtil;

0 commit comments

Comments
 (0)