Complex classes like IpValidator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IpValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class IpValidator extends Validator |
||
41 | { |
||
42 | /** |
||
43 | * The length of IPv6 address in bits |
||
44 | */ |
||
45 | const IPV6_ADDRESS_LENGTH = 128; |
||
46 | /** |
||
47 | * The length of IPv4 address in bits |
||
48 | */ |
||
49 | const IPV4_ADDRESS_LENGTH = 32; |
||
50 | /** |
||
51 | * Negation char. Used to negate [[ranges]] or [[networks]] |
||
52 | * or to negate validating value when [[negation]] is set to `true` |
||
53 | * @see negation |
||
54 | * @see networks |
||
55 | * @see ranges |
||
56 | */ |
||
57 | const NEGATION_CHAR = '!'; |
||
58 | |||
59 | /** |
||
60 | * @var array The network aliases, that can be used in [[ranges]]. |
||
61 | * - key - alias name |
||
62 | * - value - array of strings. String can be an IP range, IP address or another alias. String can be |
||
63 | * negated with [[NEGATION_CHAR]] (independent of `negation` option). |
||
64 | * |
||
65 | * The following aliases are defined by default: |
||
66 | * - `*`: `any` |
||
67 | * - `any`: `0.0.0.0/0, ::/0` |
||
68 | * - `private`: `10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8` |
||
69 | * - `multicast`: `224.0.0.0/4, ff00::/8` |
||
70 | * - `linklocal`: `169.254.0.0/16, fe80::/10` |
||
71 | * - `localhost`: `127.0.0.0/8', ::1` |
||
72 | * - `documentation`: `192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32` |
||
73 | * - `system`: `multicast, linklocal, localhost, documentation` |
||
74 | * |
||
75 | */ |
||
76 | public $networks = [ |
||
77 | '*' => ['any'], |
||
78 | 'any' => ['0.0.0.0/0', '::/0'], |
||
79 | 'private' => ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'fd00::/8'], |
||
80 | 'multicast' => ['224.0.0.0/4', 'ff00::/8'], |
||
81 | 'linklocal' => ['169.254.0.0/16', 'fe80::/10'], |
||
82 | 'localhost' => ['127.0.0.0/8', '::1'], |
||
83 | 'documentation' => ['192.0.2.0/24', '198.51.100.0/24', '203.0.113.0/24', '2001:db8::/32'], |
||
84 | 'system' => ['multicast', 'linklocal', 'localhost', 'documentation'], |
||
85 | ]; |
||
86 | /** |
||
87 | * @var bool whether the validating value can be an IPv6 address. Defaults to `true`. |
||
88 | */ |
||
89 | public $ipv6 = true; |
||
90 | /** |
||
91 | * @var bool whether the validating value can be an IPv4 address. Defaults to `true`. |
||
92 | */ |
||
93 | public $ipv4 = true; |
||
94 | /** |
||
95 | * @var bool whether the address can be an IP with CIDR subnet, like `192.168.10.0/24`. |
||
96 | * The following values are possible: |
||
97 | * |
||
98 | * - `false` - the address must not have a subnet (default). |
||
99 | * - `true` - specifying a subnet is required. |
||
100 | * - `null` - specifying a subnet is optional. |
||
101 | */ |
||
102 | public $subnet = false; |
||
103 | /** |
||
104 | * @var bool whether to add the CIDR prefix with the smallest length (32 for IPv4 and 128 for IPv6) to an |
||
105 | * address without it. Works only when `subnet` is not `false`. For example: |
||
106 | * - `10.0.1.5` will normalized to `10.0.1.5/32` |
||
107 | * - `2008:db0::1` will be normalized to `2008:db0::1/128` |
||
108 | * Defaults to `false`. |
||
109 | * @see subnet |
||
110 | */ |
||
111 | public $normalize = false; |
||
112 | /** |
||
113 | * @var bool whether address may have a [[NEGATION_CHAR]] character at the beginning. |
||
114 | * Defaults to `false`. |
||
115 | */ |
||
116 | public $negation = false; |
||
117 | /** |
||
118 | * @var bool whether to expand an IPv6 address to the full notation format. |
||
119 | * Defaults to `false`. |
||
120 | */ |
||
121 | public $expandIPv6 = false; |
||
122 | /** |
||
123 | * @var string Regexp-pattern to validate IPv4 address |
||
124 | */ |
||
125 | public $ipv4Pattern = '/^(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))$/'; |
||
126 | /** |
||
127 | * @var string Regexp-pattern to validate IPv6 address |
||
128 | */ |
||
129 | public $ipv6Pattern = '/^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/'; |
||
130 | /** |
||
131 | * @var string user-defined error message is used when validation fails due to the wrong IP address format. |
||
132 | * |
||
133 | * You may use the following placeholders in the message: |
||
134 | * |
||
135 | * - `{attribute}`: the label of the attribute being validated |
||
136 | * - `{value}`: the value of the attribute being validated |
||
137 | */ |
||
138 | public $message; |
||
139 | /** |
||
140 | * @var string user-defined error message is used when validation fails due to the disabled IPv6 validation. |
||
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 ipv6 |
||
148 | */ |
||
149 | public $ipv6NotAllowed; |
||
150 | /** |
||
151 | * @var string user-defined error message is used when validation fails due to the disabled IPv4 validation. |
||
152 | * |
||
153 | * You may use the following placeholders in the message: |
||
154 | * |
||
155 | * - `{attribute}`: the label of the attribute being validated |
||
156 | * - `{value}`: the value of the attribute being validated |
||
157 | * |
||
158 | * @see ipv4 |
||
159 | */ |
||
160 | public $ipv4NotAllowed; |
||
161 | /** |
||
162 | * @var string user-defined error message is used when validation fails due to the wrong CIDR. |
||
163 | * |
||
164 | * You may use the following placeholders in the message: |
||
165 | * |
||
166 | * - `{attribute}`: the label of the attribute being validated |
||
167 | * - `{value}`: the value of the attribute being validated |
||
168 | * @see subnet |
||
169 | */ |
||
170 | public $wrongCidr; |
||
171 | /** |
||
172 | * @var string user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', |
||
173 | * but the CIDR prefix is not set. |
||
174 | * |
||
175 | * You may use the following placeholders in the message: |
||
176 | * |
||
177 | * - `{attribute}`: the label of the attribute being validated |
||
178 | * - `{value}`: the value of the attribute being validated |
||
179 | * |
||
180 | * @see subnet |
||
181 | */ |
||
182 | public $noSubnet; |
||
183 | /** |
||
184 | * @var string user-defined error message is used when validation fails |
||
185 | * due to [[subnet]] is false, but CIDR prefix is present. |
||
186 | * |
||
187 | * You may use the following placeholders in the message: |
||
188 | * |
||
189 | * - `{attribute}`: the label of the attribute being validated |
||
190 | * - `{value}`: the value of the attribute being validated |
||
191 | * |
||
192 | * @see subnet |
||
193 | */ |
||
194 | public $hasSubnet; |
||
195 | /** |
||
196 | * @var string user-defined error message is used when validation fails due to IP address |
||
197 | * is not not allowed by [[ranges]] check. |
||
198 | * |
||
199 | * You may use the following placeholders in the message: |
||
200 | * |
||
201 | * - `{attribute}`: the label of the attribute being validated |
||
202 | * - `{value}`: the value of the attribute being validated |
||
203 | * |
||
204 | * @see ranges |
||
205 | */ |
||
206 | public $notInRange; |
||
207 | |||
208 | /** |
||
209 | * @var array |
||
210 | */ |
||
211 | private $_ranges = []; |
||
212 | |||
213 | |||
214 | /** |
||
215 | * @inheritdoc |
||
216 | */ |
||
217 | 26 | public function init() |
|
251 | |||
252 | /** |
||
253 | * Set the IPv4 or IPv6 ranges that are allowed or forbidden. |
||
254 | * |
||
255 | * The following preparation tasks are performed: |
||
256 | * |
||
257 | * - Recursively substitutes aliases (described in [[networks]]) with their values. |
||
258 | * - Removes duplicates |
||
259 | * |
||
260 | * @property array the IPv4 or IPv6 ranges that are allowed or forbidden. |
||
261 | * See [[setRanges()]] for detailed description. |
||
262 | * @param array $ranges the IPv4 or IPv6 ranges that are allowed or forbidden. |
||
263 | * |
||
264 | * When the array is empty, or the option not set, all IP addresses are allowed. |
||
265 | * |
||
266 | * Otherwise, the rules are checked sequentially until the first match is found. |
||
267 | * An IP address is forbidden, when it has not matched any of the rules. |
||
268 | * |
||
269 | * Example: |
||
270 | * |
||
271 | * ```php |
||
272 | * [ |
||
273 | * 'ranges' => [ |
||
274 | * '192.168.10.128' |
||
275 | * '!192.168.10.0/24', |
||
276 | * 'any' // allows any other IP addresses |
||
277 | * ] |
||
278 | * ] |
||
279 | * ``` |
||
280 | * |
||
281 | * In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the `192.168.10.0/24` subnet. |
||
282 | * IPv4 address `192.168.10.128` is also allowed, because it is listed before the restriction. |
||
283 | */ |
||
284 | 8 | public function setRanges($ranges) |
|
288 | |||
289 | /** |
||
290 | * @return array The IPv4 or IPv6 ranges that are allowed or forbidden. |
||
291 | */ |
||
292 | 13 | public function getRanges() |
|
296 | |||
297 | /** |
||
298 | * @inheritdoc |
||
299 | */ |
||
300 | 13 | protected function validateValue($value) |
|
310 | |||
311 | /** |
||
312 | * @inheritdoc |
||
313 | */ |
||
314 | 8 | public function validateAttribute($model, $attribute) |
|
326 | |||
327 | /** |
||
328 | * Validates an IPv4/IPv6 address or subnet |
||
329 | * |
||
330 | * @param $ip string |
||
331 | * @return string|array |
||
332 | * string - the validation was successful; |
||
333 | * array - an error occurred during the validation. |
||
334 | * Array[0] contains the text of an error, array[1] contains values for the placeholders in the error message |
||
335 | */ |
||
336 | 21 | private function validateSubnet($ip) |
|
411 | |||
412 | /** |
||
413 | * Expands an IPv6 address to it's full notation. For example `2001:db8::1` will be |
||
414 | * expanded to `2001:0db8:0000:0000:0000:0000:0000:0001` |
||
415 | * |
||
416 | * @param string $ip the original IPv6 |
||
417 | * @return string the expanded IPv6 |
||
418 | */ |
||
419 | 1 | private function expandIPv6($ip) |
|
424 | |||
425 | /** |
||
426 | * The method checks whether the IP address with specified CIDR is allowed according to the [[ranges]] list. |
||
427 | * |
||
428 | * @param string $ip |
||
429 | * @param int $cidr |
||
430 | * @return bool |
||
431 | * @see ranges |
||
432 | */ |
||
433 | 9 | private function isAllowed($ip, $cidr) |
|
448 | |||
449 | /** |
||
450 | * Parses IP address/range for the negation with [[NEGATION_CHAR]]. |
||
451 | * |
||
452 | * @param $string |
||
453 | * @return array `[0 => bool, 1 => string]` |
||
454 | * - boolean: whether the string is negated |
||
455 | * - string: the string without negation (when the negation were present) |
||
456 | */ |
||
457 | 8 | private function parseNegatedRange($string) |
|
462 | |||
463 | /** |
||
464 | * Prepares array to fill in [[ranges]]: |
||
465 | * - Recursively substitutes aliases, described in [[networks]] with their values |
||
466 | * - Removes duplicates |
||
467 | * |
||
468 | * |
||
469 | * @param $ranges |
||
470 | * @return array |
||
471 | * @see networks |
||
472 | */ |
||
473 | 8 | private function prepareRanges($ranges) |
|
490 | |||
491 | /** |
||
492 | * Validates IPv4 address |
||
493 | * |
||
494 | * @param string $value |
||
495 | * @return bool |
||
496 | */ |
||
497 | 8 | protected function validateIPv4($value) |
|
501 | |||
502 | /** |
||
503 | * Validates IPv6 address |
||
504 | * |
||
505 | * @param string $value |
||
506 | * @return bool |
||
507 | */ |
||
508 | 8 | protected function validateIPv6($value) |
|
512 | |||
513 | /** |
||
514 | * Gets the IP version |
||
515 | * |
||
516 | * @param string $ip |
||
517 | * @return int |
||
518 | */ |
||
519 | 13 | private function getIpVersion($ip) |
|
523 | |||
524 | /** |
||
525 | * Used to get the Regexp pattern for initial IP address parsing |
||
526 | * @return string |
||
527 | */ |
||
528 | 13 | private function getIpParsePattern() |
|
532 | |||
533 | /** |
||
534 | * Checks whether the IP is in subnet range |
||
535 | * |
||
536 | * @param string $ip an IPv4 or IPv6 address |
||
537 | * @param int $cidr |
||
538 | * @param string $range subnet in CIDR format e.g. `10.0.0.0/8` or `2001:af::/64` |
||
539 | * @return bool |
||
540 | */ |
||
541 | 4 | private function inRange($ip, $cidr, $range) |
|
562 | |||
563 | /** |
||
564 | * Converts IP address to bits representation |
||
565 | * |
||
566 | * @param string $ip |
||
567 | * @return string bits as a string |
||
568 | */ |
||
569 | 4 | private function ip2bin($ip) |
|
584 | |||
585 | /** |
||
586 | * @inheritdoc |
||
587 | */ |
||
588 | public function clientValidateAttribute($model, $attribute, $view) |
||
595 | |||
596 | /** |
||
597 | * @inheritdoc |
||
598 | */ |
||
599 | public function getClientOptions($model, $attribute) |
||
630 | } |
||
631 |