server_request_headers()   B
last analyzed

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
use function strncmp;
15
use function strtolower;
16
use function strtr;
17
use function substr;
18
use function ucwords;
19
20
/**
21
 * @return array<string, string>
22
 *
23
 * @link http://php.net/manual/en/reserved.variables.server.php
24
 * @link https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.18
25
 */
26
function server_request_headers(?array $serverParams = null): array
27
{
28 58
    $serverParams ??= $_SERVER;
29
30
    // https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.2
31 58
    if (!isset($serverParams['HTTP_CONTENT_LENGTH']) && isset($serverParams['CONTENT_LENGTH'])) {
32 2
        $serverParams['HTTP_CONTENT_LENGTH'] = $serverParams['CONTENT_LENGTH'];
33
    }
34
35
    // https://datatracker.ietf.org/doc/html/rfc3875#section-4.1.3
36 58
    if (!isset($serverParams['HTTP_CONTENT_TYPE']) && isset($serverParams['CONTENT_TYPE'])) {
37 2
        $serverParams['HTTP_CONTENT_TYPE'] = $serverParams['CONTENT_TYPE'];
38
    }
39
40 58
    $result = [];
41 58
    foreach ($serverParams as $key => $value) {
42 50
        if (strncmp('HTTP_', $key, 5) !== 0) {
43 44
            continue;
44
        }
45
46 10
        $name = strtr(substr($key, 5), '_', '-');
47 10
        $name = ucwords(strtolower($name), '-');
48
49 10
        $result[$name] = $value;
50
    }
51
52 58
    return $result;
53
}
54