Passed
Pull Request — master (#27)
by Anatoly
39:10
created

server_request_uri()   B

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
/**
15
 * Import classes
16
 */
17
use Psr\Http\Message\UriInterface;
18
19
/**
20
 * Import functions
21
 */
22
use function array_key_exists;
23
24
/**
25
 * Gets the request URI
26
 *
27
 * @param array|null $serverParams
28
 *
29
 * @return UriInterface
30
 *
31
 * @link http://php.net/manual/en/reserved.variables.server.php
32
 */
33
function server_request_uri(?array $serverParams = null): UriInterface
34
{
35 40
    $serverParams ??= $_SERVER;
36
37 40
    if (array_key_exists('HTTPS', $serverParams)) {
38 2
        if (! ('off' === $serverParams['HTTPS'])) {
39 1
            $scheme = 'https://';
40
        }
41
    }
42
43 40
    if (array_key_exists('HTTP_HOST', $serverParams)) {
44 3
        $host = $serverParams['HTTP_HOST'];
45 37
    } elseif (array_key_exists('SERVER_NAME', $serverParams)) {
46 2
        $host = $serverParams['SERVER_NAME'];
47 2
        if (array_key_exists('SERVER_PORT', $serverParams)) {
48 1
            $host .= ':' . $serverParams['SERVER_PORT'];
49
        }
50
    }
51
52 40
    if (array_key_exists('REQUEST_URI', $serverParams)) {
53 2
        $target = $serverParams['REQUEST_URI'];
54 38
    } elseif (array_key_exists('PHP_SELF', $serverParams)) {
55 10
        $target = $serverParams['PHP_SELF'];
56 10
        if (array_key_exists('QUERY_STRING', $serverParams)) {
57 1
            $target .= '?' . $serverParams['QUERY_STRING'];
58
        }
59
    }
60
61 40
    return new Uri(
62 40
        ($scheme ?? 'http://') .
63 40
        ($host ?? 'localhost') .
64 40
        ($target ?? '/')
65 40
    );
66
}
67