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 |
||
8 | class Connection implements ConnectionInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var \AMQPConnection |
||
12 | */ |
||
13 | protected $delegate; |
||
14 | |||
15 | /** |
||
16 | * @param \AMQPConnection $delegate |
||
17 | */ |
||
18 | 43 | public function __construct(\AMQPConnection $delegate) |
|
22 | |||
23 | /** |
||
24 | * Closes connection on destruct. |
||
25 | */ |
||
26 | 41 | public function __destruct() |
|
30 | |||
31 | /** |
||
32 | * @inheritdoc |
||
33 | */ |
||
34 | 2 | public function getHost() |
|
38 | |||
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | 2 | public function getLogin() |
|
46 | |||
47 | /** |
||
48 | * @inheritdoc |
||
49 | */ |
||
50 | 2 | public function getPassword() |
|
54 | |||
55 | /** |
||
56 | * @inheritdoc |
||
57 | */ |
||
58 | 2 | public function getPort() |
|
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | 2 | public function getVhost() |
|
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | 2 | View Code Duplication | public function setHost($host) |
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | */ |
||
86 | 2 | public function setLogin($login) |
|
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | 2 | public function setPassword($password) |
|
106 | |||
107 | /** |
||
108 | * @inheritdoc |
||
109 | */ |
||
110 | 2 | View Code Duplication | public function setPort($port) |
118 | |||
119 | /** |
||
120 | * @inheritdoc |
||
121 | */ |
||
122 | 2 | public function setVhost($vhost) |
|
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 30 | public function connect() |
|
142 | |||
143 | /** |
||
144 | * @inheritdoc |
||
145 | */ |
||
146 | 42 | public function close() |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 29 | public function isConnected() |
|
158 | |||
159 | /** |
||
160 | * @inheritdoc |
||
161 | */ |
||
162 | 1 | public function reconnect() |
|
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | * |
||
170 | * @return \AMQPConnection |
||
171 | */ |
||
172 | 27 | public function getDelegate() |
|
176 | } |
||
177 |
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.