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 () |
|
| 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 () |
|
| 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 () |
|
| 144 | |||
| 145 | /** |
||
| 146 | * The update's body. |
||
| 147 | * |
||
| 148 | * @api |
||
| 149 | * |
||
| 150 | * @since 0.1.0 |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 2 | public function getBody () |
|
| 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 () |
|
| 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 () |
|
| 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 () |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get an array of this update's assets |
||
| 212 | * |
||
| 213 | * Sample array structure of assets |
||
| 214 | * |
||
| 215 | * ``` |
||
| 216 | * array( |
||
| 217 | * 0 => array( |
||
| 218 | * 'account_id' => 115448 |
||
| 219 | * 'big_geometry' => '250x250' |
||
| 220 | * 'created_at' => '2017-01-21T09:45:28Z' |
||
| 221 | * 'crocodoc_status' => null |
||
| 222 | * 'crocodoc_uuid' => null |
||
| 223 | * 'crocodoc_viewable' => true |
||
| 224 | * 'desc' => null |
||
| 225 | * 'holder_id' => 23611844 |
||
| 226 | * 'holder_type' => 'Post' |
||
| 227 | * 'id' => 2401793 |
||
| 228 | * 'large_geometry' => '250x250' |
||
| 229 | * 'metadata' => Array () |
||
| 230 | * 'original_geometry' => '250x250' |
||
| 231 | * 'resource_content_type' => 'image/png' |
||
| 232 | * 'resource_file_name' => 'sample.png' |
||
| 233 | * 'resource_file_size' => 6077 |
||
| 234 | * 'thumb_big_geometry' => '250x250' |
||
| 235 | * 'thumb_geometry' => '150x150' |
||
| 236 | * 'updated_at' => '2017-01-21T09:45:32Z' |
||
| 237 | * 'uploaded_by_id' => 303448 |
||
| 238 | * ) |
||
| 239 | * ) |
||
| 240 | * ``` |
||
| 241 | * |
||
| 242 | * @api |
||
| 243 | * |
||
| 244 | * @since 0.3.0 Documentation has been corrected; this returns an array |
||
| 245 | * @since 0.1.0 |
||
| 246 | * |
||
| 247 | * @return array |
||
| 248 | */ |
||
| 249 | 2 | public function getAssets () |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Creation time. |
||
| 258 | * |
||
| 259 | * @api |
||
| 260 | * |
||
| 261 | * @since 0.1.0 |
||
| 262 | * |
||
| 263 | * @return \DateTime |
||
| 264 | */ |
||
| 265 | 1 | public function getCreatedAt () |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Last update time. |
||
| 275 | * |
||
| 276 | * @api |
||
| 277 | * |
||
| 278 | * @since 0.1.0 |
||
| 279 | * |
||
| 280 | * @return \DateTime |
||
| 281 | */ |
||
| 282 | 1 | public function getUpdatedAt () |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Retrieve whether or not this update has any attachments |
||
| 292 | * |
||
| 293 | * @api |
||
| 294 | * |
||
| 295 | * @since 0.3.0 Previously was available as 'getHasAssets()' |
||
| 296 | * |
||
| 297 | * @return bool |
||
| 298 | */ |
||
| 299 | 1 | public function hasAssets () |
|
| 305 | |||
| 306 | // ================================================================================================================= |
||
| 307 | // Modification functions |
||
| 308 | // ================================================================================================================= |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Delete this update |
||
| 312 | * |
||
| 313 | * @api |
||
| 314 | * |
||
| 315 | * @since 0.1.0 |
||
| 316 | * |
||
| 317 | * @throws InvalidObjectException if this PulseUpdate has already been deleted |
||
| 318 | */ |
||
| 319 | View Code Duplication | public function deleteUpdate () |
|
| 328 | |||
| 329 | // ================================================================================================================= |
||
| 330 | // Liking functions |
||
| 331 | // ================================================================================================================= |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Have a user like an update |
||
| 335 | * |
||
| 336 | * @api |
||
| 337 | * |
||
| 338 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
| 339 | * |
||
| 340 | * @since 0.1.0 |
||
| 341 | * |
||
| 342 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
| 343 | * |
||
| 344 | * @return bool Returns true on success |
||
| 345 | */ |
||
| 346 | 1 | public function likeUpdate ($user) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Have a user unlike an update |
||
| 353 | * |
||
| 354 | * @api |
||
| 355 | * |
||
| 356 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
| 357 | * |
||
| 358 | * @since 0.1.0 |
||
| 359 | * |
||
| 360 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
| 361 | * |
||
| 362 | * @return bool Returns true on success |
||
| 363 | */ |
||
| 364 | 2 | public function unlikeUpdate ($user) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Like and un-liking functionality |
||
| 371 | * |
||
| 372 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
| 373 | * @param bool $like True to like the update, false to unlike |
||
| 374 | * |
||
| 375 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
| 376 | * |
||
| 377 | * @return bool Returns true on success |
||
| 378 | */ |
||
| 379 | 3 | private function likeUnlikeUpdate ($user, $like) |
|
| 389 | |||
| 390 | // ================================================================================================================= |
||
| 391 | // PulseUpdate functions |
||
| 392 | // ================================================================================================================= |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get all of the account's updates (ordered from newest to oldest) |
||
| 396 | * |
||
| 397 | * ``` |
||
| 398 | * array['since'] \DateTime - Get updates from a specific date |
||
| 399 | * ['until'] \DateTime - Get updates until a specific date |
||
| 400 | * ['updated_since'] \DateTime - Get updates that were edited or replied to after a specific date |
||
| 401 | * ['updated_until'] \DateTime - Get updates that were edited or replied to before a specific date |
||
| 402 | * ``` |
||
| 403 | * |
||
| 404 | * If you do not pass \DateTime objects, they should be strings of dates in the format, YYYY-mm-dd, or a unix |
||
| 405 | * timestamp |
||
| 406 | * |
||
| 407 | * @api |
||
| 408 | * |
||
| 409 | * @param array $params GET parameters passed to the URL (see above) |
||
| 410 | * |
||
| 411 | * @since 0.3.0 $params now accepts \DateTime objects and will be converted automatically. Strings will also try |
||
| 412 | * to be converted to Unix timestamps |
||
| 413 | * @since 0.1.0 |
||
| 414 | * |
||
| 415 | * @return PulseUpdate[] |
||
| 416 | */ |
||
| 417 | 4 | public static function getUpdates ($params = []) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Create a new update |
||
| 447 | * |
||
| 448 | * @api |
||
| 449 | * |
||
| 450 | * @param int|PulseUser $user The author of this post |
||
| 451 | * @param int|Pulse $pulse The Pulse to whom this update will belong to |
||
| 452 | * @param string $text The content of the update |
||
| 453 | * @param null|bool $announceToAll Whether or not to announce this update to everyone's wall |
||
| 454 | * |
||
| 455 | * @since 0.3.0 An InvalidArgumentException can now be thrown |
||
| 456 | * @since 0.1.0 |
||
| 457 | * |
||
| 458 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
| 459 | * |
||
| 460 | * @return PulseUpdate |
||
| 461 | */ |
||
| 462 | 2 | public static function createUpdate ($user, $pulse, $text, $announceToAll = null) |
|
| 479 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.