Skip to content

Commit 8de9778

Browse files
committed
🚿
1 parent b235b34 commit 8de9778

File tree

6 files changed

+63
-46
lines changed

6 files changed

+63
-46
lines changed

src/MessageUtil.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,39 @@ public static function decodeXML(MessageInterface $message, bool $assoc = null):
5454
/**
5555
* Returns the string representation of an HTTP message. (from Guzzle)
5656
*/
57-
public static function toString(MessageInterface $message, bool $appendBody = true):string{
58-
$msg = '';
57+
public static function toString(MessageInterface $message, bool $appendBody = null):string{
58+
$appendBody ??= true;
59+
$msg = '';
5960

6061
if($message instanceof RequestInterface){
61-
$msg = trim($message->getMethod().' '.$message->getRequestTarget()).' HTTP/'.$message->getProtocolVersion();
62+
$msg = sprintf(
63+
'%s %s HTTP/%s',
64+
$message->getMethod(),
65+
$message->getRequestTarget(),
66+
$message->getProtocolVersion()
67+
);
6268

6369
if(!$message->hasHeader('host')){
64-
$msg .= "\r\nHost: ".$message->getUri()->getHost();
70+
$msg .= sprintf("\r\nHost: %s", $message->getUri()->getHost());
6571
}
6672

6773
}
6874
elseif($message instanceof ResponseInterface){
69-
$msg = 'HTTP/'.$message->getProtocolVersion().' '.$message->getStatusCode().' '.$message->getReasonPhrase();
75+
$msg = sprintf(
76+
'HTTP/%s %s %s',
77+
$message->getProtocolVersion(),
78+
$message->getStatusCode(),
79+
$message->getReasonPhrase()
80+
);
7081
}
7182

7283
foreach($message->getHeaders() as $name => $values){
73-
$msg .= "\r\n".$name.': '.implode(', ', $values);
84+
$msg .= sprintf("\r\n%s: %s", $name, implode(', ', $values));
7485
}
7586

7687
// appending the body might cause issues in some cases, e.g. with large responses or file streams
77-
if($appendBody){
78-
$msg .= "\r\n\r\n".self::getContents($message);
88+
if($appendBody === true){
89+
$msg .= sprintf("\r\n\r\n%s", self::getContents($message));
7990
}
8091

8192
return $msg;

src/ServerUtil.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public function __construct(
2828
protected UriFactoryInterface $uriFactory,
2929
protected UploadedFileFactoryInterface $uploadedFileFactory,
3030
protected StreamFactoryInterface $streamFactory
31-
){}
31+
){
32+
// noop
33+
}
3234

3335
/**
3436
* Returns a ServerRequest populated with superglobals:

tests/HeaderUtilTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public static function headerDataProvider():array{
2727
'UPPERCASEKEY' => [['UPPERCASEKEY' => 'UPPERCASEVALUE'], ['Uppercasekey' => 'UPPERCASEVALUE']],
2828
'mIxEdCaSeKey' => [['mIxEdCaSeKey' => 'MiXeDcAsEvAlUe'], ['Mixedcasekey' => 'MiXeDcAsEvAlUe']],
2929
'31i71casekey' => [['31i71casekey' => '31i71casevalue'], ['31i71casekey' => '31i71casevalue']],
30-
'numericvalue' => [['numericvalue:1'], ['Numericvalue' => '1']],
31-
'numericvalue2' => [['numericvalue' => 2], ['Numericvalue' => '2']],
30+
'numericvalue' => [['numericvalue:1'], ['Numericvalue' => '1']],
31+
'numericvalue2' => [['numericvalue' => 2], ['Numericvalue' => '2']],
3232
'keyvaluearray' => [[['foo' => 'bar']], ['Foo' => 'bar']],
3333
'arrayvalue' => [['foo' => ['bar', 'baz']], ['Foo' => 'bar, baz']],
3434
'invalid: 2' => [[2 => 2], []],
@@ -57,7 +57,7 @@ public function testCombineHeaderFields():void{
5757
$this::assertSame([
5858
'Accept' => 'foo, bar',
5959
'X-Whatever' => 'nope',
60-
'X-Foo' => 'bar, baz, what, nope'
60+
'X-Foo' => 'bar, baz, what, nope',
6161
], HeaderUtil::normalize($headers));
6262

6363
$r = $this->responseFactory->createResponse();
@@ -66,10 +66,10 @@ public function testCombineHeaderFields():void{
6666
$r = $r->withAddedHeader($k, $v);
6767
}
6868

69-
$this::assertSame( [
69+
$this::assertSame([
7070
'Accept' => ['foo, bar'],
7171
'X-Whatever' => ['nope'],
72-
'X-Foo' => ['bar, baz, what, nope']
72+
'X-Foo' => ['bar, baz, what, nope'],
7373
], $r->getHeaders());
7474

7575
}
@@ -85,7 +85,7 @@ public function testCombinedCookieHeaders():void{
8585
$this::assertSame([
8686
'Set-Cookie' => [
8787
'foo' => 'foo=baz',
88-
'whatever' => 'whatever=nope; HttpOnly'
88+
'whatever' => 'whatever=nope; HttpOnly',
8989
]
9090
], HeaderUtil::normalize($headers));
9191
}

tests/MessageUtilTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public function testDecompressContentUnknownHeaderValueException():void{
125125

126126
public static function decompressExceptionFnProvider():array{
127127
return [
128-
'br' => ['brotli', 'brotli_compress', 'br'],
129-
'zstd' => ['zstd', 'zstd_compress', 'zstd'],
128+
'br' => ['brotli', 'brotli_compress', 'br'],
129+
'zstd' => ['zstd', 'zstd_compress', 'zstd'],
130130
];
131131
}
132132

tests/QueryUtilTest.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ public static function queryParamDataProvider():array{
3333
['whatever' => null, 'nope' => '', 'true' => true, 'false' => false, 'array' => ['value' => false]],
3434
],
3535
// bool cast to types
36-
'BOOLEANS_AS_BOOL' => [
36+
'BOOLEANS_AS_BOOL' => [
3737
QueryUtil::BOOLEANS_AS_BOOL,
3838
true,
3939
['true' => true, 'false' => false, 'array' => ['value' => false]],
4040
],
41-
'BOOLEANS_AS_INT' => [
41+
'BOOLEANS_AS_INT' => [
4242
QueryUtil::BOOLEANS_AS_INT,
4343
true,
4444
['true' => 1, 'false' => 0, 'array' => ['value' => 0]],
4545
],
46-
'BOOLEANS_AS_INT_STRING' => [
46+
'BOOLEANS_AS_INT_STRING' => [
4747
QueryUtil::BOOLEANS_AS_INT_STRING,
4848
true,
4949
['true' => '1', 'false' => '0', 'array' => ['value' => '0']],
5050
],
51-
'BOOLEANS_AS_STRING' => [
51+
'BOOLEANS_AS_STRING' => [
5252
QueryUtil::BOOLEANS_AS_STRING,
5353
true,
5454
['true' => 'true', 'false' => 'false', 'array' => ['value' => 'false']],
@@ -171,16 +171,20 @@ public function testParseDoesTrimQuestionMark():void{
171171
public static function parseUrlProvider():array{
172172
return [
173173
['http://', null],
174-
['https://яндекAс.рф', [
175-
'scheme' => 'https',
176-
'host' => 'яндекAс.рф'
177-
]],
178-
['http://[2a00:f48:1008::212:183:10]:56?foo=bar', [
179-
'scheme' => 'http',
180-
'host' => '[2a00:f48:1008::212:183:10]',
181-
'port' => '56',
182-
'query' => 'foo=bar',
183-
]]
174+
[
175+
'https://яндекAс.рф', [
176+
'scheme' => 'https',
177+
'host' => 'яндекAс.рф',
178+
],
179+
],
180+
[
181+
'http://[2a00:f48:1008::212:183:10]:56?foo=bar', [
182+
'scheme' => 'http',
183+
'host' => '[2a00:f48:1008::212:183:10]',
184+
'port' => '56',
185+
'query' => 'foo=bar',
186+
],
187+
],
184188
];
185189
}
186190

tests/ServerUtilTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,43 +46,43 @@ public static function dataGetUriFromGlobals():array{
4646
];
4747

4848
return [
49-
'HTTPS request' => [
49+
'HTTPS request' => [
5050
'https://www.example.org/blog/article.php?id=10&user=foo',
5151
$server,
5252
],
53-
'HTTPS request with different on value' => [
53+
'HTTPS request with different on value' => [
5454
'https://www.example.org/blog/article.php?id=10&user=foo',
5555
array_merge($server, ['HTTPS' => '1']),
5656
],
57-
'HTTP request' => [
57+
'HTTP request' => [
5858
'http://www.example.org/blog/article.php?id=10&user=foo',
5959
array_merge($server, ['HTTPS' => 'off', 'SERVER_PORT' => '80']),
6060
],
61-
'HTTP_HOST missing -> fallback to SERVER_NAME' => [
61+
'HTTP_HOST missing -> fallback to SERVER_NAME' => [
6262
'https://www.example.org/blog/article.php?id=10&user=foo',
6363
array_merge($server, ['HTTP_HOST' => null]),
6464
],
6565
'HTTP_HOST and SERVER_NAME missing -> fallback to SERVER_ADDR' => [
6666
'https://217.112.82.20/blog/article.php?id=10&user=foo',
6767
array_merge($server, ['HTTP_HOST' => null, 'SERVER_NAME' => null]),
6868
],
69-
'No query String' => [
69+
'No query String' => [
7070
'https://www.example.org/blog/article.php',
7171
array_merge($server, ['REQUEST_URI' => '/blog/article.php', 'QUERY_STRING' => '']),
7272
],
73-
'Host header with port' => [
73+
'Host header with port' => [
7474
'https://www.example.org:8324/blog/article.php?id=10&user=foo',
7575
array_merge($server, ['HTTP_HOST' => 'www.example.org:8324']),
7676
],
77-
'Different port with SERVER_PORT' => [
77+
'Different port with SERVER_PORT' => [
7878
'https://www.example.org:8324/blog/article.php?id=10&user=foo',
7979
array_merge($server, ['SERVER_PORT' => '8324']),
8080
],
81-
'REQUEST_URI missing query string' => [
81+
'REQUEST_URI missing query string' => [
8282
'https://www.example.org/blog/article.php?id=10&user=foo',
8383
array_merge($server, ['REQUEST_URI' => '/blog/article.php']),
8484
],
85-
'Empty server variable' => [
85+
'Empty server variable' => [
8686
'http://localhost',
8787
['REQUEST_TIME' => time(), 'SCRIPT_NAME' => '/blog/article.php'], // phpunit fix
8888
],
@@ -119,7 +119,7 @@ public function testCreateServerRequestFromGlobals():void{
119119
];
120120

121121
$_COOKIE = [
122-
'logged-in' => 'yes!'
122+
'logged-in' => 'yes!',
123123
];
124124

125125
$_POST = [
@@ -128,7 +128,7 @@ public function testCreateServerRequestFromGlobals():void{
128128
];
129129

130130
$_GET = [
131-
'id' => 10,
131+
'id' => 10,
132132
'user' => 'foo',
133133
];
134134

@@ -139,7 +139,7 @@ public function testCreateServerRequestFromGlobals():void{
139139
'tmp_name' => __DIR__.'/uploaded_file.tmp',
140140
'error' => UPLOAD_ERR_OK,
141141
'size' => 123,
142-
]
142+
],
143143
];
144144

145145
$server = $this->server->createServerRequestFromGlobals();
@@ -173,11 +173,11 @@ public function testNormalizeMultipleFiles():void{
173173
$files = [
174174
'files' => [
175175
'name' => ['MyFile1.txt', 'MyFile2.gif', 'MyFile3.txt'],
176-
'type' => ['text/plain','image/gif','text/plain',],
176+
'type' => ['text/plain','image/gif','text/plain'],
177177
'tmp_name' => [$tmp, $tmp, $tmp],
178178
'error' => [UPLOAD_ERR_OK, UPLOAD_ERR_PARTIAL, UPLOAD_ERR_OK],
179179
'size' => [123, 456, 789],
180-
]
180+
],
181181
];
182182

183183
/** @var array $normalized */
@@ -213,7 +213,7 @@ public function testNormalizeMultipleFiles():void{
213213
'error' => UPLOAD_ERR_OK,
214214
'size' => 789,
215215
],
216-
]
216+
],
217217
];
218218

219219
/** @var array $normalized */

0 commit comments

Comments
 (0)