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 ZeroMqClient implements Client |
||
15 | { |
||
16 | /** |
||
17 | * @var Location |
||
18 | */ |
||
19 | private $location; |
||
20 | |||
21 | /** |
||
22 | * @var ZMQSocket |
||
23 | */ |
||
24 | private $socket; |
||
25 | |||
26 | /** |
||
27 | * @param Location $location |
||
28 | */ |
||
29 | 2 | public function __construct(Location $location) |
|
33 | |||
34 | /** |
||
35 | * @param string $name |
||
36 | * @param array $parameters |
||
37 | */ |
||
38 | 1 | public function emit($name, array $parameters = []) |
|
46 | |||
47 | /** |
||
48 | * @param string $name |
||
49 | * @param array $parameters |
||
50 | * |
||
51 | * @return Event |
||
52 | */ |
||
53 | 1 | private function newEvent($name, array $parameters = []) |
|
57 | |||
58 | /** |
||
59 | * @return ZMQSocket |
||
60 | */ |
||
61 | 1 | View Code Duplication | private function getSocket() |
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | 1 | public function serialize() |
|
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | * |
||
89 | * @param string $serialized |
||
90 | */ |
||
91 | 1 | public function unserialize($serialized) |
|
95 | |||
96 | /** |
||
97 | * @return Location |
||
98 | */ |
||
99 | public function getLocation() |
||
103 | |||
104 | /** |
||
105 | * @inheritdoc |
||
106 | */ |
||
107 | 1 | View Code Duplication | public function disconnect() |
120 | |||
121 | 1 | public function __destruct() |
|
125 | } |
||
126 |
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.