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 |
||
14 | final class ZeroMqServer implements Server |
||
15 | { |
||
16 | /** |
||
17 | * @var Location |
||
18 | */ |
||
19 | private $location; |
||
20 | |||
21 | /** |
||
22 | * @var ZMQSocket |
||
23 | */ |
||
24 | private $socket; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $listeners = []; |
||
30 | |||
31 | /** |
||
32 | * @param Location $location |
||
33 | */ |
||
34 | 2 | public function __construct(Location $location) |
|
38 | |||
39 | /** |
||
40 | * @inheritdoc |
||
41 | * |
||
42 | * @param string $name |
||
43 | * @param Closure $closure |
||
44 | * |
||
45 | * @return $this |
||
46 | */ |
||
47 | 1 | public function addListener($name, Closure $closure) |
|
57 | |||
58 | /** |
||
59 | * @inheritdoc |
||
60 | * |
||
61 | * @param string $name |
||
62 | * @param Closure $closure |
||
63 | */ |
||
64 | public function removeListener($name, Closure $closure) |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public function tick() |
||
92 | |||
93 | /** |
||
94 | * @return ZMQSocket |
||
95 | */ |
||
96 | View Code Duplication | private function getSocket() |
|
110 | |||
111 | /** |
||
112 | * @param Event $event |
||
113 | * |
||
114 | * @return $this |
||
115 | */ |
||
116 | private function dispatchEvent(Event $event) |
||
128 | |||
129 | /** |
||
130 | * @inheritdoc |
||
131 | * |
||
132 | * @param string $name |
||
133 | * @param array $parameters |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function emit($name, array $parameters = []) |
||
143 | |||
144 | /** |
||
145 | * @inheritdoc |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | 1 | public function serialize() |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | * |
||
157 | * @param string $serialized |
||
158 | */ |
||
159 | 1 | public function unserialize($serialized) |
|
163 | |||
164 | /** |
||
165 | * @return Location |
||
166 | */ |
||
167 | public function getLocation() |
||
171 | |||
172 | /** |
||
173 | * @inheritdoc |
||
174 | */ |
||
175 | 1 | View Code Duplication | public function disconnect() |
188 | |||
189 | 1 | public function __destruct() |
|
193 | } |
||
194 |
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.