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 | View Code Duplication | class IPv6NetworkComparator |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * @var IPv6Network |
||
20 | */ |
||
21 | private $network; |
||
22 | |||
23 | /** |
||
24 | * @param IPv6Network $network |
||
25 | */ |
||
26 | 17 | public function __construct(IPv6Network $network) |
|
30 | |||
31 | /** |
||
32 | * Checks if this network is equal to the specified network. |
||
33 | * |
||
34 | * @param IPv6Network $interval |
||
35 | * |
||
36 | * @return bool |
||
37 | */ |
||
38 | public function equal(IPv6Network $interval) |
||
45 | |||
46 | /** |
||
47 | * Does this network contain the specified IP. |
||
48 | * |
||
49 | * @param IPv6NetworkPoint $point |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | 2 | public function contains(IPv6NetworkPoint $point) |
|
60 | |||
61 | /** |
||
62 | * Does this network intersect the specified network. |
||
63 | * |
||
64 | * @param IPv6Network $network |
||
65 | * |
||
66 | * @return bool |
||
67 | */ |
||
68 | 3 | public function intersects(IPv6Network $network) |
|
79 | |||
80 | /** |
||
81 | * Does this network abut with the network specified. |
||
82 | * |
||
83 | * @param IPv6Network $network |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function abuts(IPv6Network $network) |
||
94 | } |
||
95 |
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.