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 Single extends AbstractRange |
||
17 | { |
||
18 | /** |
||
19 | * @var \IPLib\Address\AddressInterface |
||
20 | */ |
||
21 | protected $address; |
||
22 | |||
23 | /** |
||
24 | * Initializes the instance. |
||
25 | * |
||
26 | * @param \IPLib\Address\AddressInterface $address |
||
27 | */ |
||
28 | 102 | protected function __construct(AddressInterface $address) |
|
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | * |
||
36 | * @see \IPLib\Range\RangeInterface::__toString() |
||
37 | */ |
||
38 | 11 | public function __toString() |
|
39 | { |
||
40 | 11 | return $this->address->__toString(); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Try get the range instance starting from its string representation. |
||
45 | * |
||
46 | * @param string|mixed $range |
||
47 | * @param bool $supportNonDecimalIPv4 set to true to support parsing non decimal (that is, octal and hexadecimal) IPv4 addresses |
||
48 | * |
||
49 | * @return static|null |
||
50 | */ |
||
51 | 47 | public static function fromString($range, $supportNonDecimalIPv4 = false) |
|
52 | { |
||
53 | 47 | $result = null; |
|
54 | 47 | $address = Factory::addressFromString($range, true, true, $supportNonDecimalIPv4); |
|
55 | 47 | if ($address !== null) { |
|
56 | 33 | $result = new static($address); |
|
57 | } |
||
58 | |||
59 | 47 | return $result; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * Create the range instance starting from an address instance. |
||
64 | * |
||
65 | * @param \IPLib\Address\AddressInterface $address |
||
66 | * |
||
67 | * @return static |
||
68 | */ |
||
69 | 69 | public static function fromAddress(AddressInterface $address) |
|
70 | { |
||
71 | 69 | return new static($address); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | * |
||
77 | * @see \IPLib\Range\RangeInterface::toString() |
||
78 | */ |
||
79 | 19 | public function toString($long = false) |
|
80 | { |
||
81 | 19 | return $this->address->toString($long); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | * |
||
87 | * @see \IPLib\Range\RangeInterface::getAddressType() |
||
88 | */ |
||
89 | 19 | public function getAddressType() |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | * |
||
97 | * @see \IPLib\Range\RangeInterface::getRangeType() |
||
98 | */ |
||
99 | 9 | public function getRangeType() |
|
103 | |||
104 | /** |
||
105 | * {@inheritdoc} |
||
106 | * |
||
107 | * @see \IPLib\Range\RangeInterface::contains() |
||
108 | */ |
||
109 | 7 | View Code Duplication | public function contains(AddressInterface $address) |
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | * |
||
124 | * @see \IPLib\Range\RangeInterface::containsRange() |
||
125 | */ |
||
126 | 9 | View Code Duplication | public function containsRange(RangeInterface $range) |
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | * |
||
141 | * @see \IPLib\Range\RangeInterface::getStartAddress() |
||
142 | */ |
||
143 | 60 | public function getStartAddress() |
|
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | * |
||
151 | * @see \IPLib\Range\RangeInterface::getEndAddress() |
||
152 | */ |
||
153 | 1 | public function getEndAddress() |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | * |
||
161 | * @see \IPLib\Range\RangeInterface::getComparableStartString() |
||
162 | */ |
||
163 | 8 | public function getComparableStartString() |
|
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | * |
||
171 | * @see \IPLib\Range\RangeInterface::getComparableEndString() |
||
172 | */ |
||
173 | 8 | public function getComparableEndString() |
|
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | * |
||
181 | * @see \IPLib\Range\RangeInterface::asSubnet() |
||
182 | */ |
||
183 | 56 | public function asSubnet() |
|
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | * |
||
196 | * @see \IPLib\Range\RangeInterface::asPattern() |
||
197 | */ |
||
198 | 56 | public function asPattern() |
|
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | * |
||
206 | * @see \IPLib\Range\RangeInterface::getSubnetMask() |
||
207 | */ |
||
208 | 2 | public function getSubnetMask() |
|
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | * |
||
220 | * @see \IPLib\Range\RangeInterface::getReverseDNSLookupName() |
||
221 | */ |
||
222 | 3 | public function getReverseDNSLookupName() |
|
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | * |
||
230 | * @see \IPLib\Range\RangeInterface::getSize() |
||
231 | */ |
||
232 | public function getSize() |
||
236 | } |
||
237 |
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.