Completed
Push — master ( 635821...8e6189 )
by Freek
05:20 queued 13s
created

Url::isRelative()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 an inline javascript.
92
     *
93
     * @return bool
94
     */
95
    public function isJavascript()
96
    {
97
        return $this->scheme === 'javascript';
98
    }
99
100
    /**
101
     * Set the scheme.
102
     *
103
     * @param string $scheme
104
     *
105
     * @return $this
106
     */
107
    public function setScheme($scheme)
108
    {
109
        $this->scheme = $scheme;
110
111
        return $this;
112
    }
113
114
    /**
115
     * Set the host.
116
     *
117
     * @param string $host
118
     *
119
     * @return $this
120
     */
121
    public function setHost($host)
122
    {
123
        $this->host = $host;
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param int $port
130
     *
131
     * @return $this
132
     *
133
     * @throws \Spatie\Crawler\Exceptions\InvalidPortNumber
134
     */
135
    public function setPort($port)
136
    {
137
        if (!is_numeric($port)) {
138
            throw new InvalidPortNumber();
139
        }
140
141
        $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...
142
143
        return $this;
144
    }
145
146
    /**
147
     * Remove the fragment.
148
     *
149
     * @return $this
150
     */
151
    public function removeFragment()
152
    {
153
        $this->path = explode('#', $this->path)[0];
154
155
        return $this;
156
    }
157
158
    /**
159
     * @return null|string
160
     */
161
    public function path()
162
    {
163
        return $this->path;
164
    }
165
166
    /**
167
     * @param int|null $index
168
     *
169
     * @return array|null|string
170
     */
171
    public function segments($index = null)
172
    {
173
        $segments = collect(explode('/', $this->path()))
174
            ->filter(function ($value) {
175
                return $value !== '';
176
            })
177
            ->values()
178
            ->toArray();
179
180
        if (! is_null($index)) {
181
            return $this->segment($index);
182
        }
183
184
        return $segments;
185
    }
186
187
    /**
188
     * @param int $index
189
     *
190
     * @return string|null
191
     */
192
    public function segment($index)
193
    {
194
        if (! isset($this->segments()[$index - 1])) {
195
            return null;
196
        }
197
198
        return $this->segments()[$index - 1];
199
    }
200
201
    /**
202
     * Convert the url to string.
203
     *
204
     * @return string
205
     */
206
    public function __toString()
207
    {
208
        $path = starts_with($this->path, '/') ? substr($this->path, 1) : $this->path;
209
210
        $port = ($this->port === 80 ? '' : ":{$this->port}");
211
212
        $queryString = (is_null($this->query) ? '' : "?{$this->query}");
213
214
        return "{$this->scheme}://{$this->host}{$port}/{$path}{$queryString}";
215
    }
216
}
217