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:
Complex classes like Util 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 Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Util |
||
9 | { |
||
10 | /** |
||
11 | * This class should not be instantiated. |
||
12 | */ |
||
13 | private function __construct() |
||
16 | |||
17 | /* Convert IP address to long int, truncated to 32-bits to avoid sign extension |
||
18 | on 64-bit platforms. */ |
||
19 | public static function ip2long32($ip) |
||
23 | |||
24 | /* Convert IP address to unsigned long int. */ |
||
25 | public static function ip2ulong($ip) |
||
29 | |||
30 | /* Convert long int to IP address, truncating to 32-bits. */ |
||
31 | public static function long2ip32($ip) |
||
35 | |||
36 | /* returns true if $ipaddr is a valid dotted IPv4 address */ |
||
37 | public static function is_ipaddr($ipaddr) |
||
48 | |||
49 | /* Return true if the first IP is 'before' the second */ |
||
50 | public static function ip_less_than($ip1, $ip2) |
||
56 | |||
57 | /* Return true if the first IP is 'after' the second */ |
||
58 | public static function ip_greater_than($ip1, $ip2) |
||
64 | |||
65 | /* Return the next IP address after the given address */ |
||
66 | public static function ip_after($ip, $increment = 1) |
||
70 | |||
71 | /* Return the next IP address after the given address */ |
||
72 | public static function ip_before($ip, $decrement = 1) |
||
76 | |||
77 | public static function ip_diff($ip1, $ip2) |
||
81 | |||
82 | public static function cidr_contains($cidr, $ip) |
||
88 | |||
89 | /* Find the smallest possible subnet mask which can contain a given number of IPs |
||
90 | * e.g. 512 IPs can fit in a /23, but 513 IPs need a /22 |
||
91 | */ |
||
92 | public static function find_smallest_cidr($number) |
||
100 | |||
101 | /* Find out how many IPs are contained within a given IP range |
||
102 | * e.g. 192.168.0.0 to 192.168.0.255 returns 256 |
||
103 | */ |
||
104 | public static function ip_range_size($startip, $endip) |
||
113 | |||
114 | |||
115 | public static function get_single_subnet($startip, $endip) |
||
132 | |||
133 | public static function find_smallest_common_IP($startip, $endip) |
||
172 | |||
173 | public static function subnet_range_size($subnetmask) |
||
177 | |||
178 | public static function cidr_to_ips_array($cidr) |
||
196 | |||
197 | /* return the subnet address given a host address and a subnet bit count */ |
||
198 | View Code Duplication | public static function gen_subnet($ipaddr, $bits) |
|
206 | |||
207 | /* returns a subnet mask (long given a bit count) */ |
||
208 | public static function gen_subnet_mask_long($bits) |
||
217 | |||
218 | /* return the highest (broadcast) address in the subnet given a host address and |
||
219 | a subnet bit count */ |
||
220 | View Code Duplication | public static function gen_subnet_max($ipaddr, $bits) |
|
228 | |||
229 | /* takes an array of ip address, sorts and returns as an array */ |
||
230 | public static function sort_addresses($ipaddr) |
||
244 | |||
245 | /* takes an array of ip address, sorts and returns as an array */ |
||
246 | public static function sort_cidrs($cidrs) |
||
262 | } |
||
263 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.