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 |
||
15 | class IPv4NetworkMask |
||
16 | { |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $ip = ''; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | private $long = 0; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | private $cidr = 0; |
||
31 | |||
32 | /** |
||
33 | * @param string $ip |
||
34 | * @param int $cidr |
||
35 | */ |
||
36 | 27 | private function __construct($ip, $cidr) |
|
42 | |||
43 | /** |
||
44 | * @param string $ip |
||
45 | * |
||
46 | * @return self |
||
47 | */ |
||
48 | 3 | public static function fromIP($ip) |
|
69 | |||
70 | /** |
||
71 | * @param int $cidr |
||
72 | * |
||
73 | * @return self |
||
74 | */ |
||
75 | 24 | public static function fromCIDR($cidr) |
|
83 | |||
84 | /** |
||
85 | * @return int |
||
86 | */ |
||
87 | 6 | public function ip() |
|
91 | |||
92 | /** |
||
93 | * @return int |
||
94 | */ |
||
95 | 27 | public function cidr() |
|
99 | |||
100 | /** |
||
101 | * @param IPv4NetworkMask $mask |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function equal(IPv4NetworkMask $mask) |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | 16 | public function __toString() |
|
117 | } |
||
118 |
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.