Passed
Push — master ( 8683f2...2f9d20 )
by Mehmet
01:43
created

BaseUrlExtractor::getRelativeBaseUrl()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 5
nop 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Selami\Stdlib;
5
6
class BaseUrlExtractor
7
{
8
    public static function getBaseUrl(array $httpServerData) : string
9
    {
10
        $protocol = self::getProtocol($httpServerData);
11
        $host = self::getHost($httpServerData);
12
        $uriPath = $httpServerData['REQUEST_URI'] ?? '';
0 ignored issues
show
Unused Code introduced by
$uriPath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
13
        $filename = $httpServerData['SCRIPT_FILENAME'] ?? '';
14
        $scriptName = $httpServerData['SCRIPT_NAME'];
15
        $phpSelf = $httpServerData['PHP_SELF'];
16
        $baseUrl = self::getRelativeBaseUrl($scriptName, $phpSelf, $filename);
17
        return trim($protocol . '://' . $host . $baseUrl, '/');
18
    }
19
20
21
22
    public static function getProtocol(array $httpServerData) : string
23
    {
24
        return isset($httpServerData['HTTPS']) && $httpServerData['HTTPS'] !== 'Off' ? 'https': 'http';
25
    }
26
27
    public static function getHost($httpServerData) : string
28
    {
29
        return $httpServerData['HTTP_HOST'];
30
    }
31
32
    public static function getRelativeBaseUrl($scriptName, $phpSelf, $filename) : string
0 ignored issues
show
Unused Code introduced by
The parameter $scriptName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
35
        // Backtrack up the SCRIPT_FILENAME to find the portion
36
        // matching PHP_SELF.
37
        $baseUrl  = '/';
38
        $basename = basename($filename);
39
        if ($basename) {
40
            $path     = ($phpSelf ? trim($phpSelf, '/') : '');
41
            $basePos  = strpos($path, $basename) ?: 0;
42
            $baseUrl .= substr($path, 0, $basePos) ;
43
        }
44
        return $baseUrl;
45
    }
46
}
47