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 |
||
25 | class IpValidator extends Validator |
||
26 | { |
||
27 | /** |
||
28 | * The length of IPv6 address in bits |
||
29 | */ |
||
30 | const IPV6_ADDRESS_LENGTH = 128; |
||
31 | /** |
||
32 | * The length of IPv4 address in bits |
||
33 | */ |
||
34 | const IPV4_ADDRESS_LENGTH = 32; |
||
35 | /** |
||
36 | * Negation char. Used to negate [[ranges]] or [[networks]] |
||
37 | * or to negate validating value when [[negation]] is set to `true` |
||
38 | * @see negation |
||
39 | * @see networks |
||
40 | * @see ranges |
||
41 | */ |
||
42 | const NEGATION_CHAR = '!'; |
||
43 | |||
44 | /** |
||
45 | * @var array The network aliases, that can be used in [[ranges]]. |
||
46 | * - key - alias name |
||
47 | * - value - array of strings. String can be an IP range, IP address or another alias. String can be |
||
48 | * negated with [[NEGATION_CHAR]] (independent of `negation` option). |
||
49 | * |
||
50 | * The following aliases are defined by default: |
||
51 | * - `*`: `any` |
||
52 | * - `any`: `0.0.0.0/0, ::/0` |
||
53 | * - `private`: `10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fd00::/8` |
||
54 | * - `multicast`: `224.0.0.0/4, ff00::/8` |
||
55 | * - `linklocal`: `169.254.0.0/16, fe80::/10` |
||
56 | * - `localhost`: `127.0.0.0/8', ::1` |
||
57 | * - `documentation`: `192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24, 2001:db8::/32` |
||
58 | * - `system`: `multicast, linklocal, localhost, documentation` |
||
59 | * |
||
60 | */ |
||
61 | public $networks = [ |
||
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 | /** |
||
72 | * @var boolean whether the validating value can be an IPv6 address. Defaults to `true`. |
||
73 | */ |
||
74 | public $ipv6 = true; |
||
75 | /** |
||
76 | * @var boolean whether the validating value can be an IPv4 address. Defaults to `true`. |
||
77 | */ |
||
78 | public $ipv4 = true; |
||
79 | /** |
||
80 | * @var boolean whether the address can be an IP with CIDR subnet, like `192.168.10.0/24`. |
||
81 | * The following values are possible: |
||
82 | * |
||
83 | * - `false` - the address must not have a subnet (default). |
||
84 | * - `true` - specifying a subnet is required. |
||
85 | * - `null` - specifying a subnet is optional. |
||
86 | */ |
||
87 | public $subnet = false; |
||
88 | /** |
||
89 | * @var boolean whether to add the CIDR prefix with the smallest length (32 for IPv4 and 128 for IPv6) to an |
||
90 | * address without it. Works only when `subnet` is not `false`. For example: |
||
91 | * - `10.0.1.5` will normalized to `10.0.1.5/32` |
||
92 | * - `2008:db0::1` will be normalized to `2008:db0::1/128` |
||
93 | * Defaults to `false`. |
||
94 | * @see subnet |
||
95 | */ |
||
96 | public $normalize = false; |
||
97 | /** |
||
98 | * @var boolean whether address may have a [[NEGATION_CHAR]] character at the beginning. |
||
99 | * Defaults to `false`. |
||
100 | */ |
||
101 | public $negation = false; |
||
102 | /** |
||
103 | * @var boolean whether to expand an IPv6 address to the full notation format. |
||
104 | * Defaults to `false`. |
||
105 | */ |
||
106 | public $expandIPv6 = false; |
||
107 | /** |
||
108 | * @var string Regexp-pattern to validate IPv4 address |
||
109 | */ |
||
110 | 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]))$/'; |
||
111 | /** |
||
112 | * @var string Regexp-pattern to validate IPv6 address |
||
113 | */ |
||
114 | 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]))$/'; |
||
115 | /** |
||
116 | * @var string user-defined error message is used when validation fails due to the wrong IP address format. |
||
117 | * |
||
118 | * You may use the following placeholders in the message: |
||
119 | * |
||
120 | * - `{attribute}`: the label of the attribute being validated |
||
121 | * - `{value}`: the value of the attribute being validated |
||
122 | */ |
||
123 | public $message; |
||
124 | /** |
||
125 | * @var string user-defined error message is used when validation fails due to the disabled IPv6 validation. |
||
126 | * |
||
127 | * You may use the following placeholders in the message: |
||
128 | * |
||
129 | * - `{attribute}`: the label of the attribute being validated |
||
130 | * - `{value}`: the value of the attribute being validated |
||
131 | * |
||
132 | * @see ipv6 |
||
133 | */ |
||
134 | public $ipv6NotAllowed; |
||
135 | /** |
||
136 | * @var string user-defined error message is used when validation fails due to the disabled IPv4 validation. |
||
137 | * |
||
138 | * You may use the following placeholders in the message: |
||
139 | * |
||
140 | * - `{attribute}`: the label of the attribute being validated |
||
141 | * - `{value}`: the value of the attribute being validated |
||
142 | * |
||
143 | * @see ipv4 |
||
144 | */ |
||
145 | public $ipv4NotAllowed; |
||
146 | /** |
||
147 | * @var string user-defined error message is used when validation fails due to the wrong CIDR. |
||
148 | * |
||
149 | * You may use the following placeholders in the message: |
||
150 | * |
||
151 | * - `{attribute}`: the label of the attribute being validated |
||
152 | * - `{value}`: the value of the attribute being validated |
||
153 | * @see subnet |
||
154 | */ |
||
155 | public $wrongCidr; |
||
156 | /** |
||
157 | * @var string user-defined error message is used when validation fails due to subnet [[subnet]] set to 'only', |
||
158 | * but the CIDR prefix is not set. |
||
159 | * |
||
160 | * You may use the following placeholders in the message: |
||
161 | * |
||
162 | * - `{attribute}`: the label of the attribute being validated |
||
163 | * - `{value}`: the value of the attribute being validated |
||
164 | * |
||
165 | * @see subnet |
||
166 | */ |
||
167 | public $noSubnet; |
||
168 | /** |
||
169 | * @var string user-defined error message is used when validation fails |
||
170 | * due to [[subnet]] is false, but CIDR prefix is present. |
||
171 | * |
||
172 | * You may use the following placeholders in the message: |
||
173 | * |
||
174 | * - `{attribute}`: the label of the attribute being validated |
||
175 | * - `{value}`: the value of the attribute being validated |
||
176 | * |
||
177 | * @see subnet |
||
178 | */ |
||
179 | public $hasSubnet; |
||
180 | /** |
||
181 | * @var string user-defined error message is used when validation fails due to IP address |
||
182 | * is not not allowed by [[ranges]] check. |
||
183 | * |
||
184 | * You may use the following placeholders in the message: |
||
185 | * |
||
186 | * - `{attribute}`: the label of the attribute being validated |
||
187 | * - `{value}`: the value of the attribute being validated |
||
188 | * |
||
189 | * @see ranges |
||
190 | */ |
||
191 | public $notInRange; |
||
192 | |||
193 | /** |
||
194 | * @var array |
||
195 | */ |
||
196 | private $_ranges = []; |
||
197 | |||
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | 19 | public function init() |
|
236 | |||
237 | /** |
||
238 | * Set the IPv4 or IPv6 ranges that are allowed or forbidden. |
||
239 | * |
||
240 | * The following preparation tasks are performed: |
||
241 | * |
||
242 | * - Recursively substitutes aliases (described in [[networks]]) with their values. |
||
243 | * - Removes duplicates |
||
244 | * |
||
245 | * @property array the IPv4 or IPv6 ranges that are allowed or forbidden. |
||
246 | * See [[setRanges()]] for detailed description. |
||
247 | * @param array $ranges the IPv4 or IPv6 ranges that are allowed or forbidden. |
||
248 | * |
||
249 | * When the array is empty, or the option not set, all IP addresses are allowed. |
||
250 | * |
||
251 | * Otherwise, the rules are checked sequentially until the first match is found. |
||
252 | * An IP address is forbidden, when it has not matched any of the rules. |
||
253 | * |
||
254 | * Example: |
||
255 | * |
||
256 | * ```php |
||
257 | * [ |
||
258 | * 'ranges' => [ |
||
259 | * '192.168.10.128' |
||
260 | * '!192.168.10.0/24', |
||
261 | * 'any' // allows any other IP addresses |
||
262 | * ] |
||
263 | * ] |
||
264 | * ``` |
||
265 | * |
||
266 | * In this example, access is allowed for all the IPv4 and IPv6 addresses excluding the `192.168.10.0/24` subnet. |
||
267 | * IPv4 address `192.168.10.128` is also allowed, because it is listed before the restriction. |
||
268 | */ |
||
269 | 8 | public function setRanges($ranges) |
|
273 | |||
274 | /** |
||
275 | * @return array The IPv4 or IPv6 ranges that are allowed or forbidden. |
||
276 | */ |
||
277 | 13 | public function getRanges() |
|
281 | |||
282 | /** |
||
283 | * @inheritdoc |
||
284 | */ |
||
285 | 12 | protected function validateValue($value) |
|
295 | |||
296 | /** |
||
297 | * @inheritdoc |
||
298 | */ |
||
299 | 2 | public function validateAttribute($model, $attribute) |
|
311 | |||
312 | /** |
||
313 | * Validates an IPv4/IPv6 address or subnet |
||
314 | * |
||
315 | * @param $ip string |
||
316 | * @return string|array |
||
317 | * string - the validation was successful; |
||
318 | * array - an error occurred during the validation. |
||
319 | * Array[0] contains the text of an error, array[1] contains values for the placeholders in the error message |
||
320 | */ |
||
321 | 14 | private function validateSubnet($ip) |
|
397 | |||
398 | /** |
||
399 | * Expands an IPv6 address to it's full notation. For example `2001:db8::1` will be |
||
400 | * expanded to `2001:0db8:0000:0000:0000:0000:0000:0001` |
||
401 | * |
||
402 | * @param string $ip the original IPv6 |
||
403 | * @return string the expanded IPv6 |
||
404 | */ |
||
405 | 1 | private function expandIPv6($ip) |
|
410 | |||
411 | /** |
||
412 | * The method checks whether the IP address with specified CIDR is allowed according to the [[ranges]] list. |
||
413 | * |
||
414 | * @param string $ip |
||
415 | * @param integer $cidr |
||
416 | * @return boolean |
||
417 | * @see ranges |
||
418 | */ |
||
419 | 9 | private function isAllowed($ip, $cidr) |
|
434 | |||
435 | /** |
||
436 | * Parses IP address/range for the negation with [[NEGATION_CHAR]]. |
||
437 | * |
||
438 | * @param $string |
||
439 | * @return array `[0 => boolean, 1 => string]` |
||
440 | * - boolean: whether the string is negated |
||
441 | * - string: the string without negation (when the negation were present) |
||
442 | */ |
||
443 | 8 | private function parseNegatedRange ($string) { |
|
447 | |||
448 | /** |
||
449 | * Prepares array to fill in [[ranges]]: |
||
450 | * - Recursively substitutes aliases, described in [[networks]] with their values |
||
451 | * - Removes duplicates |
||
452 | * |
||
453 | * |
||
454 | * @param $ranges |
||
455 | * @return array |
||
456 | * @see networks |
||
457 | */ |
||
458 | 8 | private function prepareRanges($ranges) { |
|
474 | |||
475 | /** |
||
476 | * Validates IPv4 address |
||
477 | * |
||
478 | * @param string $value |
||
479 | * @return boolean |
||
480 | */ |
||
481 | 7 | protected function validateIPv4($value) |
|
485 | |||
486 | /** |
||
487 | * Validates IPv6 address |
||
488 | * |
||
489 | * @param string $value |
||
490 | * @return boolean |
||
491 | */ |
||
492 | 6 | protected function validateIPv6($value) |
|
496 | |||
497 | /** |
||
498 | * Gets the IP version |
||
499 | * |
||
500 | * @param string $ip |
||
501 | * @return integer |
||
502 | */ |
||
503 | 10 | private function getIpVersion($ip) |
|
507 | |||
508 | /** |
||
509 | * Used to get the Regexp pattern for initial IP address parsing |
||
510 | * @return string |
||
511 | */ |
||
512 | 10 | private function getIpParsePattern() |
|
516 | |||
517 | /** |
||
518 | * Checks whether the IP is in subnet range |
||
519 | * |
||
520 | * @param string $ip an IPv4 or IPv6 address |
||
521 | * @param integer $cidr |
||
522 | * @param string $range subnet in CIDR format e.g. `10.0.0.0/8` or `2001:af::/64` |
||
523 | * @return bool |
||
524 | */ |
||
525 | 4 | private function inRange($ip, $cidr, $range) |
|
549 | |||
550 | /** |
||
551 | * Converts IP address to bits representation |
||
552 | * |
||
553 | * @param string $ip |
||
554 | * @return string bits as a string |
||
555 | */ |
||
556 | 4 | private function ip2bin($ip) |
|
571 | |||
572 | /** |
||
573 | * @inheritdoc |
||
574 | */ |
||
575 | public function clientValidateAttribute($model, $attribute, $view) |
||
608 | } |
||
609 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.