Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 18 | class Requests_IPv6 { |
||
| 19 | /** |
||
| 20 | * Uncompresses an IPv6 address |
||
| 21 | * |
||
| 22 | * RFC 4291 allows you to compress consecutive zero pieces in an address to |
||
| 23 | * '::'. This method expects a valid IPv6 address and expands the '::' to |
||
| 24 | * the required number of zero pieces. |
||
| 25 | * |
||
| 26 | * Example: FF01::101 -> FF01:0:0:0:0:0:0:101 |
||
| 27 | * ::1 -> 0:0:0:0:0:0:0:1 |
||
| 28 | * |
||
| 29 | * @author Alexander Merz <[email protected]> |
||
| 30 | * @author elfrink at introweb dot nl |
||
| 31 | * @author Josh Peck <jmp at joshpeck dot org> |
||
| 32 | * @copyright 2003-2005 The PHP Group |
||
| 33 | * @license http://www.opensource.org/licenses/bsd-license.php |
||
| 34 | * @param string $ip An IPv6 address |
||
| 35 | * @return string The uncompressed IPv6 address |
||
| 36 | */ |
||
| 37 | public static function uncompress($ip) { |
||
| 38 | if (substr_count($ip, '::') !== 1) { |
||
| 39 | return $ip; |
||
| 40 | } |
||
| 41 | |||
| 42 | list($ip1, $ip2) = explode('::', $ip); |
||
| 43 | $c1 = ($ip1 === '') ? -1 : substr_count($ip1, ':'); |
||
| 44 | $c2 = ($ip2 === '') ? -1 : substr_count($ip2, ':'); |
||
| 45 | |||
| 46 | if (strpos($ip2, '.') !== false) { |
||
| 47 | $c2++; |
||
| 48 | } |
||
| 49 | // :: |
||
| 50 | if ($c1 === -1 && $c2 === -1) { |
||
| 51 | $ip = '0:0:0:0:0:0:0:0'; |
||
| 52 | } |
||
| 53 | // ::xxx |
||
| 54 | View Code Duplication | elseif ($c1 === -1) { |
|
| 55 | $fill = str_repeat('0:', 7 - $c2); |
||
| 56 | $ip = str_replace('::', $fill, $ip); |
||
| 57 | } |
||
| 58 | // xxx:: |
||
| 59 | View Code Duplication | elseif ($c2 === -1) { |
|
| 60 | $fill = str_repeat(':0', 7 - $c1); |
||
| 61 | $ip = str_replace('::', $fill, $ip); |
||
| 62 | } |
||
| 63 | // xxx::xxx |
||
| 64 | else { |
||
| 65 | $fill = ':' . str_repeat('0:', 6 - $c2 - $c1); |
||
| 66 | $ip = str_replace('::', $fill, $ip); |
||
| 67 | } |
||
| 68 | return $ip; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Compresses an IPv6 address |
||
| 73 | * |
||
| 74 | * RFC 4291 allows you to compress consecutive zero pieces in an address to |
||
| 75 | * '::'. This method expects a valid IPv6 address and compresses consecutive |
||
| 76 | * zero pieces to '::'. |
||
| 77 | * |
||
| 78 | * Example: FF01:0:0:0:0:0:0:101 -> FF01::101 |
||
| 79 | * 0:0:0:0:0:0:0:1 -> ::1 |
||
| 80 | * |
||
| 81 | * @see uncompress() |
||
| 82 | * @param string $ip An IPv6 address |
||
| 83 | * @return string The compressed IPv6 address |
||
| 84 | */ |
||
| 85 | public static function compress($ip) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Splits an IPv6 address into the IPv6 and IPv4 representation parts |
||
| 117 | * |
||
| 118 | * RFC 4291 allows you to represent the last two parts of an IPv6 address |
||
| 119 | * using the standard IPv4 representation |
||
| 120 | * |
||
| 121 | * Example: 0:0:0:0:0:0:13.1.68.3 |
||
| 122 | * 0:0:0:0:0:FFFF:129.144.52.38 |
||
| 123 | * |
||
| 124 | * @param string $ip An IPv6 address |
||
| 125 | * @return string[] [0] contains the IPv6 represented part, and [1] the IPv4 represented part |
||
| 126 | */ |
||
| 127 | protected static function split_v6_v4($ip) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Checks an IPv6 address |
||
| 141 | * |
||
| 142 | * Checks if the given IP is a valid IPv6 address |
||
| 143 | * |
||
| 144 | * @param string $ip An IPv6 address |
||
| 145 | * @return bool true if $ip is a valid IPv6 address |
||
| 146 | */ |
||
| 147 | public static function check_ipv6($ip) { |
||
| 190 | } |
||
| 191 |