Passed
Push — master ( a3db06...6080f3 )
by Sebastian
03:13
created

ParsedInfoFilter::filterParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppUtils\URLInfo\Parser;
6
7
use AppUtils\ConvertHelper;
8
use AppUtils\URLInfo;
9
use AppUtils\URLInfo\URISchemes;
10
use AppUtils\URLInfo\URLHosts;
11
use AppUtils\URLInfo\URLInfoTrait;
12
13
class ParsedInfoFilter
14
{
15
    use URLInfoTrait;
16
17
    private string $url;
18
19
    public function __construct(string $url, array $info)
20
    {
21
        $this->info = $info;
22
        $this->url = $url;
23
    }
24
25
    public function filter() : array
26
    {
27
        $this->info['type'] = URLInfo::TYPE_NONE;
28
29
        $this->filterScheme();
30
        $this->filterAuth();
31
        $this->filterHost();
32
        $this->filterPath();
33
        $this->filterKnownHosts();
34
        $this->filterParams();
35
36
        return $this->info;
37
    }
38
39
    private function filterScheme() : void
40
    {
41
        if($this->hasScheme())
42
        {
43
            $this->setScheme(strtolower($this->getScheme()));
0 ignored issues
show
Bug introduced by
It seems like $this->getScheme() can also be of type null; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
            $this->setScheme(strtolower(/** @scrutinizer ignore-type */ $this->getScheme()));
Loading history...
44
        }
45
        else
46
        {
47
            $scheme = URISchemes::detectScheme($this->url);
48
            if(!empty($scheme)) {
49
                $this->setScheme(URISchemes::resolveSchemeName($scheme));
50
            }
51
        }
52
    }
53
54
    private function filterAuth() : void
55
    {
56
        if(!$this->hasAuth()) {
57
            return;
58
        }
59
60
        $this->setAuth(
61
            urldecode((string)$this->getUser()),
62
            urldecode((string)$this->getPassword())
63
        );
64
    }
65
66
    private function filterHost() : void
67
    {
68
        if(!$this->hasHost())
69
        {
70
            return;
71
        }
72
73
        $host = $this->getHost();
74
        $host = strtolower($host);
0 ignored issues
show
Bug introduced by
It seems like $host can also be of type null; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $host = strtolower(/** @scrutinizer ignore-type */ $host);
Loading history...
75
        $host = str_replace(' ', '', $host);
76
        $this->setHost($host);
77
    }
78
79
    private function filterPath() : void
80
    {
81
        if($this->hasPath()) {
82
            $this->setPath(str_replace(' ', '', $this->getPath()));
83
        }
84
    }
85
86
    private function filterKnownHosts() : void
87
    {
88
        $host = $this->getPath();
89
90
        if(empty($host) || !URLHosts::isHostKnown($host))
91
        {
92
            return;
93
        }
94
95
        $this->setHost($host);
96
        $this->removePath();
97
98
        if(!$this->hasScheme()) {
99
            $this->setSchemeHTTPS();
100
        }
101
    }
102
103
    private function filterParams() : void
104
    {
105
        $this->info['params'] = array();
106
107
        $query = $this->getQuery();
108
        if(empty($query)) {
109
            return;
110
        }
111
112
        $this->info['params'] = ConvertHelper::parseQueryString($query);
113
114
        ksort($this->info['params']);
115
    }
116
}
117