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 Factory 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 Factory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Factory |
||
13 | { |
||
14 | /** |
||
15 | * Parse an IP address string. |
||
16 | * |
||
17 | * @param string $address the address to parse |
||
18 | * @param bool $mayIncludePort set to false to avoid parsing addresses with ports |
||
19 | * @param bool $mayIncludeZoneID set to false to avoid parsing IPv6 addresses with zone IDs (see RFC 4007) |
||
20 | * @param bool $supportNonDecimalIPv4 set to true to support parsing non decimal (that is, octal and hexadecimal) IPv4 addresses |
||
21 | * |
||
22 | * @return \IPLib\Address\AddressInterface|null |
||
23 | */ |
||
24 | 1167 | View Code Duplication | public static function addressFromString($address, $mayIncludePort = true, $mayIncludeZoneID = true, $supportNonDecimalIPv4 = false) |
36 | |||
37 | /** |
||
38 | * Convert a byte array to an address instance. |
||
39 | * |
||
40 | * @param int[]|array $bytes |
||
41 | * |
||
42 | * @return \IPLib\Address\AddressInterface|null |
||
43 | */ |
||
44 | 561 | public static function addressFromBytes(array $bytes) |
|
56 | |||
57 | /** |
||
58 | * Parse an IP range string. |
||
59 | * |
||
60 | * @param string $range |
||
61 | * @param bool $supportNonDecimalIPv4 set to true to support parsing non decimal (that is, octal and hexadecimal) IPv4 addresses |
||
62 | * |
||
63 | * @return \IPLib\Range\RangeInterface|null |
||
64 | */ |
||
65 | 639 | View Code Duplication | public static function rangeFromString($range, $supportNonDecimalIPv4 = false) |
80 | |||
81 | /** |
||
82 | * Create the smallest address range that comprises two addresses. |
||
83 | * |
||
84 | * @param string|\IPLib\Address\AddressInterface $from |
||
85 | * @param string|\IPLib\Address\AddressInterface $to |
||
86 | * @param bool $supportNonDecimalIPv4 set to true to support parsing non decimal (that is, octal and hexadecimal) IPv4 addresses |
||
87 | * |
||
88 | * @return \IPLib\Range\RangeInterface|null return NULL if $from and/or $to are invalid addresses, or if both are NULL or empty strings, or if they are addresses of different types |
||
89 | */ |
||
90 | 42 | public static function rangeFromBoundaries($from, $to, $supportNonDecimalIPv4 = false) |
|
96 | |||
97 | /** |
||
98 | * Create a list of Range instances that exactly describes all the addresses between the two provided addresses. |
||
99 | * |
||
100 | * @param string|\IPLib\Address\AddressInterface $from |
||
101 | * @param string|\IPLib\Address\AddressInterface $to |
||
102 | * @param bool $supportNonDecimalIPv4 set to true to support parsing non decimal (that is, octal and hexadecimal) IPv4 addresses |
||
103 | * |
||
104 | * @return \IPLib\Range\Subnet[]|null return NULL if $from and/or $to are invalid addresses, or if both are NULL or empty strings, or if they are addresses of different types |
||
105 | */ |
||
106 | 33 | public static function rangesFromBoundaries($from, $to, $supportNonDecimalIPv4 = false) |
|
125 | |||
126 | /** |
||
127 | * @param \IPLib\Address\AddressInterface $from |
||
128 | * @param \IPLib\Address\AddressInterface $to |
||
129 | * |
||
130 | * @return \IPLib\Range\RangeInterface|null |
||
131 | */ |
||
132 | 39 | protected static function rangeFromBoundaryAddresses(AddressInterface $from = null, AddressInterface $to = null) |
|
173 | |||
174 | /** |
||
175 | * @param string|\IPLib\Address\AddressInterface $from |
||
176 | * @param string|\IPLib\Address\AddressInterface $to |
||
177 | * @param bool $supportNonDecimalIPv4 |
||
178 | * |
||
179 | * @return \IPLib\Address\AddressInterface[]|null[]|false[] |
||
180 | */ |
||
181 | 75 | private static function parseBoundaries($from, $to, $supportNonDecimalIPv4 = false) |
|
205 | } |
||
206 |
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.