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 | * Create new contact. |
||
| 47 | * |
||
| 48 | * @param string $email |
||
| 49 | * @param string $firstName |
||
| 50 | * @param string $lastName |
||
| 51 | * @param int|null $orgid |
||
| 52 | * |
||
| 53 | * @return Contact|null |
||
| 54 | */ |
||
| 55 | public function createContact($email, $firstName, $lastName, $orgid = null) |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Find or create a contact. |
||
| 67 | * |
||
| 68 | * @param string $email |
||
| 69 | * @param string $firstName |
||
| 70 | * @param string $lastName |
||
| 71 | * @param int|null $orgid |
||
| 72 | * |
||
| 73 | * @return Contact |
||
| 74 | */ |
||
| 75 | public function findOrCreateContact($email, $firstName, $lastName, $orgid = null) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Updates a contact by its ActiveCampaign ID. |
||
| 88 | * |
||
| 89 | * @param $id |
||
| 90 | * @param $email |
||
| 91 | * @param $firstName |
||
| 92 | * @param $lastName |
||
| 93 | * @param null $orgid |
||
| 94 | * |
||
| 95 | * @return Contact|null |
||
| 96 | */ |
||
| 97 | public function updateContactById($id, $email, $firstName, $lastName, $orgid = null) |
||
| 98 | { |
||
| 99 | $this->put('contacts/'.$id, ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'orgid')]]); |
||
| 100 | |||
| 101 | return $this->findContactById($id); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get all automations of a contact. |
||
| 106 | * |
||
| 107 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | public function contactAutomations(Contact $contact) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get all tags of a contact. |
||
| 122 | * |
||
| 123 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function contactTags(Contact $contact) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Removing a automation from a contact. |
||
| 138 | * |
||
| 139 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 140 | * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function removeAutomationFromContact(Contact $contact, Automation $automation) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Removing all automations from a contact. |
||
| 159 | * |
||
| 160 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 161 | */ |
||
| 162 | public function removeAllAutomationsFromContact(Contact $contact) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Removing a tag from a contact. |
||
| 173 | * |
||
| 174 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
| 175 | * @param \TestMonitor\ActiveCampaign\Resources\Tag $tag |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function removeTagFromContact(Contact $contact, Tag $tag) |
|
| 191 | } |
||
| 192 |
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.