BaseUrlExtractor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 44
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseUrl() 0 12 1
A getProtocol() 0 8 4
A getHost() 0 4 1
A getRelativeBaseUrl() 0 14 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Selami\Stdlib;
6
7
use function basename;
8
use function strpos;
9
use function substr;
10
use function trim;
11
12
class BaseUrlExtractor
13
{
14
    public static function getBaseUrl(array $httpServerData): string
15
    {
16
        $protocol   = self::getProtocol($httpServerData);
17
        $host       = self::getHost($httpServerData);
18
        $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...
19
        $filename   = $httpServerData['SCRIPT_FILENAME'] ?? '';
20
        $scriptName = $httpServerData['SCRIPT_NAME'];
21
        $phpSelf    = $httpServerData['PHP_SELF'];
22
        $baseUrl    = self::getRelativeBaseUrl($scriptName, $phpSelf, $filename);
23
24
        return trim($protocol . '://' . $host . $baseUrl, '/');
25
    }
26
27
    public static function getProtocol(array $httpServerData): string
28
    {
29
        if (isset($httpServerData['HTTP_X_FORWARDED_PROTO'])) {
30
            return $httpServerData['HTTP_X_FORWARDED_PROTO'];
31
        }
32
33
        return isset($httpServerData['HTTPS']) && $httpServerData['HTTPS'] !== 'Off' ? 'https' : 'http';
34
    }
35
36
    public static function getHost($httpServerData): string
37
    {
38
        return $httpServerData['HTTP_HOST'];
39
    }
40
41
    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...
42
    {
43
        // Backtrack up the SCRIPT_FILENAME to find the portion
44
        // matching PHP_SELF.
45
        $baseUrl  = '/';
46
        $basename = basename($filename);
47
        if ($basename) {
48
            $path     = ($phpSelf ? trim($phpSelf, '/') : '');
49
            $basePos  = strpos($path, $basename) ?: 0;
50
            $baseUrl .= substr($path, 0, $basePos);
51
        }
52
53
        return $baseUrl;
54
    }
55
}
56