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 |
||
17 | class Subnet extends AbstractRange |
||
18 | { |
||
19 | /** |
||
20 | * Starting address of the range. |
||
21 | * |
||
22 | * @var \IPLib\Address\AddressInterface |
||
23 | */ |
||
24 | protected $fromAddress; |
||
25 | |||
26 | /** |
||
27 | * Final address of the range. |
||
28 | * |
||
29 | * @var \IPLib\Address\AddressInterface |
||
30 | */ |
||
31 | protected $toAddress; |
||
32 | |||
33 | /** |
||
34 | * Number of the same bits of the range. |
||
35 | * |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $networkPrefix; |
||
39 | |||
40 | /** |
||
41 | * The type of the range of this IP range. |
||
42 | * |
||
43 | * @var int|null |
||
44 | */ |
||
45 | protected $rangeType; |
||
46 | |||
47 | /** |
||
48 | * The 6to4 address IPv6 address range. |
||
49 | * |
||
50 | * @var self|null |
||
51 | */ |
||
52 | private static $sixToFour; |
||
53 | |||
54 | /** |
||
55 | * Initializes the instance. |
||
56 | * |
||
57 | * @param \IPLib\Address\AddressInterface $fromAddress |
||
58 | * @param \IPLib\Address\AddressInterface $toAddress |
||
59 | * @param int $networkPrefix |
||
60 | * |
||
61 | * @internal |
||
62 | 512 | */ |
|
63 | public function __construct(AddressInterface $fromAddress, AddressInterface $toAddress, $networkPrefix) |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | * |
||
73 | * @see \IPLib\Range\RangeInterface::__toString() |
||
74 | 44 | */ |
|
75 | public function __toString() |
||
79 | |||
80 | /** |
||
81 | * Try get the range instance starting from its string representation. |
||
82 | * |
||
83 | * @param string|mixed $range |
||
84 | * @param bool $supportNonDecimalIPv4 set to true to support parsing non decimal (that is, octal and hexadecimal) IPv4 addresses |
||
85 | * |
||
86 | * @return static|null |
||
87 | 625 | */ |
|
88 | public static function fromString($range, $supportNonDecimalIPv4 = false) |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | * |
||
132 | * @see \IPLib\Range\RangeInterface::toString() |
||
133 | 279 | */ |
|
134 | public function toString($long = false) |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | * |
||
142 | * @see \IPLib\Range\RangeInterface::getAddressType() |
||
143 | 472 | */ |
|
144 | public function getAddressType() |
||
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | * |
||
152 | * @see \IPLib\Range\RangeInterface::getStartAddress() |
||
153 | 61 | */ |
|
154 | public function getStartAddress() |
||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | * |
||
162 | * @see \IPLib\Range\RangeInterface::getEndAddress() |
||
163 | 5 | */ |
|
164 | public function getEndAddress() |
||
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | * |
||
172 | * @see \IPLib\Range\RangeInterface::getComparableStartString() |
||
173 | 467 | */ |
|
174 | public function getComparableStartString() |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | * |
||
182 | * @see \IPLib\Range\RangeInterface::getComparableEndString() |
||
183 | 467 | */ |
|
184 | public function getComparableEndString() |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | * |
||
192 | * @see \IPLib\Range\RangeInterface::asSubnet() |
||
193 | 72 | */ |
|
194 | public function asSubnet() |
||
198 | |||
199 | 72 | /** |
|
200 | * Get the pattern (asterisk) representation (if applicable) of this range. |
||
201 | * |
||
202 | * @return \IPLib\Range\Pattern|null return NULL if this range can't be represented by a pattern notation |
||
203 | */ |
||
204 | public function asPattern() |
||
215 | |||
216 | /** |
||
217 | 56 | * Get the 6to4 address IPv6 address range. |
|
218 | * |
||
219 | 56 | * @return self |
|
220 | 56 | */ |
|
221 | 56 | public static function get6to4() |
|
229 | |||
230 | /** |
||
231 | * Get subnet prefix. |
||
232 | * |
||
233 | * @return int |
||
234 | 19 | */ |
|
235 | public function getNetworkPrefix() |
||
239 | 18 | ||
240 | 18 | /** |
|
241 | 18 | * {@inheritdoc} |
|
242 | 10 | * |
|
243 | 10 | * @see \IPLib\Range\RangeInterface::getSubnetMask() |
|
244 | */ |
||
245 | 18 | public function getSubnetMask() |
|
263 | |||
264 | /** |
||
265 | * {@inheritdoc} |
||
266 | * |
||
267 | * @see \IPLib\Range\RangeInterface::getReverseDNSLookupName() |
||
268 | */ |
||
269 | public function getReverseDNSLookupName() |
||
305 | } |
||
306 |
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.