Conditions | 4 |
Paths | 5 |
Total Lines | 172 |
Code Lines | 8 |
Lines | 0 |
Ratio | 0 % |
Tests | 8 |
CRAP Score | 4.0218 |
Changes | 0 |
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:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
52 | 8 | public function __construct( |
|
53 | /** |
||
54 | * @var array Custom network aliases, that can be used in {@see $ranges}. |
||
55 | * |
||
56 | * - key - alias name |
||
57 | * - value - array of strings. String can be an IP range, IP address or another alias. String can be |
||
58 | * negated with {@see NEGATION_CHAR} (independent of {@see $allowNegation} option). |
||
59 | * |
||
60 | * The following aliases are defined by default in {@see $defaultNetworks} and will be merged with custom ones: |
||
61 | * |
||
62 | * - `*`: `any` |
||
63 | * - `any`: `0.0.0.0/0, ::/0` |
||
64 | * - `private`: `10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8` |
||
65 | * - `multicast`: `224.0.0.0/4, ff00::/8` |
||
66 | * - `linklocal`: `169.254.0.0/16, fe80::/10` |
||
67 | * - `localhost`: `127.0.0.0/8', ::1` |
||
68 | * - `documentation`: `192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32` |
||
69 | * - `system`: `multicast, linklocal, localhost, documentation` |
||
70 | * |
||
71 | * @see $defaultNetworks |
||
72 | */ |
||
73 | private array $networks = [], |
||
74 | /** |
||
75 | * @var bool whether the validating value can be an IPv4 address. Defaults to `true`. |
||
76 | */ |
||
77 | private bool $allowIpv4 = true, |
||
78 | /** |
||
79 | * @var bool whether the validating value can be an IPv6 address. Defaults to `true`. |
||
80 | */ |
||
81 | private bool $allowIpv6 = true, |
||
82 | /** |
||
83 | * @var bool whether the address can be an IP with CIDR subnet, like `192.168.10.0/24`. |
||
84 | * The following values are possible: |
||
85 | * |
||
86 | * - `false` - the address must not have a subnet (default). |
||
87 | * - `true` - specifying a subnet is optional. |
||
88 | */ |
||
89 | private bool $allowSubnet = false, |
||
90 | private bool $requireSubnet = false, |
||
91 | /** |
||
92 | * @var bool whether address may have a {@see NEGATION_CHAR} character at the beginning. |
||
93 | * Defaults to `false`. |
||
94 | */ |
||
95 | private bool $allowNegation = false, |
||
96 | /** |
||
97 | * @var string user-defined error message is used when validation fails due to the wrong IP address format. |
||
98 | * |
||
99 | * You may use the following placeholders in the message: |
||
100 | * |
||
101 | * - `{attribute}`: the label of the attribute being validated |
||
102 | * - `{value}`: the value of the attribute being validated |
||
103 | */ |
||
104 | private string $message = 'Must be a valid IP address.', |
||
105 | /** |
||
106 | * @var string user-defined error message is used when validation fails due to the disabled IPv4 validation. |
||
107 | * |
||
108 | * You may use the following placeholders in the message: |
||
109 | * |
||
110 | * - `{attribute}`: the label of the attribute being validated |
||
111 | * - `{value}`: the value of the attribute being validated |
||
112 | * |
||
113 | * @see $allowIpv4 |
||
114 | */ |
||
115 | private string $ipv4NotAllowedMessage = 'Must not be an IPv4 address.', |
||
116 | /** |
||
117 | * @var string user-defined error message is used when validation fails due to the disabled IPv6 validation. |
||
118 | * |
||
119 | * You may use the following placeholders in the message: |
||
120 | * |
||
121 | * - `{attribute}`: the label of the attribute being validated |
||
122 | * - `{value}`: the value of the attribute being validated |
||
123 | * |
||
124 | * @see $allowIpv6 |
||
125 | */ |
||
126 | private string $ipv6NotAllowedMessage = 'Must not be an IPv6 address.', |
||
127 | /** |
||
128 | * @var string user-defined error message is used when validation fails due to the wrong CIDR. |
||
129 | * |
||
130 | * You may use the following placeholders in the message: |
||
131 | * |
||
132 | * - `{attribute}`: the label of the attribute being validated |
||
133 | * - `{value}`: the value of the attribute being validated |
||
134 | * |
||
135 | * @see $allowSubnet |
||
136 | */ |
||
137 | private string $wrongCidrMessage = 'Contains wrong subnet mask.', |
||
138 | /** |
||
139 | * @var string user-defined error message is used when validation fails due to subnet {@see $allowSubnet} is |
||
140 | * used, but the CIDR prefix is not set. |
||
141 | * |
||
142 | * You may use the following placeholders in the message: |
||
143 | * |
||
144 | * - `{attribute}`: the label of the attribute being validated |
||
145 | * - `{value}`: the value of the attribute being validated |
||
146 | * |
||
147 | * @see $allowSubnet |
||
148 | */ |
||
149 | private string $noSubnetMessage = 'Must be an IP address with specified subnet.', |
||
150 | /** |
||
151 | * @var string user-defined error message is used when validation fails |
||
152 | * due to {@see $allowSubnet} is false, but CIDR prefix is present. |
||
153 | * |
||
154 | * You may use the following placeholders in the message: |
||
155 | * |
||
156 | * - `{attribute}`: the label of the attribute being validated |
||
157 | * - `{value}`: the value of the attribute being validated |
||
158 | * |
||
159 | * @see $allowSubnet |
||
160 | */ |
||
161 | private string $hasSubnetMessage = 'Must not be a subnet.', |
||
162 | /** |
||
163 | * @var string user-defined error message is used when validation fails due to IP address |
||
164 | * is not allowed by {@see $ranges} check. |
||
165 | * |
||
166 | * You may use the following placeholders in the message: |
||
167 | * |
||
168 | * - `{attribute}`: the label of the attribute being validated |
||
169 | * - `{value}`: the value of the attribute being validated |
||
170 | * |
||
171 | * @see $ranges |
||
172 | */ |
||
173 | private string $notInRangeMessage = 'Is not in the allowed range.', |
||
174 | /** |
||
175 | * @var string[] The IPv4 or IPv6 ranges that are allowed or forbidden. |
||
176 | * |
||
177 | * The following preparation tasks are performed: |
||
178 | * |
||
179 | * - Recursively substitutes aliases (described in {@see $networks}) with their values. |
||
180 | * - Removes duplicates. |
||
181 | * |
||
182 | * When the array is empty, or the option not set, all IP addresses are allowed. |
||
183 | * |
||
184 | * Otherwise, the rules are checked sequentially until the first match is found. |
||
185 | * An IP address is forbidden, when it has not matched any of the rules. |
||
186 | * |
||
187 | * Example: |
||
188 | * |
||
189 | * ```php |
||
190 | * (new Ip(ranges: [ |
||
191 | * '192.168.10.128' |
||
192 | * '!192.168.10.0/24', |
||
193 | * 'any' // allows any other IP addresses |
||
194 | * ]); |
||
195 | * ``` |
||
196 | * |
||
197 | * In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the `192.168.10.0/24` |
||
198 | * subnet. IPv4 address `192.168.10.128` is also allowed, because it is listed before the restriction. |
||
199 | */ |
||
200 | private array $ranges = [], |
||
201 | private bool $skipOnEmpty = false, |
||
202 | private $skipOnEmptyCallback = null, |
||
203 | private bool $skipOnError = false, |
||
204 | /** |
||
205 | * @var Closure(mixed, ValidationContext):bool|null |
||
206 | */ |
||
207 | private ?Closure $when = null, |
||
208 | ) { |
||
209 | 8 | $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback); |
|
210 | |||
211 | 8 | foreach ($networks as $key => $_values) { |
|
212 | 1 | if (array_key_exists($key, $this->defaultNetworks)) { |
|
213 | 1 | throw new RuntimeException("Network alias \"{$key}\" already set as default."); |
|
214 | } |
||
215 | } |
||
216 | |||
217 | 7 | $this->networks = array_merge($this->defaultNetworks, $this->networks); |
|
218 | |||
219 | 7 | if ($requireSubnet) { |
|
220 | $this->allowSubnet = true; |
||
221 | } |
||
222 | |||
223 | 7 | $this->ranges = $this->prepareRanges($ranges); |
|
224 | } |
||
448 |