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 | use ImplementsActions; |
||
14 | |||
15 | /** |
||
16 | * Get all contacts. |
||
17 | * |
||
18 | * @return array |
||
19 | */ |
||
20 | public function contacts() |
||
28 | |||
29 | /** |
||
30 | * Find contact by email. |
||
31 | * |
||
32 | * @param string $email |
||
33 | * |
||
34 | * @return Contact|null |
||
35 | */ |
||
36 | public function findContact($email) |
||
46 | |||
47 | /** |
||
48 | * Create new contact. |
||
49 | * |
||
50 | * @param string $email |
||
51 | * @param string $firstName |
||
52 | * @param string $lastName |
||
53 | * @param int|null $orgid |
||
|
|||
54 | * |
||
55 | * @return Contact|null |
||
56 | */ |
||
57 | View Code Duplication | public function createContact($email, $firstName, $lastName, $phone = null) |
|
58 | { |
||
59 | $contacts = $this->transformCollection( |
||
60 | $this->post('contacts', ['json' => ['contact' => compact('email', 'firstName', 'lastName', 'phone')]]), |
||
61 | Contact::class |
||
62 | ); |
||
63 | |||
64 | return array_shift($contacts); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Find or create a contact. |
||
69 | * |
||
70 | * @param string $email |
||
71 | * @param string $firstName |
||
72 | * @param string $lastName |
||
73 | * @param string $phone |
||
74 | * |
||
75 | * @return Contact |
||
76 | */ |
||
77 | public function findOrCreateContact($email, $firstName, $lastName, $phone) |
||
87 | |||
88 | /** |
||
89 | * Update or create an account. |
||
90 | * |
||
91 | * @param string $name |
||
92 | * @param array $data |
||
93 | * |
||
94 | * @return Contact |
||
95 | */ |
||
96 | View Code Duplication | public function updateOrCreateContact($email, $firstName, $lastName, $phone) |
|
105 | |||
106 | /** |
||
107 | * Get all automations of a contact. |
||
108 | * |
||
109 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | public function contactAutomations(Contact $contact) |
||
121 | |||
122 | /** |
||
123 | * Get all tags of a contact. |
||
124 | * |
||
125 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
126 | * |
||
127 | * @return array |
||
128 | */ |
||
129 | public function contactTags(Contact $contact) |
||
137 | |||
138 | /** |
||
139 | * Removing a automation from a contact. |
||
140 | * |
||
141 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
142 | * @param \TestMonitor\ActiveCampaign\Resources\Automation $automation |
||
143 | */ |
||
144 | View Code Duplication | public function removeAutomationFromContact(Contact $contact, Automation $automation) |
|
158 | |||
159 | /** |
||
160 | * Removing all automations from a contact. |
||
161 | * |
||
162 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
163 | */ |
||
164 | public function removeAllAutomationsFromContact(Contact $contact) |
||
172 | |||
173 | /** |
||
174 | * Removing a tag from a contact. |
||
175 | * |
||
176 | * @param \TestMonitor\ActiveCampaign\Resources\Contact $contact |
||
177 | * @param \TestMonitor\ActiveCampaign\Resources\Tag $tag |
||
178 | */ |
||
179 | View Code Duplication | public function removeTagFromContact(Contact $contact, Tag $tag) |
|
193 | } |
||
194 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.