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 IPv4Network implements IntervalInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | const REGEXP = '/^ |
||
22 | \s* |
||
23 | (?<ip>'.IPv4NetworkPoint::IPV4_ADDR_COMPACT.') # IP supported compact format |
||
24 | \/ # separator |
||
25 | (?<cidr>'.IPv4NetworkMask::CIDR.') # CIDR bit |
||
26 | \s* |
||
27 | $/x'; |
||
28 | |||
29 | /** |
||
30 | * @var IPv4NetworkPoint |
||
31 | */ |
||
32 | private $start; |
||
33 | |||
34 | /** |
||
35 | * @var IPv4NetworkPoint |
||
36 | */ |
||
37 | private $end; |
||
38 | |||
39 | /** |
||
40 | * @var IPv4NetworkMask |
||
41 | */ |
||
42 | private $mask; |
||
43 | |||
44 | /** |
||
45 | * @var IPv4NetworkComparator |
||
46 | */ |
||
47 | private $comparator; |
||
48 | |||
49 | /** |
||
50 | * @param IPv4NetworkPoint $ip |
||
51 | * @param IPv4NetworkMask $mask |
||
52 | */ |
||
53 | 29 | private function __construct(IPv4NetworkPoint $ip, IPv4NetworkMask $mask) |
|
54 | { |
||
55 | 29 | $this->start = $ip; |
|
56 | 29 | $this->mask = $mask; |
|
57 | 29 | $this->end = new IPv4NetworkPoint(long2ip( |
|
58 | 29 | $ip->value() + 2 ** (32 - $mask->cidr()) - 1 |
|
59 | 29 | )); |
|
60 | 29 | $this->comparator = new IPv4NetworkComparator($this); |
|
61 | 29 | } |
|
62 | |||
63 | /** |
||
64 | * @param string $ip |
||
65 | * @param int $cidr |
||
66 | * |
||
67 | * @return IPv4Network |
||
68 | */ |
||
69 | 26 | public static function fromCIDR($ip, $cidr) |
|
70 | { |
||
71 | 26 | return new self(new IPv4NetworkPoint($ip), IPv4NetworkMask::fromCIDR($cidr)); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param string $ip |
||
76 | * @param string $mask |
||
77 | * |
||
78 | * @return IPv4Network |
||
79 | */ |
||
80 | 3 | public static function fromMask($ip, $mask) |
|
81 | { |
||
82 | 3 | return new self(new IPv4NetworkPoint($ip), IPv4NetworkMask::fromIP($mask)); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * Create network from string. |
||
87 | * |
||
88 | * Example formats of network: |
||
89 | * 10.0.0.0/8 |
||
90 | * 172.16.0.0/12 |
||
91 | * 192.168.0.0/16 |
||
92 | * |
||
93 | * Supported compact format: |
||
94 | * 10/8 |
||
95 | * 172.16/12 |
||
96 | * 192.168/16 |
||
97 | * |
||
98 | * Spaces are ignored in format. |
||
99 | * |
||
100 | * @param string $string |
||
101 | * |
||
102 | * @return self |
||
103 | */ |
||
104 | 23 | View Code Duplication | public static function fromString($string) |
|
|||
105 | { |
||
106 | 23 | if (!preg_match(self::REGEXP, $string, $match)) { |
|
107 | throw InvalidIntervalFormatException::create('0.0.0.0/32', $string); |
||
108 | } |
||
109 | |||
110 | // fill IP compact format |
||
111 | 23 | $match['ip'] .= str_repeat('.0', 3 - substr_count($match['ip'], '.')); |
|
112 | |||
113 | 23 | return self::fromCIDR($match['ip'], $match['cidr']); |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Checks if this network is equal to the specified network. |
||
118 | * |
||
119 | * @param IPv4Network $network |
||
120 | * |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function equal(IPv4Network $network) |
||
124 | { |
||
125 | return $this->comparator->equal($network); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Does this network contain the specified IP. |
||
130 | * |
||
131 | * @param string $point |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | 6 | public function contains($point) |
|
136 | { |
||
137 | 6 | return $this->comparator->contains(new IPv4NetworkPoint($point)); |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * Does this network intersect the specified network. |
||
142 | * |
||
143 | * @param IPv4Network $network |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | 3 | public function intersects(IPv4Network $network) |
|
148 | { |
||
149 | 3 | return $this->comparator->intersects($network); |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Does this network abut with the network specified. |
||
154 | * |
||
155 | * @param IPv4Network $network |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | public function abuts(IPv4Network $network) |
||
163 | |||
164 | /** |
||
165 | * @param int $step |
||
166 | * |
||
167 | * @return \Generator |
||
168 | */ |
||
169 | 2 | View Code Duplication | public function iterate($step = 1) |
170 | { |
||
171 | 2 | $end = $this->endPoint()->value(); |
|
172 | 2 | $ip = $this->startPoint()->value(); |
|
173 | |||
174 | 2 | while ($ip <= $end) { |
|
175 | 2 | yield long2ip($ip); |
|
176 | 2 | $ip += $step; |
|
177 | 2 | } |
|
178 | 2 | } |
|
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | 16 | public function start() |
|
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 16 | public function end() |
|
195 | |||
196 | /** |
||
197 | * @return IPv4NetworkMask |
||
198 | */ |
||
199 | 22 | public function mask() |
|
203 | |||
204 | /** |
||
205 | * @return int |
||
206 | */ |
||
207 | 16 | public function cidr() |
|
211 | |||
212 | /** |
||
213 | * @return IPv4NetworkPoint |
||
214 | */ |
||
215 | 14 | public function startPoint() |
|
219 | |||
220 | /** |
||
221 | * @return IPv4NetworkPoint |
||
222 | */ |
||
223 | 8 | public function endPoint() |
|
227 | |||
228 | /** |
||
229 | * @return string |
||
230 | */ |
||
231 | 1 | public function __toString() |
|
235 | } |
||
236 |
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.