| Conditions | 20 |
| Paths | 36 |
| Total Lines | 99 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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:
| 1 | <?php |
||
| 187 | private function validateDomainLiteral(string $domain): bool |
||
| 188 | { |
||
| 189 | // Must be enclosed in brackets |
||
| 190 | if (!preg_match('/^\[(.*)]$/', $domain, $matches)) { |
||
| 191 | return false; |
||
| 192 | } |
||
| 193 | |||
| 194 | $content = $matches[1]; |
||
| 195 | |||
| 196 | // Handle IPv6 |
||
| 197 | if (stripos($content, 'IPv6:') === 0) { |
||
| 198 | $ipv6 = substr($content, 5); |
||
| 199 | // Remove any whitespace |
||
| 200 | $ipv6 = trim($ipv6); |
||
| 201 | |||
| 202 | // Handle compressed notation |
||
| 203 | if (strpos($ipv6, '::') !== false) { |
||
| 204 | // Only one :: allowed |
||
| 205 | if (substr_count($ipv6, '::') > 1) { |
||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | // Split on :: |
||
| 210 | $parts = explode('::', $ipv6); |
||
| 211 | if (count($parts) !== 2) { |
||
| 212 | return false; |
||
| 213 | } |
||
| 214 | |||
| 215 | // Count segments on each side |
||
| 216 | $leftSegments = $parts[0] ? explode(':', $parts[0]) : []; |
||
| 217 | $rightSegments = $parts[1] ? explode(':', $parts[1]) : []; |
||
| 218 | |||
| 219 | // Calculate missing segments |
||
| 220 | $totalSegments = count($leftSegments) + count($rightSegments); |
||
| 221 | if ($totalSegments >= 8) { |
||
| 222 | return false; |
||
| 223 | } |
||
| 224 | |||
| 225 | // Fill in missing segments |
||
| 226 | $middleSegments = array_fill(0, 8 - $totalSegments, '0'); |
||
| 227 | |||
| 228 | // Combine all segments |
||
| 229 | $segments = array_merge($leftSegments, $middleSegments, $rightSegments); |
||
| 230 | } else { |
||
| 231 | $segments = explode(':', $ipv6); |
||
| 232 | if (count($segments) !== 8) { |
||
| 233 | return false; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | // Validate each segment |
||
| 238 | foreach ($segments as $segment) { |
||
| 239 | if (!preg_match('/^[0-9A-Fa-f]{1,4}$/', $segment)) { |
||
| 240 | return false; |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | // Convert to standard format for final validation |
||
| 245 | $ipv6 = implode(':', array_map(function ($segment) { |
||
| 246 | return str_pad($segment, 4, '0', STR_PAD_LEFT); |
||
| 247 | }, $segments)); |
||
| 248 | |||
| 249 | // Final validation using filter_var |
||
| 250 | if (!filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
||
| 251 | return false; |
||
| 252 | } |
||
| 253 | |||
| 254 | return true; |
||
| 255 | } |
||
| 256 | |||
| 257 | // Handle IPv4 |
||
| 258 | $ipv4 = trim($content); |
||
| 259 | |||
| 260 | // Split into octets |
||
| 261 | $octets = explode('.', $ipv4); |
||
| 262 | if (count($octets) !== 4) { |
||
| 263 | return false; |
||
| 264 | } |
||
| 265 | |||
| 266 | // Validate each octet |
||
| 267 | foreach ($octets as $octet) { |
||
| 268 | // Remove leading zeros |
||
| 269 | $octet = ltrim($octet, '0'); |
||
| 270 | if ($octet === '') { |
||
| 271 | $octet = '0'; |
||
| 272 | } |
||
| 273 | |||
| 274 | // Check numeric value |
||
| 275 | if (!is_numeric($octet) || intval($octet) < 0 || intval($octet) > 255) { |
||
| 276 | return false; |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | // Convert to standard format for final validation |
||
| 281 | $ipv4 = implode('.', array_map(function ($octet) { |
||
| 282 | return ltrim($octet, '0') ?: '0'; |
||
| 283 | }, $octets)); |
||
| 284 | |||
| 285 | return filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false; |
||
| 286 | } |
||
| 345 |