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 | class WufooForm extends ApiObject |
||
12 | { |
||
13 | /** |
||
14 | * Create an object for interacting with a Wufoo form. |
||
15 | * |
||
16 | * **Warning:** Keep in mind, when using the URL-friendly name, that value will change if you rename the form. It is |
||
17 | * recommended you use the unique ID of the form. |
||
18 | * |
||
19 | * @api |
||
20 | * |
||
21 | * @param string $id The unique ID of the Wufoo form or URL-friendly name. |
||
22 | * |
||
23 | * @since 0.1.0 |
||
24 | */ |
||
25 | public function __construct($id) |
||
29 | |||
30 | /** |
||
31 | * Get the details of this form. |
||
32 | * |
||
33 | * @api |
||
34 | * |
||
35 | * @param bool $includeTodayCount Set to true to include the number of entries received today. |
||
36 | * |
||
37 | * @since 0.1.0 |
||
38 | * |
||
39 | * @return mixed |
||
40 | */ |
||
41 | public function getDetails($includeTodayCount = false) |
||
55 | |||
56 | /** |
||
57 | * Get the fields in this form. |
||
58 | * |
||
59 | * @api |
||
60 | * |
||
61 | * @param bool $getSystem Set to true to receive |
||
62 | * |
||
63 | * @since 0.1.0 |
||
64 | * |
||
65 | * @return array |
||
66 | */ |
||
67 | public function getFields($getSystem = false) |
||
86 | |||
87 | /** |
||
88 | * Get any comments made on this form's entries. |
||
89 | * |
||
90 | * @api |
||
91 | * |
||
92 | * @param int|null $entryID Get comments for only a specific entry |
||
93 | * @param int|null $offset The offset of comments that |
||
94 | * @param int|null $limit The number comments returned in the request (maximum of 100) |
||
95 | * |
||
96 | * @since 0.1.0 |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | public function getComments($entryID = null, $offset = null, $limit = null) |
||
121 | |||
122 | /** |
||
123 | * Get the number of comments made on this form's entries. |
||
124 | * |
||
125 | * @api |
||
126 | * |
||
127 | * @since 0.1.0 |
||
128 | * |
||
129 | * @return int |
||
130 | */ |
||
131 | View Code Duplication | public function getCommentCount() |
|
143 | |||
144 | /** |
||
145 | * Get the entries belonging to this form. |
||
146 | * |
||
147 | * **Warning:** |
||
148 | * - Data in fields that are marked as “Admin Only” are not returned via the API. |
||
149 | * - Data from "hidden" and encrypted fields will be shown |
||
150 | * |
||
151 | * @api |
||
152 | * |
||
153 | * @param EntryQuery|null $query When set to null, 25 entries will be retrieved (a limit imposed by Wufoo). Use an |
||
154 | * EntryQuery object to have more control on what entries to receive and how. |
||
155 | * |
||
156 | * @since 0.1.0 |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public function getEntries(EntryQuery $query = null) |
||
173 | |||
174 | /** |
||
175 | * Get the number of entries this Wufoo form has. |
||
176 | * |
||
177 | * @api |
||
178 | * |
||
179 | * @since 0.1.0 |
||
180 | * |
||
181 | * @return int |
||
182 | */ |
||
183 | View Code Duplication | public function getEntriesCount() |
|
191 | |||
192 | /** |
||
193 | * Submit an entry to this Wufoo form. |
||
194 | * |
||
195 | * @api |
||
196 | * |
||
197 | * @param array $formData An array containing the data that will be POST'd to the Wufoo form. The keys used in the |
||
198 | * the array should be the unique IDs (e.g. Field1, Field12). |
||
199 | * |
||
200 | * @since 0.1.0 |
||
201 | * |
||
202 | * @throws SubmissionException Wufoo returned an error on the entry submission. |
||
203 | * |
||
204 | * @return mixed |
||
205 | */ |
||
206 | public function submitEntry(array $formData) |
||
223 | |||
224 | /** |
||
225 | * Get details of all the forms under this account. |
||
226 | * |
||
227 | * @api |
||
228 | * |
||
229 | * @param bool $includeTodayCount Set to true to include the number of entries received today. |
||
230 | * |
||
231 | * @since 0.1.0 |
||
232 | * |
||
233 | * @return array |
||
234 | */ |
||
235 | public static function getForms($includeTodayCount = false) |
||
256 | } |
||
257 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: