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) |
||
53 | { |
||
54 | $contacts = $this->transformCollection( |
||
55 | $this->get('contacts', ['query' => ['id' => $id]]), |
||
56 | Contact::class, |
||
57 | 'contacts' |
||
58 | ); |
||
59 | |||
60 | return array_shift($contacts); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Create new contact. |
||
65 | * |
||
66 | * @param string $email |
||
67 | * @param string $firstName |
||
68 | * @param string $lastName |
||
69 | * @param int|null $orgid |
||
70 | * |
||
71 | * @return Contact|null |
||
72 | */ |
||
73 | public function createContact($email, $firstName, $lastName, $orgid = null) |
||
82 | |||
83 | /** |
||
84 | * Find or create a contact. |
||
85 | * |
||
86 | * @param string $email |
||
87 | * @param string $firstName |
||
88 | * @param string $lastName |
||
89 | * @param int|null $orgid |
||
90 | * |
||
91 | * @return Contact |
||
92 | */ |
||
93 | public function findOrCreateContact($email, $firstName, $lastName, $orgid = null) |
||
103 | |||
104 | /** |
||
105 | * Updates a contact. |
||
106 | * |
||
107 | * @param Contact|int|string $id |
||
108 | * @param string|null $email |
||
109 | * @param string|null $firstName |
||
110 | * @param string|null $lastName |
||
111 | * @param null $orgid |
||
112 | * |
||
113 | * @return Contact|null |
||
114 | */ |
||
115 | public function updateContact($id, $email, $firstName, $lastName, $orgid = null) |
||
116 | { |
||
117 | $id = $this->getContactId($id); |
||
118 | |||
119 | $this->put('contacts/'.$id, ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'orgid')]]); |
||
120 | |||
121 | return $this->findContactById($id); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Deletes a contact. |
||
126 | * |
||
127 | * @param Contact|int|string $contact |
||
128 | */ |
||
129 | public function deleteContact($contact) |
||
130 | { |
||
131 | $this->delete('contacts/'.$this->getContactId($contact)); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get all automations of a contact. |
||
136 | * |
||
137 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function contactAutomations(Contact $contact) |
||
149 | |||
150 | /** |
||
151 | * Get all tags of a contact. |
||
152 | * |
||
153 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | public function contactTags(Contact $contact) |
||
165 | |||
166 | /** |
||
167 | * Removing a automation from a contact. |
||
168 | * |
||
169 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
170 | * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation |
||
171 | */ |
||
172 | View Code Duplication | public function removeAutomationFromContact(Contact $contact, Automation $automation) |
|
186 | |||
187 | /** |
||
188 | * Removing all automations from a contact. |
||
189 | * |
||
190 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
191 | */ |
||
192 | public function removeAllAutomationsFromContact(Contact $contact) |
||
200 | |||
201 | /** |
||
202 | * Removing a tag from a contact. |
||
203 | * |
||
204 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
205 | * @param \TestMonitor\ActiveCampaign\Resources\Tag $tag |
||
206 | */ |
||
207 | View Code Duplication | public function removeTagFromContact(Contact $contact, Tag $tag) |
|
221 | |||
222 | /** |
||
223 | * Determines the contact ID. |
||
224 | * |
||
225 | * @param Contact|int|string $contact |
||
226 | */ |
||
227 | protected function getContactId($contact) |
||
239 | } |
||
240 |
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
Idable
provides a methodequalsId
that 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.