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 MailingList extends HttpApi |
||
21 | { |
||
22 | /** |
||
23 | * @return Member |
||
24 | */ |
||
25 | public function member() |
||
29 | |||
30 | /** |
||
31 | * Returns a paginated list of mailing lists on the domain. |
||
32 | * |
||
33 | * @param int $limit Maximum number of records to return (optional: 100 by default) |
||
34 | * |
||
35 | * @return PagesResponse |
||
36 | * |
||
37 | * @throws \Exception |
||
38 | */ |
||
39 | 2 | View Code Duplication | public function pages($limit = 100) |
52 | |||
53 | /** |
||
54 | * Creates a new mailing list on the current domain. |
||
55 | * |
||
56 | * @param string $address Address for the new mailing list |
||
57 | * @param string $name Name for the new mailing list (optional) |
||
58 | * @param string $description Description for the new mailing list (optional) |
||
59 | * @param string $accessLevel List access level, one of: readonly (default), members, everyone |
||
60 | * |
||
61 | * @return CreateResponse |
||
62 | * |
||
63 | * @throws \Exception |
||
64 | */ |
||
65 | 3 | public function create($address, $name = null, $description = null, $accessLevel = 'readonly') |
|
83 | |||
84 | /** |
||
85 | * Returns a single mailing list. |
||
86 | * |
||
87 | * @param string $address Address of the mailing list |
||
88 | * |
||
89 | * @return ShowResponse |
||
90 | * |
||
91 | * @throws \Exception |
||
92 | */ |
||
93 | 2 | public function show($address) |
|
101 | |||
102 | /** |
||
103 | * Updates a mailing list. |
||
104 | * |
||
105 | * @param string $address Address of the mailing list |
||
106 | * @param array $parameters Array of field => value pairs to update |
||
107 | * |
||
108 | * @return UpdateResponse |
||
109 | * |
||
110 | * @throws \Exception |
||
111 | */ |
||
112 | 2 | public function update($address, $parameters = []) |
|
136 | |||
137 | /** |
||
138 | * Removes a mailing list from the domain. |
||
139 | * |
||
140 | * @param string $address Address of the mailing list |
||
141 | * |
||
142 | * @return DeleteResponse |
||
143 | * |
||
144 | * @throws \Exception |
||
145 | */ |
||
146 | 1 | public function delete($address) |
|
154 | } |
||
155 |
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.