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 |
||
| 28 | class MailingList extends HttpApi |
||
| 29 | { |
||
| 30 | public function member(): Member |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Returns a paginated list of mailing lists on the domain. |
||
| 37 | * |
||
| 38 | * @param int $limit Maximum number of records to return (optional: 100 by default) |
||
| 39 | * |
||
| 40 | * @return PagesResponse |
||
| 41 | * |
||
| 42 | * @throws \Exception |
||
| 43 | */ |
||
| 44 | 2 | public function pages(int $limit = 100) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Creates a new mailing list on the current domain. |
||
| 59 | * |
||
| 60 | * @param string $address Address for the new mailing list |
||
| 61 | * @param string $name Name for the new mailing list (optional) |
||
| 62 | * @param string $description Description for the new mailing list (optional) |
||
| 63 | * @param string $accessLevel List access level, one of: readonly (default), members, everyone |
||
| 64 | * |
||
| 65 | * @return CreateResponse |
||
| 66 | * |
||
| 67 | * @throws \Exception |
||
| 68 | */ |
||
| 69 | 3 | public function create(string $address, string $name = null, string $description = null, string $accessLevel = 'readonly', string $replyPreference = 'list') |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Returns a single mailing list. |
||
| 92 | * |
||
| 93 | * @param string $address Address of the mailing list |
||
| 94 | * |
||
| 95 | * @return ShowResponse |
||
| 96 | * |
||
| 97 | * @throws \Exception |
||
| 98 | */ |
||
| 99 | 2 | View Code Duplication | public function show(string $address) |
| 107 | |||
| 108 | /** |
||
| 109 | * Updates a mailing list. |
||
| 110 | * |
||
| 111 | * @param string $address Address of the mailing list |
||
| 112 | * @param array $parameters Array of field => value pairs to update |
||
| 113 | * |
||
| 114 | * @return UpdateResponse |
||
| 115 | * |
||
| 116 | * @throws \Exception |
||
| 117 | */ |
||
| 118 | 2 | public function update(string $address, array $parameters = []) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Removes a mailing list from the domain. |
||
| 145 | * |
||
| 146 | * @param string $address Address of the mailing list |
||
| 147 | * |
||
| 148 | * @return DeleteResponse |
||
| 149 | * |
||
| 150 | * @throws \Exception |
||
| 151 | */ |
||
| 152 | 1 | public function delete(string $address) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Validates mailing list. |
||
| 163 | * |
||
| 164 | * @param string $address Address of the mailing list |
||
| 165 | * |
||
| 166 | * @return ValidateResponse |
||
| 167 | * |
||
| 168 | * @throws \Exception |
||
| 169 | */ |
||
| 170 | 1 | public function validate(string $address) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Get mailing list validation status. |
||
| 181 | * |
||
| 182 | * @param string $address Address of the mailing list |
||
| 183 | * |
||
| 184 | * @return ValidationStatusResponse |
||
| 185 | * |
||
| 186 | * @throws \Exception |
||
| 187 | */ |
||
| 188 | 1 | View Code Duplication | public function getValidationStatus(string $address) |
| 196 | |||
| 197 | /** |
||
| 198 | * Cancel mailing list validation. |
||
| 199 | * |
||
| 200 | * @param string $address Address of the mailing list |
||
| 201 | * |
||
| 202 | * @return ValidationCancelResponse |
||
| 203 | * |
||
| 204 | * @throws \Exception |
||
| 205 | */ |
||
| 206 | 1 | View Code Duplication | public function cancelValidation(string $address) |
| 214 | } |
||
| 215 |
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: