Skip to content

Commit 210c9ff

Browse files
test: Add coverage attributes for Kirby\Http
Co-authored-by: Lukas Bestle <[email protected]>
1 parent 7467c88 commit 210c9ff

32 files changed

+258
-117
lines changed

src/Http/Cookie.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ public static function remove(string $key): bool
222222
protected static function trackUsage(string $key): void
223223
{
224224
// lazily request the instance for non-CMS use cases
225-
$kirby = App::instance(null, true);
226-
$kirby?->response()->usesCookie($key);
225+
App::instance(lazy: true)?->response()->usesCookie($key);
227226
}
228227
}

src/Http/Environment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
* secure host and base URL detection, as
1414
* well as loading the dedicated
1515
* environment options.
16-
* @since 3.7.0
1716
*
1817
* @package Kirby Http
1918
* @author Bastian Allgeier <[email protected]>
2019
* @link https://getkirby.com
2120
* @copyright Bastian Allgeier
2221
* @license https://opensource.org/licenses/MIT
22+
* @since 3.7.0
2323
*/
2424
class Environment
2525
{

src/Http/Header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function contentType(
6868

6969
$header = 'Content-type: ' . $mime;
7070

71-
if (empty($charset) === false) {
71+
if ($charset !== '') {
7272
$header .= '; charset=' . $charset;
7373
}
7474

src/Http/Params.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(array|string|null $params)
3838
*/
3939
public static function extract(string|array|null $path = null): array
4040
{
41-
if (empty($path) === true) {
41+
if ($path === null || $path === '' || $path === []) {
4242
return [
4343
'path' => null,
4444
'params' => null,
@@ -62,12 +62,16 @@ public static function extract(string|array|null $path = null): array
6262
continue;
6363
}
6464

65-
$paramParts = Str::split($p, $separator);
66-
$paramKey = $paramParts[0] ?? null;
67-
$paramValue = $paramParts[1] ?? null;
65+
$parts = Str::split($p, $separator);
6866

69-
if ($paramKey !== null) {
70-
$params[rawurldecode($paramKey)] = $paramValue !== null ? rawurldecode($paramValue) : null;
67+
if ($key = $parts[0] ?? null) {
68+
$key = rawurldecode($key);
69+
70+
if ($value = $parts[1] ?? null) {
71+
$value = rawurldecode($value);
72+
}
73+
74+
$params[$key] = $value;
7175
}
7276

7377
unset($path[$index]);
@@ -89,7 +93,7 @@ public static function extract(string|array|null $path = null): array
8993

9094
public function isEmpty(): bool
9195
{
92-
return empty((array)$this) === true;
96+
return (array)$this === [];
9397
}
9498

9599
public function isNotEmpty(): bool
@@ -106,15 +110,7 @@ public function isNotEmpty(): bool
106110
*/
107111
public static function separator(): string
108112
{
109-
if (static::$separator !== null) {
110-
return static::$separator;
111-
}
112-
113-
if (DIRECTORY_SEPARATOR === '/') {
114-
return static::$separator = ':';
115-
}
116-
117-
return static::$separator = ';';
113+
return static::$separator ??= DIRECTORY_SEPARATOR === '/' ? ':' : ';';
118114
}
119115

120116
/**
@@ -134,7 +130,9 @@ public function toString(
134130

135131
foreach ($this as $key => $value) {
136132
if ($value !== null && $value !== '') {
137-
$params[] = rawurlencode($key) . $separator . rawurlencode($value);
133+
$key = rawurlencode($key);
134+
$value = rawurlencode($value);
135+
$params[] = $key . $separator . $value;
138136
}
139137
}
140138

src/Http/Query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(string|array|null $query)
2929

3030
public function isEmpty(): bool
3131
{
32-
return empty((array)$this) === true;
32+
return (array)$this === [];
3333
}
3434

3535
public function isNotEmpty(): bool
@@ -41,7 +41,7 @@ public function toString(bool $questionMark = false): string
4141
{
4242
$query = http_build_query($this, '', '&', PHP_QUERY_RFC3986);
4343

44-
if (empty($query) === true) {
44+
if ($query === '') {
4545
return '';
4646
}
4747

src/Http/Remote.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ class Remote
4747
public string $errorMessage;
4848
public array $headers = [];
4949
public array $info = [];
50-
public array $options = [];
5150

5251
/**
5352
* @throws \Exception when the curl request failed
5453
*/
55-
public function __construct(string $url, array $options = [])
56-
{
54+
public function __construct(
55+
string $url,
56+
public array $options = []
57+
) {
5758
$defaults = static::$defaults;
5859

5960
// use the system CA store by default if
@@ -71,11 +72,8 @@ public function __construct(string $url, array $options = [])
7172
$defaults = [...$defaults, ...$app->option('remote', [])];
7273
}
7374

74-
// set all options
75-
$this->options = [...$defaults, ...$options];
76-
77-
// add the url
78-
$this->options['url'] = $url;
75+
// set all options, incl. url
76+
$this->options = [...$defaults, ...$options, 'url' => $url];
7977

8078
// send the request
8179
$this->fetch();
@@ -277,7 +275,7 @@ public static function get(string $url, array $params = []): static
277275

278276
$query = http_build_query($options['data']);
279277

280-
if (empty($query) === false) {
278+
if ($query !== '') {
281279
$url = match (Url::hasQuery($url)) {
282280
true => $url . '&' . $query,
283281
default => $url . '?' . $query
@@ -339,7 +337,7 @@ public function options(): array
339337
*/
340338
protected function postfields($data)
341339
{
342-
if (is_object($data) || is_array($data)) {
340+
if (is_object($data) === true || is_array($data) === true) {
343341
return http_build_query($data);
344342
}
345343

src/Http/Request.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ class Request
6767
*/
6868
protected string $method;
6969

70-
/**
71-
* All options that have been passed to
72-
* the request in the constructor
73-
*/
74-
protected array $options;
75-
7670
/**
7771
* The Query object is a wrapper around
7872
* the URL query string, which parses the
@@ -96,9 +90,9 @@ class Request
9690
* data via the $options array or use
9791
* the data from the incoming request.
9892
*/
99-
public function __construct(array $options = [])
100-
{
101-
$this->options = $options;
93+
public function __construct(
94+
protected array $options = []
95+
) {
10296
$this->method = $this->detectRequestMethod($options['method'] ?? null);
10397

10498
if (isset($options['body']) === true) {
@@ -155,7 +149,7 @@ public function auth(): Auth|false|null
155149
}
156150

157151
// lazily request the instance for non-CMS use cases
158-
$kirby = App::instance(null, true);
152+
$kirby = App::instance(lazy: true);
159153

160154
// tell the CMS responder that the response relies on
161155
// the `Authorization` header and its value (even if
@@ -224,13 +218,26 @@ public function data(): array
224218
public function detectRequestMethod(string|null $method = null): string
225219
{
226220
// all possible methods
227-
$methods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE', 'PATCH'];
221+
$methods = [
222+
'CONNECT',
223+
'DELETE',
224+
'GET',
225+
'HEAD',
226+
'OPTIONS',
227+
'PATCH',
228+
'POST',
229+
'PUT',
230+
'TRACE',
231+
];
228232

229233
// the request method can be overwritten with a header
230-
$methodOverride = strtoupper(Environment::getGlobally('HTTP_X_HTTP_METHOD_OVERRIDE', ''));
234+
if ($method === null) {
235+
$override = Environment::getGlobally('HTTP_X_HTTP_METHOD_OVERRIDE', '');
236+
$override = strtoupper($override);
231237

232-
if (in_array($methodOverride, $methods, true) === true) {
233-
$method ??= $methodOverride;
238+
if (in_array($override, $methods, true) === true) {
239+
$method = $override;
240+
}
234241
}
235242

236243
// final chain of options to detect the method
@@ -410,14 +417,15 @@ protected function authString(): string|null
410417
// both variants need to be checked separately
411418
// because empty strings are treated as invalid
412419
// but the `??` operator wouldn't do the fallback
413-
414420
$option = $this->options['auth'] ?? null;
415-
if (empty($option) === false) {
421+
422+
if (is_string($option) === true && $option !== '') {
416423
return $option;
417424
}
418425

419426
$header = $this->header('authorization');
420-
if (empty($header) === false) {
427+
428+
if (is_string($header) === true && $header !== '') {
421429
return $header;
422430
}
423431

src/Http/Request/Body.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ class Body implements Stringable
2020
{
2121
use Data;
2222

23-
/**
24-
* The raw body content
25-
*/
26-
protected string|array|null $contents;
27-
2823
/**
2924
* The parsed content as array
3025
*/
@@ -36,10 +31,12 @@ class Body implements Stringable
3631
* If null is being passed, the class will
3732
* fetch the body either from the $_POST global
3833
* or from php://input.
34+
*
35+
* @param array|string|null $contents The raw body content
3936
*/
40-
public function __construct(array|string|null $contents = null)
41-
{
42-
$this->contents = $contents;
37+
public function __construct(
38+
protected array|string|null $contents = null
39+
) {
4340
}
4441

4542
/**
@@ -52,7 +49,7 @@ public function contents(): string|array
5249
return $this->contents;
5350
}
5451

55-
if (empty($_POST) === false) {
52+
if ($_POST !== []) {
5653
return $this->contents = $_POST;
5754
}
5855

@@ -90,7 +87,7 @@ public function data(): array
9087
// try to parse the body as query string
9188
parse_str($contents, $parsed);
9289

93-
if (is_array($parsed)) {
90+
if (is_array($parsed) === true) {
9491
return $this->data = $parsed;
9592
}
9693
}

src/Http/Request/Data.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ public function get(string|array $key, $default = null)
4545
{
4646
if (is_array($key) === true) {
4747
$result = [];
48+
4849
foreach ($key as $k) {
4950
$result[$k] = $this->get($k);
5051
}
52+
5153
return $result;
5254
}
5355

src/Http/Request/Files.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(array|null $files = null)
3434
$files ??= $_FILES;
3535

3636
foreach ($files as $key => $file) {
37-
if (is_array($file['name'])) {
37+
if (is_array($file['name']) === true) {
3838
foreach ($file['name'] as $i => $name) {
3939
$this->files[$key][] = [
4040
'name' => $file['name'][$i] ?? null,

0 commit comments

Comments
 (0)