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 ConnectionOpen extends Frame |
|
|
|||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $virtualHost; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $reserved1; |
||
27 | |||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $reserved2; |
||
32 | |||
33 | /** |
||
34 | * @param int $channel |
||
35 | * @param string $virtualHost |
||
36 | * @param string $reserved1 |
||
37 | * @param bool $reserved2 |
||
38 | */ |
||
39 | public function __construct($channel, $virtualHost, $reserved1, $reserved2) |
||
47 | |||
48 | /** |
||
49 | * Virtual host name. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public function getVirtualHost() |
||
57 | |||
58 | /** |
||
59 | * Reserved1. |
||
60 | * |
||
61 | * @return string |
||
62 | */ |
||
63 | public function getReserved1() |
||
67 | |||
68 | /** |
||
69 | * Reserved2. |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | public function isReserved2() |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | public function encode() |
||
90 | } |
||
91 |
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.