Passed
Push — master ( cd6ebd...24ee82 )
by Anatoly
07:49 queued 03:36
created

server_request_headers()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 13
c 0
b 0
f 0
nc 12
nop 1
dl 0
loc 27
ccs 13
cts 13
cp 1
crap 7
rs 8.8333
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message;
13
14
/**
15
 * Import functions
16
 */
17
use function strncmp;
18
use function strtolower;
19
use function strtr;
20
use function substr;
21
use function ucwords;
22
23
/**
24
 * Gets the request headers
25
 *
26
 * @param array|null $serverParams
27
 *
28
 * @return array<string, string>
29
 *
30
 * @link http://php.net/manual/en/reserved.variables.server.php
31
 * @link https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.18
32
 */
33
function server_request_headers(?array $serverParams = null): array
34
{
35 58
    $serverParams ??= $_SERVER;
36
37
    // https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.2
38 58
    if (!isset($serverParams['HTTP_CONTENT_LENGTH']) && isset($serverParams['CONTENT_LENGTH'])) {
39 2
        $serverParams['HTTP_CONTENT_LENGTH'] = $serverParams['CONTENT_LENGTH'];
40
    }
41
42
    // https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.3
43 58
    if (!isset($serverParams['HTTP_CONTENT_TYPE']) && isset($serverParams['CONTENT_TYPE'])) {
44 2
        $serverParams['HTTP_CONTENT_TYPE'] = $serverParams['CONTENT_TYPE'];
45
    }
46
47 58
    $result = [];
48 58
    foreach ($serverParams as $key => $value) {
49 50
        if (0 <> strncmp('HTTP_', $key, 5)) {
50 44
            continue;
51
        }
52
53 10
        $name = strtr(substr($key, 5), '_', '-');
54 10
        $name = ucwords(strtolower($name), '-');
55
56 10
        $result[$name] = $value;
57
    }
58
59 58
    return $result;
60
}
61