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 |
||
11 | abstract class AbstractRange implements RangeInterface |
||
12 | { |
||
13 | /** |
||
14 | * {@inheritdoc} |
||
15 | * |
||
16 | * @see \IPLib\Range\RangeInterface::getRangeType() |
||
17 | */ |
||
18 | 242 | public function getRangeType() |
|
19 | { |
||
20 | 242 | if ($this->rangeType === null) { |
|
21 | 242 | $addressType = $this->getAddressType(); |
|
22 | 242 | if ($addressType === AddressType::T_IPv6 && Subnet::get6to4()->containsRange($this)) { |
|
23 | 9 | $this->rangeType = Factory::rangeFromBoundaries($this->fromAddress->toIPv4(), $this->toAddress->toIPv4())->getRangeType(); |
|
24 | } else { |
||
25 | 233 | switch ($addressType) { |
|
26 | case AddressType::T_IPv4: |
||
27 | 170 | $defaultType = IPv4::getDefaultReservedRangeType(); |
|
28 | 170 | $reservedRanges = IPv4::getReservedRanges(); |
|
29 | 170 | break; |
|
30 | case AddressType::T_IPv6: |
||
31 | 63 | $defaultType = IPv6::getDefaultReservedRangeType(); |
|
32 | 63 | $reservedRanges = IPv6::getReservedRanges(); |
|
33 | 63 | break; |
|
34 | default: |
||
35 | throw new \Exception('@todo'); // @codeCoverageIgnore |
||
36 | } |
||
37 | 233 | $rangeType = null; |
|
38 | 233 | foreach ($reservedRanges as $reservedRange) { |
|
39 | 233 | $rangeType = $reservedRange->getRangeType($this); |
|
40 | 233 | if ($rangeType !== null) { |
|
41 | 198 | break; |
|
42 | } |
||
43 | } |
||
44 | 233 | $this->rangeType = $rangeType === null ? $defaultType : $rangeType; |
|
45 | } |
||
46 | } |
||
47 | |||
48 | 242 | return $this->rangeType === false ? null : $this->rangeType; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | * |
||
54 | * @see \IPLib\Range\RangeInterface::getAddressAtOffset() |
||
55 | */ |
||
56 | 15 | public function getAddressAtOffset($n) |
|
57 | { |
||
58 | 15 | if (!is_int($n)) { |
|
59 | 2 | return null; |
|
60 | } |
||
61 | |||
62 | 13 | $address = null; |
|
63 | 13 | if ($n >= 0) { |
|
64 | 8 | $start = Factory::addressFromString($this->getComparableStartString()); |
|
65 | 8 | $address = $start->getAddressAtOffset($n); |
|
66 | } else { |
||
67 | 5 | $end = Factory::addressFromString($this->getComparableEndString()); |
|
68 | 5 | $address = $end->getAddressAtOffset($n + 1); |
|
69 | } |
||
70 | |||
71 | 13 | if ($address === null) { |
|
72 | 1 | return null; |
|
73 | } |
||
74 | |||
75 | 12 | return $this->contains($address) ? $address : null; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | * |
||
81 | * @see \IPLib\Range\RangeInterface::contains() |
||
82 | */ |
||
83 | 253 | View Code Duplication | public function contains(AddressInterface $address) |
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | * |
||
103 | * @see \IPLib\Range\RangeInterface::containsRange() |
||
104 | */ |
||
105 | 79 | View Code Duplication | public function containsRange(RangeInterface $range) |
122 | } |
||
123 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: