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 |
||
8 | class PulseUpdate extends ApiObject |
||
9 | { |
||
10 | const API_PREFIX = "updates"; |
||
11 | |||
12 | /** |
||
13 | * User who wrote the update. |
||
14 | * |
||
15 | * @var array|PulseUser |
||
16 | */ |
||
17 | protected $user; |
||
18 | |||
19 | /** |
||
20 | * The resource's URL. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $url; |
||
25 | |||
26 | /** |
||
27 | * The update's id. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $id; |
||
32 | |||
33 | /** |
||
34 | * The update's body. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $body; |
||
39 | |||
40 | /** |
||
41 | * The update's body in plain text |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $body_text; |
||
46 | |||
47 | /** |
||
48 | * The replies made to this update. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $replies; |
||
53 | |||
54 | /** |
||
55 | * The update's kind. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $kind; |
||
60 | |||
61 | /** |
||
62 | * The update's has_assets. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $has_assets; |
||
67 | |||
68 | /** |
||
69 | * The update's assets. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | protected $assets; |
||
74 | |||
75 | /** |
||
76 | * Creation time. |
||
77 | * |
||
78 | * @var \DateTime |
||
79 | */ |
||
80 | protected $created_at; |
||
81 | |||
82 | /** |
||
83 | * Last update time. |
||
84 | * |
||
85 | * @var \DateTime |
||
86 | */ |
||
87 | protected $updated_at; |
||
88 | |||
89 | // ================================================================================================================= |
||
90 | // Getter functions |
||
91 | // ================================================================================================================= |
||
92 | |||
93 | /** |
||
94 | * User who wrote the update. |
||
95 | * |
||
96 | * @api |
||
97 | * @since 0.1.0 |
||
98 | * @return PulseUser |
||
99 | */ |
||
100 | 1 | public function getAuthor () |
|
106 | |||
107 | /** |
||
108 | * The resource's URL. |
||
109 | * |
||
110 | * @api |
||
111 | * @since 0.1.0 |
||
112 | * @return string |
||
113 | */ |
||
114 | 1 | public function getUrl () |
|
118 | |||
119 | /** |
||
120 | * The update's id. |
||
121 | * |
||
122 | * @api |
||
123 | * @since 0.1.0 |
||
124 | * @return string |
||
125 | */ |
||
126 | 4 | public function getId () |
|
130 | |||
131 | /** |
||
132 | * The update's body. |
||
133 | * |
||
134 | * @api |
||
135 | * @since 0.1.0 |
||
136 | * @return string |
||
137 | */ |
||
138 | 1 | public function getBody () |
|
142 | |||
143 | /** |
||
144 | * The update's body in plain text |
||
145 | * |
||
146 | * @api |
||
147 | * @since 0.1.0 |
||
148 | * @return string |
||
149 | */ |
||
150 | 1 | public function getBodyText () |
|
154 | |||
155 | /** |
||
156 | * The replies made to this update. |
||
157 | * |
||
158 | * @api |
||
159 | * @since 0.1.0 |
||
160 | * @return static[] |
||
161 | */ |
||
162 | 3 | public function getReplies () |
|
168 | |||
169 | /** |
||
170 | * The update's kind. |
||
171 | * |
||
172 | * @api |
||
173 | * @since 0.1.0 |
||
174 | * @return string |
||
175 | */ |
||
176 | 1 | public function getKind () |
|
180 | |||
181 | /** |
||
182 | * Retrieve whether or not this update has any attachments |
||
183 | * |
||
184 | * @api |
||
185 | * @todo Remove at 0.4.0 or next major release |
||
|
|||
186 | * @deprecated 0.3.0 Use PulseUpdate::hasAssets(). To be removed in 0.4.0 |
||
187 | * @since 0.1.0 |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getHasAssets () |
||
194 | |||
195 | /** |
||
196 | * Get an array of this update's assets |
||
197 | * |
||
198 | * Sample array structure of assets |
||
199 | * |
||
200 | * ``` |
||
201 | * array( |
||
202 | * 0 => array( |
||
203 | * 'account_id' => 115448 |
||
204 | * 'big_geometry' => '250x250' |
||
205 | * 'created_at' => '2017-01-21T09:45:28Z' |
||
206 | * 'crocodoc_status' => null |
||
207 | * 'crocodoc_uuid' => null |
||
208 | * 'crocodoc_viewable' => true |
||
209 | * 'desc' => null |
||
210 | * 'holder_id' => 23611844 |
||
211 | * 'holder_type' => 'Post' |
||
212 | * 'id' => 2401793 |
||
213 | * 'large_geometry' => '250x250' |
||
214 | * 'metadata' => Array () |
||
215 | * 'original_geometry' => '250x250' |
||
216 | * 'resource_content_type' => 'image/png' |
||
217 | * 'resource_file_name' => 'sample.png' |
||
218 | * 'resource_file_size' => 6077 |
||
219 | * 'thumb_big_geometry' => '250x250' |
||
220 | * 'thumb_geometry' => '150x150' |
||
221 | * 'updated_at' => '2017-01-21T09:45:32Z' |
||
222 | * 'uploaded_by_id' => 303448 |
||
223 | * ) |
||
224 | * ) |
||
225 | * ``` |
||
226 | * |
||
227 | * @api |
||
228 | * |
||
229 | * @since 0.3.0 Documentation has been corrected; this returns an array |
||
230 | * @since 0.1.0 |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | 2 | public function getAssets () |
|
238 | |||
239 | /** |
||
240 | * Creation time. |
||
241 | * |
||
242 | * @api |
||
243 | * @since 0.1.0 |
||
244 | * @return \DateTime |
||
245 | */ |
||
246 | 1 | public function getCreatedAt () |
|
252 | |||
253 | /** |
||
254 | * Last update time. |
||
255 | * |
||
256 | * @api |
||
257 | * @since 0.1.0 |
||
258 | * @return \DateTime |
||
259 | */ |
||
260 | 1 | public function getUpdatedAt () |
|
266 | |||
267 | /** |
||
268 | * Get the users watching this update |
||
269 | * |
||
270 | * @api |
||
271 | * @todo Remove at 0.4.0 or next major release |
||
272 | * @deprecated 0.3.0 This data is no longer provided by the DaPulse API; this function will be removed in the next |
||
273 | * major release with planned replacement |
||
274 | * @since 0.1.0 |
||
275 | * @return PulseUser[] |
||
276 | */ |
||
277 | public function getWatchers () |
||
281 | |||
282 | /** |
||
283 | * Retrieve whether or not this update has any attachments |
||
284 | * |
||
285 | * @api |
||
286 | * |
||
287 | * @since 0.3.0 Previously was available as 'getHasAssets()' |
||
288 | * |
||
289 | * @return bool |
||
290 | */ |
||
291 | 1 | public function hasAssets () |
|
295 | |||
296 | // ================================================================================================================= |
||
297 | // Modification functions |
||
298 | // ================================================================================================================= |
||
299 | |||
300 | /** |
||
301 | * Delete this update |
||
302 | * |
||
303 | * @api |
||
304 | * |
||
305 | * @since 0.1.0 |
||
306 | * |
||
307 | * @throws InvalidObjectException if this PulseUpdate has already been deleted |
||
308 | */ |
||
309 | View Code Duplication | public function deleteUpdate () |
|
318 | |||
319 | // ================================================================================================================= |
||
320 | // Liking functions |
||
321 | // ================================================================================================================= |
||
322 | |||
323 | /** |
||
324 | * Have a user like an update |
||
325 | * |
||
326 | * @api |
||
327 | * |
||
328 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
329 | * |
||
330 | * @since 0.1.0 |
||
331 | * |
||
332 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
333 | * |
||
334 | * @return bool Returns true on success |
||
335 | */ |
||
336 | 1 | public function likeUpdate ($user) |
|
340 | |||
341 | /** |
||
342 | * Have a user unlike an update |
||
343 | * |
||
344 | * @api |
||
345 | * |
||
346 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
347 | * |
||
348 | * @since 0.1.0 |
||
349 | * |
||
350 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
351 | * |
||
352 | * @return bool Returns true on success |
||
353 | */ |
||
354 | 2 | public function unlikeUpdate ($user) |
|
358 | |||
359 | /** |
||
360 | * Like and un-liking functionality |
||
361 | * |
||
362 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
363 | * @param bool $like True to like the update, false to unlike |
||
364 | * |
||
365 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
366 | * |
||
367 | * @return bool Returns true on success |
||
368 | */ |
||
369 | 3 | private function likeUnlikeUpdate ($user, $like) |
|
381 | |||
382 | // ================================================================================================================= |
||
383 | // PulseUpdate functions |
||
384 | // ================================================================================================================= |
||
385 | |||
386 | /** |
||
387 | * Get all of the account's updates (ordered from newest to oldest) |
||
388 | * |
||
389 | * ``` |
||
390 | * array['since'] \DateTime - Get updates from a specific date |
||
391 | * ['until'] \DateTime - Get updates until a specific date |
||
392 | * ['updated_since'] \DateTime - Get updates that were edited or replied to after a specific date |
||
393 | * ['updated_until'] \DateTime - Get updates that were edited or replied to before a specific date |
||
394 | * ``` |
||
395 | * |
||
396 | * If you do not pass \DateTime objects, they should be strings of dates in the format, YYYY-mm-dd, or a unix timestamp |
||
397 | * |
||
398 | * @api |
||
399 | * |
||
400 | * @param array $params GET parameters passed to the URL (see above) |
||
401 | * |
||
402 | * @since 0.3.0 $params now accepts \DateTime objects and will be converted automatically. Strings will also try to |
||
403 | * be converted to Unix timestamps |
||
404 | * @since 0.1.0 |
||
405 | * |
||
406 | * @return PulseUpdate[] |
||
407 | */ |
||
408 | 4 | public static function getUpdates ($params = []) |
|
430 | |||
431 | /** |
||
432 | * Create a new update |
||
433 | * |
||
434 | * @api |
||
435 | * |
||
436 | * @param int|PulseUser $user The author of this post |
||
437 | * @param int|Pulse $pulse The Pulse to whom this update will belong to |
||
438 | * @param string $text The content of the update |
||
439 | * @param null|bool $announceToAll Whether or not to announce this update to everyone's wall |
||
440 | * |
||
441 | * @since 0.1.0 |
||
442 | * |
||
443 | * @return PulseUpdate |
||
444 | */ |
||
445 | public static function createUpdate ($user, $pulse, $text, $announceToAll = NULL) |
||
470 | } |
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.