server_request_uri()   B
last analyzed

Complexity

Conditions 9
Paths 48

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 20
c 0
b 0
f 0
nc 48
nop 1
dl 0
loc 32
ccs 20
cts 20
cp 1
crap 9
rs 8.0555
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 Psr\Http\Message\UriInterface;
15
16
use function array_key_exists;
17
18
function server_request_uri(?array $serverParams = null): UriInterface
19
{
20 40
    $serverParams ??= $_SERVER;
21
22 40
    if (array_key_exists('HTTPS', $serverParams)) {
23 2
        if ('off' !== $serverParams['HTTPS']) {
24 1
            $scheme = 'https://';
25
        }
26
    }
27
28 40
    if (array_key_exists('HTTP_HOST', $serverParams)) {
29 3
        $host = $serverParams['HTTP_HOST'];
30 37
    } elseif (array_key_exists('SERVER_NAME', $serverParams)) {
31 2
        $host = $serverParams['SERVER_NAME'];
32 2
        if (array_key_exists('SERVER_PORT', $serverParams)) {
33 1
            $host .= ':' . $serverParams['SERVER_PORT'];
34
        }
35
    }
36
37 40
    if (array_key_exists('REQUEST_URI', $serverParams)) {
38 2
        $target = $serverParams['REQUEST_URI'];
39 38
    } elseif (array_key_exists('PHP_SELF', $serverParams)) {
40 10
        $target = $serverParams['PHP_SELF'];
41 10
        if (array_key_exists('QUERY_STRING', $serverParams)) {
42 1
            $target .= '?' . $serverParams['QUERY_STRING'];
43
        }
44
    }
45
46 40
    return new Uri(
47 40
        ($scheme ?? 'http://') .
48 40
        ($host ?? 'localhost') .
49 40
        ($target ?? '/')
50 40
    );
51
}
52