Url::createUrl()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.5806
cc 4
eloc 18
nc 6
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Domain\Web;
13
14
use Cubiche\Domain\System\StringLiteral;
15
16
/**
17
 * Url class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class Url extends StringLiteral
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $scheme;
27
28
    /**
29
     * @var string
30
     */
31
    protected $user;
32
33
    /**
34
     * @var string
35
     */
36
    protected $password;
37
38
    /**
39
     * @var Host
40
     */
41
    protected $host;
42
43
    /**
44
     * @var string
45
     */
46
    protected $path;
47
48
    /**
49
     * @var Port
50
     */
51
    protected $port;
52
53
    /**
54
     * @var string
55
     */
56
    protected $queryString;
57
58
    /**
59
     * @var string
60
     */
61
    protected $fragmentId;
62
63
    /**
64
     * @param string $url
65
     *
66
     * @throws \InvalidArgumentException
67
     */
68
    public function __construct($url)
69
    {
70
        parent::__construct($url);
71
72
        $user = \parse_url($url, PHP_URL_USER);
73
        $this->user = $user ? new StringLiteral($user) : new StringLiteral('');
0 ignored issues
show
Documentation Bug introduced by
It seems like $user ? new \Cubiche\Dom...ystem\StringLiteral('') of type object<Cubiche\Domain\System\StringLiteral> is incompatible with the declared type string of property $user.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
        $pass = \parse_url($url, PHP_URL_PASS);
75
        $this->password = $pass ? new StringLiteral($pass) : new StringLiteral('');
0 ignored issues
show
Documentation Bug introduced by
It seems like $pass ? new \Cubiche\Dom...ystem\StringLiteral('') of type object<Cubiche\Domain\System\StringLiteral> is incompatible with the declared type string of property $password.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
76
        $this->scheme = $this->parseScheme($url);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseScheme($url) of type object<Cubiche\Domain\System\StringLiteral> is incompatible with the declared type string of property $scheme.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
        $this->host = $this->parseHost($url);
78
        $this->path = $this->parsePath($url);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parsePath($url) of type object<Cubiche\Domain\Web\Path> is incompatible with the declared type string of property $path.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
79
        $this->port = $this->parsePort($url);
80
        $this->queryString = $this->parseQueryString($url);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseQueryString($url) of type object<Cubiche\Domain\System\StringLiteral> is incompatible with the declared type string of property $queryString.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
        $this->fragmentId = $this->parseFragmentIdentifier($url);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parseFragmentIdentifier($url) of type object<Cubiche\Domain\System\StringLiteral> is incompatible with the declared type string of property $fragmentId.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
82
83
        $this->createUrl();
84
    }
85
86
    protected function createUrl()
87
    {
88
        $userPass = '';
89
        if ($this->user()->isEmpty() === false) {
0 ignored issues
show
Bug introduced by
The method isEmpty cannot be called on $this->user() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
90
            $userPass = \sprintf('%s@', $this->user());
91
            if ($this->password()->isEmpty() === false) {
0 ignored issues
show
Bug introduced by
The method isEmpty cannot be called on $this->password() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
92
                $userPass = \sprintf('%s:%s@', $this->user(), $this->password());
93
            }
94
        }
95
        $port = '';
96
        if ($this->port() !== null) {
97
            $port = \sprintf(':%d', $this->port()->toNative());
98
        }
99
100
        $this->value = \sprintf(
101
            '%s://%s%s%s%s%s%s',
102
            $this->scheme(),
103
            $userPass,
104
            $this->host(),
105
            $port,
106
            $this->path(),
107
            $this->queryString(),
108
            $this->fragmentId()
109
        );
110
    }
111
112
    /**
113
     * @param string $url
114
     *
115
     * @throws \InvalidArgumentException
116
     *
117
     * @return string
118
     */
119
    protected function parseScheme($url)
120
    {
121
        $scheme = \parse_url($url, PHP_URL_SCHEME);
122
        if (\preg_match('/^[a-z]([a-z0-9\+\.-]+)?$/i', $scheme) === 0) {
123
            throw new \InvalidArgumentException(sprintf(
124
                'Argument "%s" is invalid. Allowed types for argument are "schema".',
125
                $url
126
            ));
127
        }
128
129
        return new StringLiteral($scheme);
0 ignored issues
show
Security Bug introduced by
It seems like $scheme defined by \parse_url($url, PHP_URL_SCHEME) on line 121 can also be of type false; however, Cubiche\Domain\System\StringLiteral::__construct() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
130
    }
131
132
    /**
133
     * @param string $url
134
     *
135
     * @throws \InvalidArgumentException
136
     *
137
     * @return Host
138
     */
139
    protected function parseHost($url)
140
    {
141
        $host = \parse_url($url, PHP_URL_HOST);
142
143
        return Host::fromNative($host);
144
    }
145
146
    /**
147
     * @param string $url
148
     *
149
     * @throws \InvalidArgumentException
150
     *
151
     * @return Path | null
152
     */
153
    protected function parsePath($url)
154
    {
155
        $path = \parse_url($url, PHP_URL_PATH);
156
        $filteredValue = parse_url($path, PHP_URL_PATH);
157 View Code Duplication
        if ($filteredValue === null || strlen($filteredValue) != strlen($path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
            throw new \InvalidArgumentException(sprintf(
159
                'Argument "%s" is invalid. Allowed types for argument are "url".',
160
                $url
161
            ));
162
        }
163
164
        return new Path($filteredValue);
0 ignored issues
show
Security Bug introduced by
It seems like $filteredValue defined by parse_url($path, PHP_URL_PATH) on line 156 can also be of type false; however, Cubiche\Domain\Web\Path::__construct() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
165
    }
166
167
    /**
168
     * @param string $url
169
     *
170
     * @return Port | NULL
171
     */
172
    protected function parsePort($url)
173
    {
174
        $port = \parse_url($url, PHP_URL_PORT);
175
        if ($port) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $port of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
176
            return new Port($port);
177
        }
178
179
        return;
180
    }
181
182
    /**
183
     * @param string $url
184
     *
185
     * @throws \InvalidArgumentException
186
     *
187
     * @return string
188
     */
189
    protected function parseQueryString($url)
190
    {
191
        $queryString = \parse_url($url, PHP_URL_QUERY);
192
        if ($queryString) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $queryString of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
193
            $queryString = \sprintf('?%s', $queryString);
194
195
            return new StringLiteral($queryString);
196
        }
197
198
        return new StringLiteral('');
199
    }
200
201
    /**
202
     * @param string $url
203
     *
204
     * @throws \InvalidArgumentException
205
     *
206
     * @return string
207
     */
208
    protected function parseFragmentIdentifier($url)
209
    {
210
        $fragmentId = \parse_url($url, PHP_URL_FRAGMENT);
211
        if ($fragmentId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fragmentId of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
212
            $fragment = \sprintf('#%s', $fragmentId);
213
            if (\preg_match('/^#[?%!$&\'()*+,;=a-zA-Z0-9-._~:@\/]*$/', $fragment) === 0) {
214
                throw new \InvalidArgumentException(sprintf(
215
                    'Argument "%s" is invalid. Allowed types for argument are "fragment identifier".',
216
                    $fragment
217
                ));
218
            }
219
220
            return new StringLiteral($fragment);
221
        }
222
223
        return new StringLiteral('');
224
    }
225
226
    /**
227
     * @return Host
228
     */
229
    public function host()
230
    {
231
        return $this->host;
232
    }
233
234
    /**
235
     * @return string
236
     */
237
    public function fragmentId()
238
    {
239
        return $this->fragmentId;
240
    }
241
242
    /**
243
     * @return string
244
     */
245
    public function password()
246
    {
247
        return $this->password;
248
    }
249
250
    /**
251
     * @return string
252
     */
253
    public function path()
254
    {
255
        return $this->path;
256
    }
257
258
    /**
259
     * @return int
260
     */
261
    public function port()
262
    {
263
        return $this->port;
264
    }
265
266
    /**
267
     * @return string
268
     */
269
    public function queryString()
270
    {
271
        return $this->queryString;
272
    }
273
274
    /**
275
     * @return string
276
     */
277
    public function scheme()
278
    {
279
        return $this->scheme;
280
    }
281
282
    /**
283
     * @return string
284
     */
285
    public function user()
286
    {
287
        return $this->user;
288
    }
289
}
290