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 |
||
20 | class PulseUpdate extends ApiObject |
||
21 | { |
||
22 | const API_PREFIX = "updates"; |
||
23 | |||
24 | /** |
||
25 | * User who wrote the update. |
||
26 | * |
||
27 | * @var array|PulseUser |
||
28 | */ |
||
29 | protected $user; |
||
30 | |||
31 | /** |
||
32 | * The resource's URL. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $url; |
||
37 | |||
38 | /** |
||
39 | * The update's body. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $body; |
||
44 | |||
45 | /** |
||
46 | * The update's body in plain text |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $body_text; |
||
51 | |||
52 | /** |
||
53 | * The replies made to this update. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $replies; |
||
58 | |||
59 | /** |
||
60 | * The update's kind. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $kind; |
||
65 | |||
66 | /** |
||
67 | * The update's has_assets. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $has_assets; |
||
72 | |||
73 | /** |
||
74 | * The update's assets. |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $assets; |
||
79 | |||
80 | /** |
||
81 | * Creation time. |
||
82 | * |
||
83 | * @var \DateTime |
||
84 | */ |
||
85 | protected $created_at; |
||
86 | |||
87 | /** |
||
88 | * Last update time. |
||
89 | * |
||
90 | * @var \DateTime |
||
91 | */ |
||
92 | protected $updated_at; |
||
93 | |||
94 | // ================================================================================================================= |
||
95 | // Getter functions |
||
96 | // ================================================================================================================= |
||
97 | |||
98 | /** |
||
99 | * User who wrote the update. |
||
100 | * |
||
101 | * @api |
||
102 | * |
||
103 | * @since 0.1.0 |
||
104 | * |
||
105 | * @return PulseUser |
||
106 | */ |
||
107 | 1 | public function getAuthor () |
|
108 | { |
||
109 | 1 | $this->lazyLoad(); |
|
110 | 1 | self::lazyCast($this->user, "PulseUser"); |
|
111 | |||
112 | 1 | return $this->user; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * The resource's URL. |
||
117 | * |
||
118 | * @api |
||
119 | * |
||
120 | * @since 0.1.0 |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 1 | public function getUrl () |
|
125 | { |
||
126 | 1 | $this->lazyLoad(); |
|
127 | |||
128 | 1 | return $this->url; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * The update's id. |
||
133 | * |
||
134 | * @api |
||
135 | * |
||
136 | * @since 0.1.0 |
||
137 | * |
||
138 | * @return string |
||
|
|||
139 | */ |
||
140 | 4 | public function getId () |
|
141 | { |
||
142 | 4 | return $this->id; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * The update's body. |
||
147 | * |
||
148 | * @api |
||
149 | * |
||
150 | * @since 0.1.0 |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 1 | public function getBody () |
|
155 | { |
||
156 | 1 | $this->lazyLoad(); |
|
157 | |||
158 | 1 | return $this->body; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * The update's body in plain text |
||
163 | * |
||
164 | * @api |
||
165 | * |
||
166 | * @since 0.1.0 |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | 1 | public function getBodyText () |
|
171 | { |
||
172 | 1 | $this->lazyLoad(); |
|
173 | |||
174 | 1 | return $this->body_text; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * The replies made to this update. |
||
179 | * |
||
180 | * @api |
||
181 | * |
||
182 | * @since 0.1.0 |
||
183 | * |
||
184 | * @return static[] |
||
185 | */ |
||
186 | 3 | public function getReplies () |
|
187 | { |
||
188 | 3 | $this->lazyLoad(); |
|
189 | 3 | self::lazyCastAll($this->replies, "PulseUpdate"); |
|
190 | |||
191 | 3 | return $this->replies; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * The update's kind. |
||
196 | * |
||
197 | * @api |
||
198 | * |
||
199 | * @since 0.1.0 |
||
200 | * |
||
201 | * @return string |
||
202 | */ |
||
203 | 1 | public function getKind () |
|
204 | { |
||
205 | 1 | $this->lazyLoad(); |
|
206 | |||
207 | 1 | return $this->kind; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Retrieve whether or not this update has any attachments |
||
212 | * |
||
213 | * @api |
||
214 | * @todo Remove at 0.4.0 or next major release |
||
215 | * @deprecated 0.3.0 Use PulseUpdate::hasAssets(). To be removed in 0.4.0 |
||
216 | * @since 0.1.0 |
||
217 | * @return string |
||
218 | */ |
||
219 | public function getHasAssets () |
||
220 | { |
||
221 | return $this->hasAssets(); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Get an array of this update's assets |
||
226 | * |
||
227 | * Sample array structure of assets |
||
228 | * |
||
229 | * ``` |
||
230 | * array( |
||
231 | * 0 => array( |
||
232 | * 'account_id' => 115448 |
||
233 | * 'big_geometry' => '250x250' |
||
234 | * 'created_at' => '2017-01-21T09:45:28Z' |
||
235 | * 'crocodoc_status' => null |
||
236 | * 'crocodoc_uuid' => null |
||
237 | * 'crocodoc_viewable' => true |
||
238 | * 'desc' => null |
||
239 | * 'holder_id' => 23611844 |
||
240 | * 'holder_type' => 'Post' |
||
241 | * 'id' => 2401793 |
||
242 | * 'large_geometry' => '250x250' |
||
243 | * 'metadata' => Array () |
||
244 | * 'original_geometry' => '250x250' |
||
245 | * 'resource_content_type' => 'image/png' |
||
246 | * 'resource_file_name' => 'sample.png' |
||
247 | * 'resource_file_size' => 6077 |
||
248 | * 'thumb_big_geometry' => '250x250' |
||
249 | * 'thumb_geometry' => '150x150' |
||
250 | * 'updated_at' => '2017-01-21T09:45:32Z' |
||
251 | * 'uploaded_by_id' => 303448 |
||
252 | * ) |
||
253 | * ) |
||
254 | * ``` |
||
255 | * |
||
256 | * @api |
||
257 | * |
||
258 | * @since 0.3.0 Documentation has been corrected; this returns an array |
||
259 | * @since 0.1.0 |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | 2 | public function getAssets () |
|
264 | { |
||
265 | 2 | $this->lazyLoad(); |
|
266 | |||
267 | 2 | return $this->assets; |
|
268 | } |
||
269 | |||
270 | /** |
||
271 | * Creation time. |
||
272 | * |
||
273 | * @api |
||
274 | * |
||
275 | * @since 0.1.0 |
||
276 | * |
||
277 | * @return \DateTime |
||
278 | */ |
||
279 | 1 | public function getCreatedAt () |
|
280 | { |
||
281 | 1 | $this->lazyLoad(); |
|
282 | 1 | self::lazyCast($this->created_at, '\DateTime'); |
|
283 | |||
284 | 1 | return $this->created_at; |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * Last update time. |
||
289 | * |
||
290 | * @api |
||
291 | * |
||
292 | * @since 0.1.0 |
||
293 | * |
||
294 | * @return \DateTime |
||
295 | */ |
||
296 | 1 | public function getUpdatedAt () |
|
297 | { |
||
298 | 1 | $this->lazyLoad(); |
|
299 | 1 | self::lazyCast($this->updated_at, '\DateTime'); |
|
300 | |||
301 | 1 | return $this->updated_at; |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * Get the users watching this update |
||
306 | * |
||
307 | * @api |
||
308 | * @todo Remove at 0.4.0 or next major release |
||
309 | * @deprecated 0.3.0 This data is no longer provided by the DaPulse API; this function will be removed in the next |
||
310 | * major release with planned replacement |
||
311 | * @since 0.1.0 |
||
312 | * @return PulseUser[] |
||
313 | */ |
||
314 | public function getWatchers () |
||
318 | |||
319 | /** |
||
320 | * Retrieve whether or not this update has any attachments |
||
321 | * |
||
322 | * @api |
||
323 | * |
||
324 | * @since 0.3.0 Previously was available as 'getHasAssets()' |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | 1 | public function hasAssets () |
|
329 | { |
||
330 | 1 | $this->lazyLoad(); |
|
331 | |||
332 | 1 | return $this->has_assets; |
|
333 | } |
||
334 | |||
335 | // ================================================================================================================= |
||
336 | // Modification functions |
||
337 | // ================================================================================================================= |
||
338 | |||
339 | /** |
||
340 | * Delete this update |
||
341 | * |
||
342 | * @api |
||
343 | * |
||
344 | * @since 0.1.0 |
||
345 | * |
||
346 | * @throws InvalidObjectException if this PulseUpdate has already been deleted |
||
347 | */ |
||
348 | View Code Duplication | public function deleteUpdate () |
|
349 | { |
||
350 | $this->checkInvalid(); |
||
351 | |||
352 | $url = sprintf("%s/%d.json", self::apiEndpoint(), $this->getId()); |
||
353 | self::sendDelete($url); |
||
354 | |||
355 | $this->deletedObject = true; |
||
356 | } |
||
357 | |||
358 | // ================================================================================================================= |
||
359 | // Liking functions |
||
360 | // ================================================================================================================= |
||
361 | |||
362 | /** |
||
363 | * Have a user like an update |
||
364 | * |
||
365 | * @api |
||
366 | * |
||
367 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
368 | * |
||
369 | * @since 0.1.0 |
||
370 | * |
||
371 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
372 | * |
||
373 | * @return bool Returns true on success |
||
374 | */ |
||
375 | 1 | public function likeUpdate ($user) |
|
376 | { |
||
377 | 1 | return $this->likeUnlikeUpdate($user, true); |
|
378 | } |
||
379 | |||
380 | /** |
||
381 | * Have a user unlike an update |
||
382 | * |
||
383 | * @api |
||
384 | * |
||
385 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
386 | * |
||
387 | * @since 0.1.0 |
||
388 | * |
||
389 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
390 | * |
||
391 | * @return bool Returns true on success |
||
392 | */ |
||
393 | 2 | public function unlikeUpdate ($user) |
|
394 | { |
||
395 | 2 | return $this->likeUnlikeUpdate($user, false); |
|
396 | } |
||
397 | |||
398 | /** |
||
399 | * Like and un-liking functionality |
||
400 | * |
||
401 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
402 | * @param bool $like True to like the update, false to unlike |
||
403 | * |
||
404 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
405 | * |
||
406 | * @return bool Returns true on success |
||
407 | */ |
||
408 | 3 | private function likeUnlikeUpdate ($user, $like) |
|
409 | { |
||
410 | 3 | $user = PulseUser::_castToInt($user); |
|
411 | 3 | $url = sprintf("%s/%d/%s.json", self::apiEndpoint(), $this->getId(), (($like) ? "like" : "unlike")); |
|
412 | $params = [ |
||
413 | 3 | "user" => $user |
|
414 | ]; |
||
415 | |||
416 | 3 | return self::sendPost($url, $params); |
|
417 | } |
||
418 | |||
419 | // ================================================================================================================= |
||
420 | // PulseUpdate functions |
||
421 | // ================================================================================================================= |
||
422 | |||
423 | /** |
||
424 | * Get all of the account's updates (ordered from newest to oldest) |
||
425 | * |
||
426 | * ``` |
||
427 | * array['since'] \DateTime - Get updates from a specific date |
||
428 | * ['until'] \DateTime - Get updates until a specific date |
||
429 | * ['updated_since'] \DateTime - Get updates that were edited or replied to after a specific date |
||
430 | * ['updated_until'] \DateTime - Get updates that were edited or replied to before a specific date |
||
431 | * ``` |
||
432 | * |
||
433 | * If you do not pass \DateTime objects, they should be strings of dates in the format, YYYY-mm-dd, or a unix |
||
434 | * timestamp |
||
435 | * |
||
436 | * @api |
||
437 | * |
||
438 | * @param array $params GET parameters passed to the URL (see above) |
||
439 | * |
||
440 | * @since 0.3.0 $params now accepts \DateTime objects and will be converted automatically. Strings will also try |
||
441 | * to be converted to Unix timestamps |
||
442 | * @since 0.1.0 |
||
443 | * |
||
444 | * @return PulseUpdate[] |
||
445 | */ |
||
446 | 4 | public static function getUpdates ($params = []) |
|
447 | { |
||
448 | 4 | $url = sprintf("%s.json", self::apiEndpoint()); |
|
449 | $dateKeys = [ |
||
450 | 4 | 'since', |
|
451 | 'until', |
||
452 | 'updated_since', |
||
453 | 'updated_until' |
||
454 | ]; |
||
455 | |||
456 | 4 | foreach ($params as $key => &$value) |
|
457 | { |
||
458 | 3 | if (in_array($key, $dateKeys)) |
|
459 | { |
||
460 | 3 | if ($value instanceof \DateTime) |
|
461 | { |
||
462 | 1 | $value = date_format($value, 'U'); // A unix timestamp will allow for hours & minutes |
|
463 | } |
||
464 | 2 | else if (($unix = strtotime($value))) |
|
465 | { |
||
466 | 3 | $value = $unix; |
|
467 | } |
||
468 | } |
||
469 | } |
||
470 | |||
471 | 4 | return self::fetchAndCastToObjectArray($url, 'PulseUpdate', $params); |
|
472 | } |
||
473 | |||
474 | /** |
||
475 | * Create a new update |
||
476 | * |
||
477 | * @api |
||
478 | * |
||
479 | * @param int|PulseUser $user The author of this post |
||
480 | * @param int|Pulse $pulse The Pulse to whom this update will belong to |
||
481 | * @param string $text The content of the update |
||
482 | * @param null|bool $announceToAll Whether or not to announce this update to everyone's wall |
||
483 | * |
||
484 | * @since 0.1.0 |
||
485 | * |
||
486 | * @return PulseUpdate |
||
487 | */ |
||
488 | public static function createUpdate ($user, $pulse, $text, $announceToAll = null) |
||
513 | } |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.