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 |
||
25 | class MailingList extends HttpApi |
||
26 | { |
||
27 | public function member(): Member |
||
31 | |||
32 | /** |
||
33 | * Returns a paginated list of mailing lists on the domain. |
||
34 | * |
||
35 | * @param int $limit Maximum number of records to return (optional: 100 by default) |
||
36 | * |
||
37 | * @return PagesResponse |
||
38 | * |
||
39 | * @throws \Exception |
||
40 | */ |
||
41 | 2 | View Code Duplication | public function pages(int $limit = 100) |
53 | |||
54 | /** |
||
55 | * Creates a new mailing list on the current domain. |
||
56 | * |
||
57 | * @param string $address Address for the new mailing list |
||
58 | * @param string $name Name for the new mailing list (optional) |
||
59 | * @param string $description Description for the new mailing list (optional) |
||
60 | * @param string $accessLevel List access level, one of: readonly (default), members, everyone |
||
61 | * |
||
62 | * @return CreateResponse |
||
63 | * |
||
64 | * @throws \Exception |
||
65 | */ |
||
66 | 3 | public function create(string $address, string $name = null, string $description = null, string $accessLevel = 'readonly') |
|
84 | |||
85 | /** |
||
86 | * Returns a single mailing list. |
||
87 | * |
||
88 | * @param string $address Address of the mailing list |
||
89 | * |
||
90 | * @return ShowResponse |
||
91 | * |
||
92 | * @throws \Exception |
||
93 | */ |
||
94 | 2 | public function show(string $address) |
|
102 | |||
103 | /** |
||
104 | * Updates a mailing list. |
||
105 | * |
||
106 | * @param string $address Address of the mailing list |
||
107 | * @param array $parameters Array of field => value pairs to update |
||
108 | * |
||
109 | * @return UpdateResponse |
||
110 | * |
||
111 | * @throws \Exception |
||
112 | */ |
||
113 | 2 | public function update(string $address, array $parameters = []) |
|
137 | |||
138 | /** |
||
139 | * Removes a mailing list from the domain. |
||
140 | * |
||
141 | * @param string $address Address of the mailing list |
||
142 | * |
||
143 | * @return DeleteResponse |
||
144 | * |
||
145 | * @throws \Exception |
||
146 | */ |
||
147 | 1 | public function delete(string $address) |
|
155 | } |
||
156 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: