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 |
||
| 11 | trait ManagesContacts |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Get all contacts. |
||
| 15 | * |
||
| 16 | * @return array |
||
| 17 | */ |
||
| 18 | public function contacts() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Find contact by email. |
||
| 29 | * |
||
| 30 | * @param string $email |
||
| 31 | * |
||
| 32 | * @return Contact|null |
||
| 33 | */ |
||
| 34 | public function findContact($email) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Find contact by ID. |
||
| 47 | * |
||
| 48 | * @param int $id |
||
| 49 | * |
||
| 50 | * @return Contact|null |
||
| 51 | */ |
||
| 52 | public function findContactById($id) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Create new contact. |
||
| 60 | * |
||
| 61 | * @param string $email |
||
| 62 | * @param string $firstName |
||
| 63 | * @param string $lastName |
||
| 64 | * @param int|null $orgid |
||
| 65 | * |
||
| 66 | * @return Contact|null |
||
| 67 | */ |
||
| 68 | public function createContact($email, $firstName, $lastName, $orgid = null) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Find or create a contact. |
||
| 80 | * |
||
| 81 | * @param string $email |
||
| 82 | * @param string $firstName |
||
| 83 | * @param string $lastName |
||
| 84 | * @param int|null $orgid |
||
| 85 | * |
||
| 86 | * @return Contact |
||
| 87 | */ |
||
| 88 | public function findOrCreateContact($email, $firstName, $lastName, $orgid = null) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Updates a contact. |
||
| 101 | * |
||
| 102 | * @param Contact|int|string $id |
||
| 103 | * @param string|null $email |
||
| 104 | * @param string|null $firstName |
||
| 105 | * @param string|null $lastName |
||
| 106 | * @param null $orgid |
||
| 107 | * |
||
| 108 | * @return Contact|null |
||
| 109 | */ |
||
| 110 | public function updateContact($id, $email, $firstName, $lastName, $orgid = null) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Deletes a contact. |
||
| 121 | * |
||
| 122 | * @param Contact|int|string $contact |
||
| 123 | */ |
||
| 124 | public function deleteContact($contact) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get all automations of a contact. |
||
| 131 | * |
||
| 132 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 133 | * |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | public function contactAutomations(Contact $contact) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get all tags of a contact. |
||
| 147 | * |
||
| 148 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function contactTags(Contact $contact) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Removing a automation from a contact. |
||
| 163 | * |
||
| 164 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 165 | * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation |
||
| 166 | */ |
||
| 167 | View Code Duplication | public function removeAutomationFromContact(Contact $contact, Automation $automation) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Removing all automations from a contact. |
||
| 184 | * |
||
| 185 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 186 | */ |
||
| 187 | public function removeAllAutomationsFromContact(Contact $contact) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Removing a tag from a contact. |
||
| 198 | * |
||
| 199 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 200 | * @param \TestMonitor\ActiveCampaign\Resources\Tag $tag |
||
| 201 | */ |
||
| 202 | View Code Duplication | public function removeTagFromContact(Contact $contact, Tag $tag) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Determines the contact ID. |
||
| 219 | * |
||
| 220 | * @param Contact|int|string $contact |
||
| 221 | */ |
||
| 222 | protected function getContactId($contact) |
||
| 234 | } |
||
| 235 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.