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 |
||
20 | class Hooks extends AbstractPackage |
||
21 | { |
||
22 | /** |
||
23 | * Create a hook. |
||
24 | * |
||
25 | * @param string $user The name of the owner of the GitHub repository. |
||
26 | * @param string $repo The name of the GitHub repository. |
||
27 | * @param string $name The name of the service being called. |
||
28 | * @param array $config Array containing the config for the service. |
||
29 | * @param array $events The events the hook will be triggered for. |
||
30 | * @param boolean $active Flag to determine if the hook is active |
||
31 | * |
||
32 | * @return object |
||
33 | * |
||
34 | * @since 1.0 |
||
35 | * @throws \DomainException |
||
36 | * @throws \RuntimeException |
||
37 | */ |
||
38 | public function create($user, $repo, $name, $config, array $events = array('push'), $active = true) |
||
61 | |||
62 | /** |
||
63 | * Delete a hook |
||
64 | * |
||
65 | * @param string $user The name of the owner of the GitHub repository. |
||
66 | * @param string $repo The name of the GitHub repository. |
||
67 | * @param integer $id ID of the hook to delete. |
||
68 | * |
||
69 | * @return object |
||
70 | * |
||
71 | * @since 1.0 |
||
72 | * @throws \DomainException |
||
73 | */ |
||
74 | View Code Duplication | public function delete($user, $repo, $id) |
|
84 | |||
85 | /** |
||
86 | * Edit a hook. |
||
87 | * |
||
88 | * @param string $user The name of the owner of the GitHub repository. |
||
89 | * @param string $repo The name of the GitHub repository. |
||
90 | * @param integer $id ID of the hook to edit. |
||
91 | * @param string $name The name of the service being called. |
||
92 | * @param array $config Array containing the config for the service. |
||
93 | * @param array $events The events the hook will be triggered for. This resets the currently set list |
||
94 | * @param array $addEvents Events to add to the hook. |
||
95 | * @param array $removeEvents Events to remove from the hook. |
||
96 | * @param boolean $active Flag to determine if the hook is active |
||
97 | * |
||
98 | * @return object |
||
99 | * |
||
100 | * @since 1.0 |
||
101 | * @throws \DomainException |
||
102 | * @throws \RuntimeException |
||
103 | */ |
||
104 | public function edit($user, $repo, $id, $name, $config, array $events = array('push'), array $addEvents = array(), |
||
149 | |||
150 | /** |
||
151 | * Get single hook. |
||
152 | * |
||
153 | * @param string $user The name of the owner of the GitHub repository. |
||
154 | * @param string $repo The name of the GitHub repository. |
||
155 | * @param integer $id ID of the hook to retrieve |
||
156 | * |
||
157 | * @return object |
||
158 | * |
||
159 | * @since 1.0 |
||
160 | * @throws \DomainException |
||
161 | */ |
||
162 | public function get($user, $repo, $id) |
||
171 | |||
172 | /** |
||
173 | * List hooks. |
||
174 | * |
||
175 | * @param string $user The name of the owner of the GitHub repository. |
||
176 | * @param string $repo The name of the GitHub repository. |
||
177 | * |
||
178 | * @return object |
||
179 | * |
||
180 | * @since 1.0 |
||
181 | * @throws \DomainException |
||
182 | */ |
||
183 | public function getList($user, $repo) |
||
192 | |||
193 | /** |
||
194 | * Ping a hook. |
||
195 | * |
||
196 | * @param string $user The name of the owner of the GitHub repository. |
||
197 | * @param string $repo The name of the GitHub repository. |
||
198 | * @param integer $id ID of the hook to ping |
||
199 | * |
||
200 | * @return object |
||
201 | * |
||
202 | * @since 1.4.0 |
||
203 | * @throws \DomainException |
||
204 | */ |
||
205 | public function ping($user, $repo, $id) |
||
215 | |||
216 | /** |
||
217 | * Test a `push` hook. |
||
218 | * |
||
219 | * @param string $user The name of the owner of the GitHub repository. |
||
220 | * @param string $repo The name of the GitHub repository. |
||
221 | * @param integer $id ID of the hook to test |
||
222 | * |
||
223 | * @return object |
||
224 | * |
||
225 | * @since 1.0 |
||
226 | * @throws \DomainException |
||
227 | */ |
||
228 | public function test($user, $repo, $id) |
||
238 | } |
||
239 |
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.