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 | * @deprecated 0.1.0 This function will be removed by 0.2.0. Use of PulseUpdate->getAuthor() instead. |
||
| 97 | * |
||
| 98 | * @return PulseUser |
||
| 99 | */ |
||
| 100 | public function getUser () |
||
| 104 | |||
| 105 | /** |
||
| 106 | * User who wrote the update. |
||
| 107 | * |
||
| 108 | * @api |
||
| 109 | * @since 0.1.0 |
||
| 110 | * @return PulseUser |
||
| 111 | */ |
||
| 112 | 1 | public function getAuthor () |
|
| 118 | |||
| 119 | /** |
||
| 120 | * The resource's URL. |
||
| 121 | * |
||
| 122 | * @api |
||
| 123 | * @since 0.1.0 |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 1 | public function getUrl () |
|
| 130 | |||
| 131 | /** |
||
| 132 | * The update's id. |
||
| 133 | * |
||
| 134 | * @api |
||
| 135 | * @since 0.1.0 |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | 4 | public function getId () |
|
| 142 | |||
| 143 | /** |
||
| 144 | * The update's body. |
||
| 145 | * |
||
| 146 | * @api |
||
| 147 | * @since 0.1.0 |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | 1 | public function getBody () |
|
| 154 | |||
| 155 | /** |
||
| 156 | * The update's body in plain text |
||
| 157 | * |
||
| 158 | * @api |
||
| 159 | * @since 0.1.0 |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | 1 | public function getBodyText () |
|
| 166 | |||
| 167 | /** |
||
| 168 | * The replies made to this update. |
||
| 169 | * |
||
| 170 | * @api |
||
| 171 | * @since 0.1.0 |
||
| 172 | * @return static[] |
||
| 173 | */ |
||
| 174 | 3 | public function getReplies () |
|
| 180 | |||
| 181 | /** |
||
| 182 | * The update's kind. |
||
| 183 | * |
||
| 184 | * @api |
||
| 185 | * @since 0.1.0 |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 1 | public function getKind () |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Retrieve whether or not this update has any attachments |
||
| 195 | * |
||
| 196 | * @api |
||
| 197 | * @todo Remove at 0.4.0 or next major release |
||
|
|
|||
| 198 | * @deprecated 0.3.0 Use PulseUpdate::hasAssets(). To be removed in 0.4.0 |
||
| 199 | * @since 0.1.0 |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getHasAssets () |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Get an array of this update's assets |
||
| 209 | * |
||
| 210 | * Sample array structure of assets |
||
| 211 | * |
||
| 212 | * ``` |
||
| 213 | * array( |
||
| 214 | * 0 => array( |
||
| 215 | * 'account_id' => 115448 |
||
| 216 | * 'big_geometry' => '250x250' |
||
| 217 | * 'created_at' => '2017-01-21T09:45:28Z' |
||
| 218 | * 'crocodoc_status' => null |
||
| 219 | * 'crocodoc_uuid' => null |
||
| 220 | * 'crocodoc_viewable' => true |
||
| 221 | * 'desc' => null |
||
| 222 | * 'holder_id' => 23611844 |
||
| 223 | * 'holder_type' => 'Post' |
||
| 224 | * 'id' => 2401793 |
||
| 225 | * 'large_geometry' => '250x250' |
||
| 226 | * 'metadata' => Array () |
||
| 227 | * 'original_geometry' => '250x250' |
||
| 228 | * 'resource_content_type' => 'image/png' |
||
| 229 | * 'resource_file_name' => 'sample.png' |
||
| 230 | * 'resource_file_size' => 6077 |
||
| 231 | * 'thumb_big_geometry' => '250x250' |
||
| 232 | * 'thumb_geometry' => '150x150' |
||
| 233 | * 'updated_at' => '2017-01-21T09:45:32Z' |
||
| 234 | * 'uploaded_by_id' => 303448 |
||
| 235 | * ) |
||
| 236 | * ) |
||
| 237 | * ``` |
||
| 238 | * |
||
| 239 | * @api |
||
| 240 | * |
||
| 241 | * @since 0.3.0 Documentation has been corrected; this returns an array |
||
| 242 | * @since 0.1.0 |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | 2 | public function getAssets () |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Creation time. |
||
| 253 | * |
||
| 254 | * @api |
||
| 255 | * @since 0.1.0 |
||
| 256 | * @return \DateTime |
||
| 257 | */ |
||
| 258 | 1 | public function getCreatedAt () |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Last update time. |
||
| 267 | * |
||
| 268 | * @api |
||
| 269 | * @since 0.1.0 |
||
| 270 | * @return \DateTime |
||
| 271 | */ |
||
| 272 | 1 | public function getUpdatedAt () |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Get the users watching this update |
||
| 281 | * |
||
| 282 | * @api |
||
| 283 | * @todo Remove at 0.4.0 or next major release |
||
| 284 | * @deprecated 0.3.0 This data is no longer provided by the DaPulse API; this function will be removed in the next |
||
| 285 | * major release with planned replacement |
||
| 286 | * @since 0.1.0 |
||
| 287 | * @return PulseUser[] |
||
| 288 | */ |
||
| 289 | public function getWatchers () |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Retrieve whether or not this update has any attachments |
||
| 296 | * |
||
| 297 | * @api |
||
| 298 | * |
||
| 299 | * @since 0.3.0 Previously was available as 'getHasAssets()' |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | 1 | public function hasAssets () |
|
| 307 | |||
| 308 | // ================================================================================================================= |
||
| 309 | // Modification functions |
||
| 310 | // ================================================================================================================= |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Delete this update |
||
| 314 | * |
||
| 315 | * @api |
||
| 316 | * |
||
| 317 | * @since 0.1.0 |
||
| 318 | * |
||
| 319 | * @throws InvalidObjectException if this PulseUpdate has already been deleted |
||
| 320 | */ |
||
| 321 | View Code Duplication | public function deleteUpdate () |
|
| 330 | |||
| 331 | // ================================================================================================================= |
||
| 332 | // Liking functions |
||
| 333 | // ================================================================================================================= |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Have a user like an update |
||
| 337 | * |
||
| 338 | * @api |
||
| 339 | * |
||
| 340 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
| 341 | * |
||
| 342 | * @since 0.1.0 |
||
| 343 | * |
||
| 344 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
| 345 | * |
||
| 346 | * @return bool Returns true on success |
||
| 347 | */ |
||
| 348 | 1 | public function likeUpdate ($user) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Have a user unlike an update |
||
| 355 | * |
||
| 356 | * @api |
||
| 357 | * |
||
| 358 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
| 359 | * |
||
| 360 | * @since 0.1.0 |
||
| 361 | * |
||
| 362 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
| 363 | * |
||
| 364 | * @return bool Returns true on success |
||
| 365 | */ |
||
| 366 | 2 | public function unlikeUpdate ($user) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Like and un-liking functionality |
||
| 373 | * |
||
| 374 | * @param int|PulseUser $user The user that will be liking/un-liking the update |
||
| 375 | * @param bool $like True to like the update, false to unlike |
||
| 376 | * |
||
| 377 | * @throws \InvalidArgumentException if $user is not an integer, is not positive, or is not a PulseUser object |
||
| 378 | * |
||
| 379 | * @return bool Returns true on success |
||
| 380 | */ |
||
| 381 | 3 | private function likeUnlikeUpdate ($user, $like) |
|
| 393 | |||
| 394 | // ================================================================================================================= |
||
| 395 | // PulseUpdate functions |
||
| 396 | // ================================================================================================================= |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Get all of the account's updates (ordered from newest to oldest) |
||
| 400 | * |
||
| 401 | * array['since'] \DateTime Get updates from a specific date |
||
| 402 | * ['until'] \DateTime Get updates until a specific date |
||
| 403 | * ['updated_since'] \DateTime Get updates that were edited or replied to after a specific date |
||
| 404 | * ['updated_until'] \DateTime Get updates that were edited or replied to before a specific date |
||
| 405 | * |
||
| 406 | * If you do not pass \DateTime objects, they should be strings of dates in the format, YYYY-mm-dd, or a unix timestamp |
||
| 407 | * |
||
| 408 | * @api |
||
| 409 | * |
||
| 410 | * @param array $params GET parameters passed to the URL (see above) |
||
| 411 | * |
||
| 412 | * @since 0.3.0 $params now accepts \DateTime objects and will be converted automatically. Strings will also try to |
||
| 413 | * be converted to Unix timestamps |
||
| 414 | * @since 0.1.0 |
||
| 415 | * |
||
| 416 | * @return PulseUpdate[] |
||
| 417 | */ |
||
| 418 | 4 | public static function getUpdates ($params = []) |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Create a new update |
||
| 443 | * |
||
| 444 | * @api |
||
| 445 | * |
||
| 446 | * @param int|PulseUser $user The author of this post |
||
| 447 | * @param int|Pulse $pulse The Pulse to whom this update will belong to |
||
| 448 | * @param string $text The content of the update |
||
| 449 | * @param null|bool $announceToAll Whether or not to announce this update to everyone's wall |
||
| 450 | * |
||
| 451 | * @since 0.1.0 |
||
| 452 | * |
||
| 453 | * @return PulseUpdate |
||
| 454 | */ |
||
| 455 | public static function createUpdate ($user, $pulse, $text, $announceToAll = NULL) |
||
| 480 | } |
This check looks
TODOcomments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.