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 | class Queue |
||
15 | { |
||
16 | private $client; |
||
17 | private $tubeName; |
||
18 | private $prefix; |
||
19 | |||
20 | 157 | public function __construct(\Tarantool $client, $tubeName) |
|
26 | |||
27 | /** |
||
28 | * @param mixed $data |
||
29 | * @param array|null $options |
||
30 | * |
||
31 | * @return Task |
||
32 | */ |
||
33 | 34 | View Code Duplication | public function put($data, array $options = null) |
40 | |||
41 | /** |
||
42 | * @param int|float|null $timeout |
||
43 | * |
||
44 | * @return Task|null |
||
45 | */ |
||
46 | 20 | public function take($timeout = null) |
|
53 | |||
54 | /** |
||
55 | * @param int $taskId |
||
56 | * |
||
57 | * @return Task |
||
58 | */ |
||
59 | 9 | public function ack($taskId) |
|
65 | |||
66 | /** |
||
67 | * @param int $taskId |
||
68 | * @param array|null $options |
||
69 | * |
||
70 | * @return Task |
||
71 | */ |
||
72 | 12 | View Code Duplication | public function release($taskId, array $options = null) |
79 | |||
80 | /** |
||
81 | * @param int $taskId |
||
82 | * |
||
83 | * @return Task |
||
84 | */ |
||
85 | 13 | public function peek($taskId) |
|
91 | |||
92 | /** |
||
93 | * @param int $taskId |
||
94 | * |
||
95 | * @return Task |
||
96 | */ |
||
97 | 9 | public function bury($taskId) |
|
103 | |||
104 | /** |
||
105 | * @param int $count |
||
106 | * |
||
107 | * @return int |
||
108 | */ |
||
109 | 13 | public function kick($count) |
|
115 | |||
116 | /** |
||
117 | * @param int $taskId |
||
118 | * |
||
119 | * @return Task |
||
120 | */ |
||
121 | 11 | public function delete($taskId) |
|
127 | |||
128 | 9 | public function truncate() |
|
132 | |||
133 | /** |
||
134 | * @param string|null $path |
||
135 | * |
||
136 | * @return array|int |
||
137 | * |
||
138 | * @throws \InvalidArgumentException |
||
139 | */ |
||
140 | 43 | public function stats($path = null) |
|
158 | } |
||
159 |
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.