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 |
||
16 | class Pattern extends AbstractRange |
||
17 | { |
||
18 | /** |
||
19 | * Starting address of the range. |
||
20 | * |
||
21 | * @var \IPLib\Address\AddressInterface |
||
22 | */ |
||
23 | protected $fromAddress; |
||
24 | |||
25 | /** |
||
26 | * Final address of the range. |
||
27 | * |
||
28 | * @var \IPLib\Address\AddressInterface |
||
29 | */ |
||
30 | protected $toAddress; |
||
31 | |||
32 | /** |
||
33 | * Number of ending asterisks. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $asterisksCount; |
||
38 | |||
39 | /** |
||
40 | * The type of the range of this IP range. |
||
41 | * |
||
42 | * @var int|false|null false if this range crosses multiple range types, null if yet to be determined |
||
43 | */ |
||
44 | protected $rangeType; |
||
45 | |||
46 | /** |
||
47 | * Initializes the instance. |
||
48 | * |
||
49 | * @param \IPLib\Address\AddressInterface $fromAddress |
||
50 | * @param \IPLib\Address\AddressInterface $toAddress |
||
51 | * @param int $asterisksCount |
||
52 | */ |
||
53 | 137 | public function __construct(AddressInterface $fromAddress, AddressInterface $toAddress, $asterisksCount) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | * |
||
63 | * @see \IPLib\Range\RangeInterface::__toString() |
||
64 | */ |
||
65 | 66 | public function __toString() |
|
69 | |||
70 | /** |
||
71 | * Try get the range instance starting from its string representation. |
||
72 | * |
||
73 | * @param string|mixed $range |
||
74 | * @param bool $supportNonDecimalIPv4 set to true to support parsing non decimal (that is, octal and hexadecimal) IPv4 addresses |
||
75 | * |
||
76 | * @return static|null |
||
77 | */ |
||
78 | 129 | public static function fromString($range, $supportNonDecimalIPv4 = false) |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | * |
||
128 | * @see \IPLib\Range\RangeInterface::toString() |
||
129 | */ |
||
130 | 112 | public function toString($long = false) |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | * |
||
169 | * @see \IPLib\Range\RangeInterface::getAddressType() |
||
170 | */ |
||
171 | 66 | public function getAddressType() |
|
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | * |
||
179 | * @see \IPLib\Range\RangeInterface::getStartAddress() |
||
180 | */ |
||
181 | 30 | public function getStartAddress() |
|
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | * |
||
189 | * @see \IPLib\Range\RangeInterface::getEndAddress() |
||
190 | */ |
||
191 | 30 | public function getEndAddress() |
|
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | * |
||
199 | * @see \IPLib\Range\RangeInterface::getComparableStartString() |
||
200 | */ |
||
201 | 34 | public function getComparableStartString() |
|
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | * |
||
209 | * @see \IPLib\Range\RangeInterface::getComparableEndString() |
||
210 | */ |
||
211 | 34 | public function getComparableEndString() |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | * |
||
219 | * @see \IPLib\Range\RangeInterface::asSubnet() |
||
220 | */ |
||
221 | 26 | public function asSubnet() |
|
222 | { |
||
223 | 26 | return new Subnet($this->getStartAddress(), $this->getEndAddress(), $this->getNetworkPrefix()); |
|
224 | } |
||
225 | 9 | ||
226 | /** |
||
227 | 17 | * {@inheritdoc} |
|
228 | * |
||
229 | * @see \IPLib\Range\RangeInterface::asPattern() |
||
230 | */ |
||
231 | public function asPattern() |
||
232 | { |
||
233 | return $this; |
||
234 | } |
||
235 | |||
236 | 14 | /** |
|
237 | * {@inheritdoc} |
||
238 | 14 | * |
|
239 | * @see \IPLib\Range\RangeInterface::getSubnetMask() |
||
240 | */ |
||
241 | public function getSubnetMask() |
||
242 | { |
||
243 | if ($this->getAddressType() !== AddressType::T_IPv4) { |
||
244 | return null; |
||
245 | } |
||
246 | 5 | switch ($this->asterisksCount) { |
|
247 | case 0: |
||
248 | 5 | $bytes = array(255, 255, 255, 255); |
|
249 | 1 | break; |
|
250 | case 4: |
||
251 | 4 | $bytes = array(0, 0, 0, 0); |
|
252 | 4 | break; |
|
253 | default: |
||
254 | $bytes = array_pad(array_fill(0, 4 - $this->asterisksCount, 255), 4, 0); |
||
255 | 4 | break; |
|
256 | 1 | } |
|
257 | 1 | ||
258 | return IPv4::fromBytes($bytes); |
||
259 | 3 | } |
|
260 | 3 | ||
261 | /** |
||
262 | * {@inheritdoc} |
||
263 | 4 | * |
|
264 | * @see \IPLib\Range\RangeInterface::getReverseDNSLookupName() |
||
265 | */ |
||
266 | public function getReverseDNSLookupName() |
||
267 | { |
||
268 | return $this->asterisksCount === 0 ? array($this->getStartAddress()->getReverseDNSLookupName()) : $this->asSubnet()->getReverseDNSLookupName(); |
||
269 | } |
||
270 | |||
271 | 12 | /** |
|
272 | * {@inheritdoc} |
||
273 | 12 | * |
|
274 | * @see \IPLib\Range\RangeInterface::getSize() |
||
275 | */ |
||
276 | View Code Duplication | public function getSize() |
|
|
|||
277 | { |
||
278 | $fromAddress = $this->fromAddress; |
||
279 | $maxPrefix = $fromAddress::getNumberOfBits(); |
||
280 | $prefix = $this->getNetworkPrefix(); |
||
281 | |||
282 | return pow(2, ($maxPrefix - $prefix)); |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @return float|int |
||
287 | */ |
||
288 | private function getNetworkPrefix() |
||
297 | } |
||
298 |
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.