Completed
Push — master ( f72deb...78ed9f )
by Derek
04:03
created

Uri::getPort()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
namespace Subreality\Dilmun\Anshar\Http;
3
4
use Psr\Http\Message\UriInterface;
5
6
class Uri implements UriInterface
7
{
8
    /** @var  string */
9
    protected $uri_string;
10
11
    /** @var string */
12
    protected $scheme = "";
13
14
    /** @var  string */
15
    protected $hier_part;
16
17
    /** @var string */
18
    protected $authority;
19
20
    /** @var string */
21
    protected $user_info;
22
23
    /** @var string */
24
    protected $host;
25
26
    /** @var integer */
27
    protected $port;
28
29
    /** @var string */
30
    protected $path;
31
32
    /** @var string */
33
    protected $query;
34
35
    /** @var string */
36
    protected $fragment;
37
38
    /**
39
     * Uri constructor.
40
     *
41
     * @param string $uri
42
     */
43
    public function __construct($uri)
44
    {
45
        if (!is_string($uri)) {
46
            throw new \InvalidArgumentException("New Uri objects must be constructed with a string URI");
47
        }
48
49
        $uri_parts = $this->explodeUri($uri);
50
51
        $this->scheme = $uri_parts["scheme"];
52
    }
53
54
    /**
55
     * Retrieve the scheme component of the URI.
56
     *
57
     * If no scheme is present, this method MUST return an empty string.
58
     *
59
     * The value returned MUST be normalized to lowercase, per RFC 3986
60
     * Section 3.1.
61
     *
62
     * The trailing ":" character is not part of the scheme and MUST NOT be
63
     * added.
64
     *
65
     * @see https://tools.ietf.org/html/rfc3986#section-3.1
66
     * @return string The URI scheme.
67
     */
68
    public function getScheme()
69
    {
70
        return $this->scheme;
71
    }
72
73
    /**
74
     * Retrieve the authority component of the URI.
75
     *
76
     * If no authority information is present, this method MUST return an empty
77
     * string.
78
     *
79
     * The authority syntax of the URI is:
80
     *
81
     * <pre>
82
     * [user-info@]host[:port]
83
     * </pre>
84
     *
85
     * If the port component is not set or is the standard port for the current
86
     * scheme, it SHOULD NOT be included.
87
     *
88
     * @see https://tools.ietf.org/html/rfc3986#section-3.2
89
     * @return string The URI authority, in "[user-info@]host[:port]" format.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
90
     */
91
    public function getAuthority()
92
    {
93
        // TODO: Implement getAuthority() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
94
    }
95
96
    /**
97
     * Retrieve the user information component of the URI.
98
     *
99
     * If no user information is present, this method MUST return an empty
100
     * string.
101
     *
102
     * If a user is present in the URI, this will return that value;
103
     * additionally, if the password is also present, it will be appended to the
104
     * user value, with a colon (":") separating the values.
105
     *
106
     * The trailing "@" character is not part of the user information and MUST
107
     * NOT be added.
108
     *
109
     * @return string The URI user information, in "username[:password]" format.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
110
     */
111
    public function getUserInfo()
112
    {
113
        // TODO: Implement getUserInfo() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
114
    }
115
116
    /**
117
     * Retrieve the host component of the URI.
118
     *
119
     * If no host is present, this method MUST return an empty string.
120
     *
121
     * The value returned MUST be normalized to lowercase, per RFC 3986
122
     * Section 3.2.2.
123
     *
124
     * @see http://tools.ietf.org/html/rfc3986#section-3.2.2
125
     * @return string The URI host.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
126
     */
127
    public function getHost()
128
    {
129
        // TODO: Implement getHost() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
130
    }
131
132
    /**
133
     * Retrieve the port component of the URI.
134
     *
135
     * If a port is present, and it is non-standard for the current scheme,
136
     * this method MUST return it as an integer. If the port is the standard port
137
     * used with the current scheme, this method SHOULD return null.
138
     *
139
     * If no port is present, and no scheme is present, this method MUST return
140
     * a null value.
141
     *
142
     * If no port is present, but a scheme is present, this method MAY return
143
     * the standard port for that scheme, but SHOULD return null.
144
     *
145
     * @return null|int The URI port.
146
     */
147
    public function getPort()
148
    {
149
        // TODO: Implement getPort() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
150
    }
151
152
    /**
153
     * Retrieve the path component of the URI.
154
     *
155
     * The path can either be empty or absolute (starting with a slash) or
156
     * rootless (not starting with a slash). Implementations MUST support all
157
     * three syntaxes.
158
     *
159
     * Normally, the empty path "" and absolute path "/" are considered equal as
160
     * defined in RFC 7230 Section 2.7.3. But this method MUST NOT automatically
161
     * do this normalization because in contexts with a trimmed base path, e.g.
162
     * the front controller, this difference becomes significant. It's the task
163
     * of the user to handle both "" and "/".
164
     *
165
     * The value returned MUST be percent-encoded, but MUST NOT double-encode
166
     * any characters. To determine what characters to encode, please refer to
167
     * RFC 3986, Sections 2 and 3.3.
168
     *
169
     * As an example, if the value should include a slash ("/") not intended as
170
     * delimiter between path segments, that value MUST be passed in encoded
171
     * form (e.g., "%2F") to the instance.
172
     *
173
     * @see https://tools.ietf.org/html/rfc3986#section-2
174
     * @see https://tools.ietf.org/html/rfc3986#section-3.3
175
     * @return string The URI path.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
176
     */
177
    public function getPath()
178
    {
179
        // TODO: Implement getPath() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
180
    }
181
182
    /**
183
     * Retrieve the query string of the URI.
184
     *
185
     * If no query string is present, this method MUST return an empty string.
186
     *
187
     * The leading "?" character is not part of the query and MUST NOT be
188
     * added.
189
     *
190
     * The value returned MUST be percent-encoded, but MUST NOT double-encode
191
     * any characters. To determine what characters to encode, please refer to
192
     * RFC 3986, Sections 2 and 3.4.
193
     *
194
     * As an example, if a value in a key/value pair of the query string should
195
     * include an ampersand ("&") not intended as a delimiter between values,
196
     * that value MUST be passed in encoded form (e.g., "%26") to the instance.
197
     *
198
     * @see https://tools.ietf.org/html/rfc3986#section-2
199
     * @see https://tools.ietf.org/html/rfc3986#section-3.4
200
     * @return string The URI query string.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
201
     */
202
    public function getQuery()
203
    {
204
        // TODO: Implement getQuery() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
205
    }
206
207
    /**
208
     * Retrieve the fragment component of the URI.
209
     *
210
     * If no fragment is present, this method MUST return an empty string.
211
     *
212
     * The leading "#" character is not part of the fragment and MUST NOT be
213
     * added.
214
     *
215
     * The value returned MUST be percent-encoded, but MUST NOT double-encode
216
     * any characters. To determine what characters to encode, please refer to
217
     * RFC 3986, Sections 2 and 3.5.
218
     *
219
     * @see https://tools.ietf.org/html/rfc3986#section-2
220
     * @see https://tools.ietf.org/html/rfc3986#section-3.5
221
     * @return string The URI fragment.
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
222
     */
223
    public function getFragment()
224
    {
225
        // TODO: Implement getFragment() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
226
    }
227
228
    /**
229
     * Return an instance with the specified scheme.
230
     *
231
     * This method MUST retain the state of the current instance, and return
232
     * an instance that contains the specified scheme.
233
     *
234
     * Implementations MUST support the schemes "http" and "https" case
235
     * insensitively, and MAY accommodate other schemes if required.
236
     *
237
     * An empty scheme is equivalent to removing the scheme.
238
     *
239
     * @param string $scheme The scheme to use with the new instance.
240
     * @return static A new instance with the specified scheme.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
241
     * @throws \InvalidArgumentException for invalid or unsupported schemes.
242
     */
243
    public function withScheme($scheme)
244
    {
245
        // TODO: Implement withScheme() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
246
    }
247
248
    /**
249
     * Return an instance with the specified user information.
250
     *
251
     * This method MUST retain the state of the current instance, and return
252
     * an instance that contains the specified user information.
253
     *
254
     * Password is optional, but the user information MUST include the
255
     * user; an empty string for the user is equivalent to removing user
256
     * information.
257
     *
258
     * @param string $user The user name to use for authority.
259
     * @param null|string $password The password associated with $user.
260
     * @return static A new instance with the specified user information.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
261
     */
262
    public function withUserInfo($user, $password = null)
263
    {
264
        // TODO: Implement withUserInfo() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
265
    }
266
267
    /**
268
     * Return an instance with the specified host.
269
     *
270
     * This method MUST retain the state of the current instance, and return
271
     * an instance that contains the specified host.
272
     *
273
     * An empty host value is equivalent to removing the host.
274
     *
275
     * @param string $host The hostname to use with the new instance.
276
     * @return static A new instance with the specified host.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
277
     * @throws \InvalidArgumentException for invalid hostnames.
278
     */
279
    public function withHost($host)
280
    {
281
        // TODO: Implement withHost() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
282
    }
283
284
    /**
285
     * Return an instance with the specified port.
286
     *
287
     * This method MUST retain the state of the current instance, and return
288
     * an instance that contains the specified port.
289
     *
290
     * Implementations MUST raise an exception for ports outside the
291
     * established TCP and UDP port ranges.
292
     *
293
     * A null value provided for the port is equivalent to removing the port
294
     * information.
295
     *
296
     * @param null|int $port The port to use with the new instance; a null value
297
     *     removes the port information.
298
     * @return static A new instance with the specified port.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
299
     * @throws \InvalidArgumentException for invalid ports.
300
     */
301
    public function withPort($port)
302
    {
303
        // TODO: Implement withPort() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
304
    }
305
306
    /**
307
     * Return an instance with the specified path.
308
     *
309
     * This method MUST retain the state of the current instance, and return
310
     * an instance that contains the specified path.
311
     *
312
     * The path can either be empty or absolute (starting with a slash) or
313
     * rootless (not starting with a slash). Implementations MUST support all
314
     * three syntaxes.
315
     *
316
     * If the path is intended to be domain-relative rather than path relative then
317
     * it must begin with a slash ("/"). Paths not starting with a slash ("/")
318
     * are assumed to be relative to some base path known to the application or
319
     * consumer.
320
     *
321
     * Users can provide both encoded and decoded path characters.
322
     * Implementations ensure the correct encoding as outlined in getPath().
323
     *
324
     * @param string $path The path to use with the new instance.
325
     * @return static A new instance with the specified path.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
326
     * @throws \InvalidArgumentException for invalid paths.
327
     */
328
    public function withPath($path)
329
    {
330
        // TODO: Implement withPath() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
331
    }
332
333
    /**
334
     * Return an instance with the specified query string.
335
     *
336
     * This method MUST retain the state of the current instance, and return
337
     * an instance that contains the specified query string.
338
     *
339
     * Users can provide both encoded and decoded query characters.
340
     * Implementations ensure the correct encoding as outlined in getQuery().
341
     *
342
     * An empty query string value is equivalent to removing the query string.
343
     *
344
     * @param string $query The query string to use with the new instance.
345
     * @return static A new instance with the specified query string.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
346
     * @throws \InvalidArgumentException for invalid query strings.
347
     */
348
    public function withQuery($query)
349
    {
350
        // TODO: Implement withQuery() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
351
    }
352
353
    /**
354
     * Return an instance with the specified URI fragment.
355
     *
356
     * This method MUST retain the state of the current instance, and return
357
     * an instance that contains the specified URI fragment.
358
     *
359
     * Users can provide both encoded and decoded fragment characters.
360
     * Implementations ensure the correct encoding as outlined in getFragment().
361
     *
362
     * An empty fragment value is equivalent to removing the fragment.
363
     *
364
     * @param string $fragment The fragment to use with the new instance.
365
     * @return static A new instance with the specified fragment.
0 ignored issues
show
Documentation introduced by
Should the return type not be Uri|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
366
     */
367
    public function withFragment($fragment)
368
    {
369
        // TODO: Implement withFragment() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
370
    }
371
372
    /**
373
     * Return the string representation as a URI reference.
374
     *
375
     * Depending on which components of the URI are present, the resulting
376
     * string is either a full URI or relative reference according to RFC 3986,
377
     * Section 4.1. The method concatenates the various components of the URI,
378
     * using the appropriate delimiters:
379
     *
380
     * - If a scheme is present, it MUST be suffixed by ":".
381
     * - If an authority is present, it MUST be prefixed by "//".
382
     * - The path can be concatenated without delimiters. But there are two
383
     *   cases where the path has to be adjusted to make the URI reference
384
     *   valid as PHP does not allow to throw an exception in __toString():
385
     *     - If the path is rootless and an authority is present, the path MUST
386
     *       be prefixed by "/".
387
     *     - If the path is starting with more than one "/" and no authority is
388
     *       present, the starting slashes MUST be reduced to one.
389
     * - If a query is present, it MUST be prefixed by "?".
390
     * - If a fragment is present, it MUST be prefixed by "#".
391
     *
392
     * @see http://tools.ietf.org/html/rfc3986#section-4.1
393
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
394
     */
395
    public function __toString()
396
    {
397
        // TODO: Implement __toString() method.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
398
    }
399
400
    private function explodeUri($uri)
401
    {
402
        echo $uri . "\n";
403
404
        $uri_parts = array();
405
406
        $reg_start              = '/^';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 14 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
407
        $scheme_part            = '(.+?)';  //Adding '?' makes it lazy since a URI may contain several colons
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 12 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
408
        $scheme_separator       = ':';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 1 space but found 7 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
409
        $hier_part              = '(.+)';
0 ignored issues
show
Unused Code introduced by
$hier_part is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 14 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
410
        $query_part             = '\?';
0 ignored issues
show
Unused Code introduced by
$query_part is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 13 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
411
        $query_separator        = '#';
0 ignored issues
show
Unused Code introduced by
$query_separator is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 8 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
412
        $reg_end                = '/';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 16 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
413
414
        $uri_syntax =   $reg_start .
415
                        $scheme_part .
416
                        $scheme_separator .
417
                        $reg_end;
418
419
        echo $uri_syntax . "\n";
420
421
        $uri_valid = preg_match($uri_syntax, $uri, $parts);
422
423
        if ($uri_valid) {
424
            $uri_parts["scheme"] = $parts[1];
425
        } else {
426
            $uri_parts["scheme"] = "";
427
        }
428
429
        echo $uri_parts["scheme"] . "\n";
430
431
        return $uri_parts;
432
    }
433
}
434