Completed
Push — master ( c80620...0cd8d9 )
by Martijn
03:25
created

Rfc3986::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 120
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 72
nc 1
nop 1
dl 0
loc 120
rs 8.6109
c 0
b 0
f 0

How to fix   Long Method   

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
/** @noinspection PhpUndefinedFieldInspection */
3
4
/**
5
 * RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax
6
 *
7
 * @see https://tools.ietf.org/html/rfc3986
8
 */
9
10
namespace Vanderlee\Comprehend\Library;
11
12
use Vanderlee\Comprehend\builder\AbstractRuleset;
13
use Vanderlee\Comprehend\Parser\Parser;
14
use Vanderlee\Comprehend\Parser\Terminal\Nothing;
15
16
require_once 'functions.php';
17
18
/**
19
 * @property-read Parser IPv6address
20
 * @property-read Parser IPv4address
21
 * @property-read Parser sub_delims
22
 *
23
 * @package Vanderlee\Comprehend\Library
24
 */
25
class Rfc3986 extends AbstractRuleset
26
{
27
    protected static $name = 'Rfc3986';
28
29
    public function __construct($overwrites = [])
30
    {
31
        /** @var Rfc2234 $abnf */
32
        /** @noinspection PhpUndefinedMethodInspection */
33
        $abnf = new Rfc2234();
34
35
        // Defined out-of-order for performance reasons
36
        $rules = [
37
            // 2.1.  Percent-Encoding
38
            'pct_encoded'   => s("%", $abnf->HEXDIG, $abnf->HEXDIG),
39
40
            // 2.2.  Reserved Characters
41
            'sub_delims'    => set('!$&\'()*+,;='),
42
            'gen_delims'    => set(':/?#[]@'),
43
            'reserved'      => c($this->gen_delims, $this->sub_delims),
0 ignored issues
show
Bug Best Practice introduced by
The property gen_delims does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
44
45
            // 2.3.  Unreserved Characters
46
            'unreserved'    => c($abnf->ALPHA, $abnf->DIGIT, set('-._~')),
47
48
            // 3.3.  Path
49
            'pchar'         => c($this->unreserved, $this->pct_encoded, $this->sub_delims, ':', '@'),
0 ignored issues
show
Bug Best Practice introduced by
The property pct_encoded does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property unreserved does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
50
            'segment'       => star($this->pchar),
0 ignored issues
show
Bug Best Practice introduced by
The property pchar does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
51
            'segment_nz'    => plus($this->pchar),
52
            'segment_nz_nc' => plus(c($this->unreserved, $this->pct_encoded, $this->sub_delims, '@')),
53
            'path_abempty'  => star(['/', $this->segment]),
0 ignored issues
show
Bug Best Practice introduced by
The property segment does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
54
            'path_absolute' => s('/', opt([$this->segment_nz, star(['/', $this->segment])])),
0 ignored issues
show
Bug Best Practice introduced by
The property segment_nz does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
55
            'path_noscheme' => s($this->segment_nz_nc, star(['/', $this->segment])),
0 ignored issues
show
Bug Best Practice introduced by
The property segment_nz_nc does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
56
            'path_rootless' => s($this->segment_nz, star(['/', $this->segment])),
57
            'path_empty'    => new Nothing(),
58
            'path'          => c($this->path_abempty,
0 ignored issues
show
Bug Best Practice introduced by
The property path_abempty does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
59
                $this->path_absolute,
0 ignored issues
show
Bug Best Practice introduced by
The property path_absolute does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
60
                $this->path_noscheme,
0 ignored issues
show
Bug Best Practice introduced by
The property path_noscheme does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
                $this->path_rootless,
0 ignored issues
show
Bug Best Practice introduced by
The property path_rootless does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
62
                $this->path_empty
0 ignored issues
show
Bug Best Practice introduced by
The property path_empty does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
            ),
64
65
            // 3.4.  Query
66
            'query'         => star(c($this->pchar, '/', '?')),
67
68
            // 3.5.  Fragment
69
            'fragment    '  => star(c($this->pchar, '/', '?')),
70
71
            // 3.  Syntax Components
72
            'hier_part'     => s('//', $this->authority, [
0 ignored issues
show
Bug Best Practice introduced by
The property authority does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
73
                $this->path_abempty,
74
                $this->path_absolute,
75
                $this->path_rootless,
76
                $this->path_empty,
77
            ]),
78
            'URI'           => s($this->scheme, ':', $this->hier_part, opt(['?', $this->query]),
0 ignored issues
show
Bug Best Practice introduced by
The property scheme does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property hier_part does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property query does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
79
                opt(['#', $this->fragment])),
0 ignored issues
show
Bug Best Practice introduced by
The property fragment does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
80
81
            // 3.1.  Scheme
82
            'scheme'        => s($abnf->ALPHA, star(c($abnf->ALPHA, $abnf->DIGIT, '+', '-', '.'))),
83
84
            // 3.2.  Authority
85
            'authority'     => s(opt([$this->userinfo, '@']), $this->host, opt([':', $this->port])),
0 ignored issues
show
Bug Best Practice introduced by
The property host does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property userinfo does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property port does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
86
87
            // 3.2.1.  User Information
88
            'userinfo'      => star(c($this->unreserved, $this->pct_encoded, $this->sub_delims, ':')),
89
90
            // 3.2.3.  Port
91
            'port'          => star($abnf->DIGIT),
92
93
            // 3.2.2.  Host
94
            'h16'           => repeat(1, 4, $abnf->HEXDIG),
95
            'ls32'          => c([$this->h16, ':', $this->h16], $this->IPv4address),
0 ignored issues
show
Bug Best Practice introduced by
The property h16 does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
96
            'IPv6address'   => c(
97
                [repeat(6, 6, [$this->h16, ':']), $this->ls32],
0 ignored issues
show
Bug Best Practice introduced by
The property ls32 does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
98
                ['::', repeat(5, 5, [$this->h16, ':']), $this->ls32],
99
                [opt($this->h16), '::', repeat(4, 4, [$this->h16, ':']), $this->ls32],
100
                [
101
                    opt([repeat(0, 1, [$this->h16, ':',]), $this->h16]),
102
                    '::',
103
                    repeat(3, 3, [$this->h16, ':']),
104
                    $this->ls32
105
                ],
106
                [
107
                    opt([repeat(0, 2, [$this->h16, ':',]), $this->h16]),
108
                    '::',
109
                    repeat(2, 2, [$this->h16, ':']),
110
                    $this->ls32
111
                ],
112
                [opt([repeat(0, 3, [$this->h16, ':',]), $this->h16]), '::', $this->h16, ':', $this->ls32],
113
                [opt([repeat(0, 4, [$this->h16, ':',]), $this->h16]), '::', $this->ls32],
114
                [opt([repeat(0, 5, [$this->h16, ':',]), $this->h16]), '::', $this->h16],
115
                [opt([repeat(0, 6, [$this->h16, ':',]), $this->h16]), '::']
116
            ),
117
            'dec_octet'     => c(
118
                ['25', range('0', '5')], // 250-255
119
                ['2', range('0', '4'), $abnf->DIGIT], // 200-249
120
                ['1', repeat(2, 2, $abnf->DIGIT)], // 100-199
121
                [range('1', '9'), $abnf->DIGIT], // 10-99
122
                $abnf->DIGIT   // 0-9
123
            ),
124
            'IPv4address'   => s($this->dec_octet, '.', $this->dec_octet, '.', $this->dec_octet, '.', $this->dec_octet),
0 ignored issues
show
Bug Best Practice introduced by
The property dec_octet does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
125
            'IPvFuture'     => s('v', plus($abnf->HEXDIG), '.', plus(c($this->unreserved, $this->sub_delims, ':'))),
126
            'IP_literal'    => s('[', [$this->IPv6address, $this->IPvFuture], ']'),
0 ignored issues
show
Bug Best Practice introduced by
The property IPvFuture does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
127
            'reg_name'      => star(c($this->unreserved, $this->pct_encoded, $this->sub_delims)),
128
            'host'          => c($this->IP_literal, $this->IPv4address, $this->reg_name),
0 ignored issues
show
Bug Best Practice introduced by
The property reg_name does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property IP_literal does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
129
130
            // 4.2.  Relative Reference
131
            'relative_part' => s('//', $this->authority, [
132
                $this->path_abempty,
133
                $this->path_absolute,
134
                $this->path_noscheme,
135
                $this->path_empty
136
            ]),
137
            'relative_ref'  => s($this->relative_part, opt(['?', $this->query]), opt(['#', $this->fragment])),
0 ignored issues
show
Bug Best Practice introduced by
The property relative_part does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
138
139
            // 4.1.  URI Reference
140
            'URI_reference' => c($this->URI, $this->relative_ref),
0 ignored issues
show
Bug Best Practice introduced by
The property URI does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property relative_ref does not exist on Vanderlee\Comprehend\Library\Rfc3986. Since you implemented __get, consider adding a @property annotation.
Loading history...
141
142
            // 4.3.  Absolute URI
143
            'absolute_URI'  => s($this->scheme, ':', $this->hier_part, opt(['?', $this->query])),
144
145
            //            self::DEFAULT => $this->rulelist,
146
        ];
147
148
        parent::__construct(array_merge($rules, $overwrites));
149
    }
150
}