Completed
Push — master ( 11a693...420c23 )
by ignace nyamagana
03:57
created

functions.php ➔ uri_reference()   C

Complexity

Conditions 12
Paths 18

Size

Total Lines 56
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 32
c 1
b 0
f 0
nc 18
nop 2
dl 0
loc 56
ccs 35
cts 35
cp 1
crap 12
rs 6.7092

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2016 Ignace Nyamagana Butera
8
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
9
 * @version   4.2.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri\Modifiers;
13
14
use InvalidArgumentException;
15
use League\Uri\Interfaces\Uri;
16
use Psr\Http\Message\UriInterface;
17
18
/**
19
 * A function to give information about URI Reference
20
 *
21
 * This function returns an associative array representing the URI Reference information:
22
 * each key represents a given state and each value is a boolean to indicate the current URI
23
 * status against the declared state.
24
 *
25
 * <ul>
26
 * <li>absolute_uri: Tell whether the URI is absolute
27
 * <li>network_path: Tell whether the URI is a network-path relative reference
28
 * <li>absolute_path: Tell whether the URI is a absolute-path relative reference
29
 * <li>relative_path: Tell whether the URI is a relative-path relative reference
30
 * <li>same_document: Tell whether the URI is a same-document relative reference
31
 * </ul>
32
 *
33
 * @link https://tools.ietf.org/html/rfc3986#section-4.2
34
 * @link https://tools.ietf.org/html/rfc3986#section-4.3
35
 * @link https://tools.ietf.org/html/rfc3986#section-4.4
36
 *
37
 * @since 4.2.0
38
 *
39
 * @param Uri|UriInterface      $uri      The uri to get reference info from
40
 * @param Uri|UriInterface|null $base_uri The base uri to use to get same document reference info
41
 *
42
 * @throws InvalidArgumentException if the submitted Uri is invalid
43
 *
44
 * @return array
45
 */
46
function uri_reference($uri, $base_uri = null)
47
{
48 258
    if (!$uri instanceof Uri && !$uri instanceof UriInterface) {
49 3
        throw new InvalidArgumentException(
50 1
            'URI passed must implement PSR-7 UriInterface or League\Uri Uri interface'
51 2
        );
52
    }
53
54 255
    if (null !== $base_uri && (!$base_uri instanceof Uri && !$base_uri instanceof UriInterface)) {
55 3
        throw new InvalidArgumentException(
56 1
            'The base URI passed must implement PSR-7 UriInterface or League\Uri Uri interface'
57 2
        );
58
    }
59
60
    $infos = [
61 252
        'absolute_uri' => false,
62 168
        'network_path' => false,
63 168
        'absolute_path' => false,
64 168
        'relative_path' => false,
65 168
        'same_document' => false,
66 168
    ];
67
68 252
    static $normalizer;
69 252
    if (null === $normalizer) {
70 3
        $normalizer = new Normalize();
71 2
    }
72
73 252
    if (null !== $base_uri) {
74 6
        $uri_string = (string) $normalizer($uri)->withFragment('');
75 6
        $base_uri_string = (string) $normalizer($base_uri)->withFragment('');
76 6
        $infos['same_document'] = $uri_string === $base_uri_string;
77 4
    }
78
79 252
    if ('' !== $uri->getScheme()) {
80 111
        $infos['absolute_uri'] = true;
81
82 111
        return $infos;
83
    }
84
85 159
    if ('' !== $uri->getAuthority()) {
86 9
        $infos['network_path'] = true;
87
88 9
        return $infos;
89
    }
90
91 150
    $path = $uri->getPath();
92 150
    if (isset($path[0]) && '/' === $path[0]) {
93 12
        $infos['absolute_path'] = true;
94
95 12
        return $infos;
96
    }
97
98 138
    $infos['relative_path'] = true;
99
100 138
    return $infos;
101
}
102