Completed
Push — master ( e33465...b295dd )
by Sebastian
10s
created

Url   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 23
lcom 3
cbo 2
dl 0
loc 220
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A __construct() 0 10 3
A isRelative() 0 4 1
A isProtocolIndependent() 0 4 1
A isEmailUrl() 0 4 1
A isTelUrl() 0 4 1
A isJavascript() 0 4 1
A setScheme() 0 6 1
A setHost() 0 6 1
A setPort() 0 10 2
A removeFragment() 0 6 1
A path() 0 4 1
A segments() 0 15 2
A segment() 0 8 2
A __toString() 0 10 4
1
<?php
2
3
namespace Spatie\Crawler;
4
5
use Spatie\Crawler\Exceptions\InvalidPortNumber;
6
7
class Url
8
{
9
    /**
10
     * @var null|string
11
     */
12
    public $scheme;
13
14
    /**
15
     * @var null|string
16
     */
17
    public $host;
18
19
    /**
20
     * @var int
21
     */
22
    public $port = 80;
23
24
    /**
25
     * @var null|string
26
     */
27
    public $path;
28
29
    /**
30
     * @var null|string
31
     */
32
    public $query;
33
34
    /**
35
     * @param $url
36
     *
37
     * @return static
38
     */
39
    public static function create($url)
40
    {
41
        return new static($url);
42
    }
43
44
    /**
45
     * Url constructor.
46
     *
47
     * @param $url
48
     */
49
    public function __construct($url)
50
    {
51
        $urlProperties = parse_url($url);
52
53
        foreach (['scheme', 'host', 'path', 'port', 'query'] as $property) {
54
            if (isset($urlProperties[$property])) {
55
                $this->$property = $urlProperties[$property];
56
            }
57
        }
58
    }
59
60
    /**
61
     * Determine if the url is relative.
62
     *
63
     * @return bool
64
     */
65
    public function isRelative()
66
    {
67
        return is_null($this->host);
68
    }
69
70
    /**
71
     * Determine if the url is protocol independent.
72
     *
73
     * @return bool
74
     */
75
    public function isProtocolIndependent()
76
    {
77
        return is_null($this->scheme);
78
    }
79
80
    /**
81
     * Determine if this is a mailto-link.
82
     *
83
     * @return bool
84
     */
85
    public function isEmailUrl()
86
    {
87
        return $this->scheme === 'mailto';
88
    }
89
90
    /**
91
     * Determine if this is a tel-link.
92
     *
93
     * @return bool
94
     */
95
    public function isTelUrl()
96
    {
97
        return $this->scheme === 'tel';
98
    }
99
100
    /**
101
     * Determine if this is an inline javascript.
102
     *
103
     * @return bool
104
     */
105
    public function isJavascript()
106
    {
107
        return $this->scheme === 'javascript';
108
    }
109
110
    /**
111
     * Set the scheme.
112
     *
113
     * @param string $scheme
114
     *
115
     * @return $this
116
     */
117
    public function setScheme($scheme)
118
    {
119
        $this->scheme = $scheme;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Set the host.
126
     *
127
     * @param string $host
128
     *
129
     * @return $this
130
     */
131
    public function setHost($host)
132
    {
133
        $this->host = $host;
134
135
        return $this;
136
    }
137
138
    /**
139
     * @param int $port
140
     *
141
     * @throws \Spatie\Crawler\Exceptions\InvalidPortNumber
142
     *
143
     * @return $this
144
     */
145
    public function setPort($port)
146
    {
147
        if (! is_numeric($port)) {
148
            throw new InvalidPortNumber();
149
        }
150
151
        $this->port = $port;
0 ignored issues
show
Documentation Bug introduced by
It seems like $port can also be of type double or string. However, the property $port is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
152
153
        return $this;
154
    }
155
156
    /**
157
     * Remove the fragment.
158
     *
159
     * @return $this
160
     */
161
    public function removeFragment()
162
    {
163
        $this->path = explode('#', $this->path)[0];
164
165
        return $this;
166
    }
167
168
    /**
169
     * @return null|string
170
     */
171
    public function path()
172
    {
173
        return $this->path;
174
    }
175
176
    /**
177
     * @param int|null $index
178
     *
179
     * @return array|null|string
180
     */
181
    public function segments($index = null)
182
    {
183
        $segments = collect(explode('/', $this->path()))
184
            ->filter(function ($value) {
185
                return $value !== '';
186
            })
187
            ->values()
188
            ->toArray();
189
190
        if (! is_null($index)) {
191
            return $this->segment($index);
192
        }
193
194
        return $segments;
195
    }
196
197
    /**
198
     * @param int $index
199
     *
200
     * @return string|null
201
     */
202
    public function segment($index)
203
    {
204
        if (! isset($this->segments()[$index - 1])) {
205
            return;
206
        }
207
208
        return $this->segments()[$index - 1];
209
    }
210
211
    /**
212
     * Convert the url to string.
213
     *
214
     * @return string
215
     */
216
    public function __toString()
217
    {
218
        $path = starts_with($this->path, '/') ? substr($this->path, 1) : $this->path;
219
220
        $port = ($this->port === 80 ? '' : ":{$this->port}");
221
222
        $queryString = (is_null($this->query) ? '' : "?{$this->query}");
223
224
        return "{$this->scheme}://{$this->host}{$port}/{$path}{$queryString}";
225
    }
226
}
227