Completed
Pull Request — master (#7)
by Jolita
01:59
created

Url::isJavascript()   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
     * @param $url
31
     *
32
     * @return static
33
     */
34
    public static function create($url)
35
    {
36
        return new static($url);
37
    }
38
39
    /**
40
     * Url constructor.
41
     *
42
     * @param $url
43
     */
44
    public function __construct($url)
45
    {
46
        $urlProperties = parse_url($url);
47
48
        foreach (['scheme', 'host', 'path', 'port'] as $property) {
49
            if (isset($urlProperties[$property])) {
50
                $this->$property = $urlProperties[$property];
51
            }
52
        }
53
    }
54
55
    /**
56
     * Determine if the url is relative.
57
     *
58
     * @return bool
59
     */
60
    public function isRelative()
61
    {
62
        return is_null($this->host);
63
    }
64
65
    /**
66
     * Determine if the url is protocol independent.
67
     *
68
     * @return bool
69
     */
70
    public function isProtocolIndependent()
71
    {
72
        return is_null($this->scheme);
73
    }
74
75
    /**
76
     * Determine if this is a mailto-link.
77
     *
78
     * @return bool
79
     */
80
    public function isEmailUrl()
81
    {
82
        return $this->scheme === 'mailto';
83
    }
84
85
    /**
86
     * Determine if this is an inline javascript.
87
     *
88
     * @return bool
89
     */
90
    public function isJavascript()
91
    {
92
        return $this->scheme === 'javascript';
93
    }
94
95
96
    /**
97
     * Set the scheme.
98
     *
99
     * @param string $scheme
100
     *
101
     * @return $this
102
     */
103
    public function setScheme($scheme)
104
    {
105
        $this->scheme = $scheme;
106
107
        return $this;
108
    }
109
110
    /**
111
     * Set the host.
112
     *
113
     * @param string $host
114
     *
115
     * @return $this
116
     */
117
    public function setHost($host)
118
    {
119
        $this->host = $host;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @param int $port
126
     *
127
     * @return $this
128
     *
129
     * @throws \Spatie\Crawler\Exceptions\InvalidPortNumber
130
     */
131
    public function setPort($port)
132
    {
133
        if (!is_numeric($port)) {
134
            throw new InvalidPortNumber();
135
        }
136
137
        $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...
138
139
        return $this;
140
    }
141
142
    /**
143
     * Remove the fragment.
144
     *
145
     * @return $this
146
     */
147
    public function removeFragment()
148
    {
149
        $this->path = explode('#', $this->path)[0];
150
151
        return $this;
152
    }
153
154
    /**
155
     * Convert the url to string.
156
     *
157
     * @return string
158
     */
159
    public function __toString()
160
    {
161
        $path = starts_with($this->path, '/') ? substr($this->path, 1) : $this->path;
162
163
        $port = ($this->port === 80 ? '' : ":{$this->port}");
164
165
        return "{$this->scheme}://{$this->host}{$port}/{$path}";
166
    }
167
}
168