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:
Complex classes like Pulse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Pulse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Pulse extends SubscribableObject |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @ignore |
||
| 37 | */ |
||
| 38 | const API_PREFIX = "pulses"; |
||
| 39 | |||
| 40 | // ================================================================================================================ |
||
| 41 | // Instance Variables |
||
| 42 | // ================================================================================================================ |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The resource's URL. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $url; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The pulse's name. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $name; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The amount of updates a pulse has. |
||
| 60 | * |
||
| 61 | * @var int |
||
| 62 | */ |
||
| 63 | protected $updates_count; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The ID of the parent board. |
||
| 67 | * |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | protected $board_id; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Creation time. |
||
| 74 | * |
||
| 75 | * @var \DateTime |
||
| 76 | */ |
||
| 77 | protected $created_at; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Last update time. |
||
| 81 | * |
||
| 82 | * @var \DateTime |
||
| 83 | */ |
||
| 84 | protected $updated_at; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The ID of the group this pulse belongs to |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $group_id; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var PulseColumn[] |
||
| 95 | */ |
||
| 96 | protected $column_structure; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * An array containing all of the values a pulse has for each column |
||
| 100 | * |
||
| 101 | * @var mixed |
||
| 102 | */ |
||
| 103 | protected $raw_column_values; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * An array containing objects extended from PulseColumnValue storing all of the values for each column |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $column_values; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The common URL path for retrieving objects relating a pulse such as subscribers, notes, or updates |
||
| 114 | * |
||
| 115 | * @var string |
||
| 116 | */ |
||
| 117 | private $urlSyntax = "%s/%s/%s.json"; |
||
| 118 | |||
| 119 | // ================================================================================================================ |
||
| 120 | // Overloaded functions |
||
| 121 | // ================================================================================================================ |
||
| 122 | |||
| 123 | 70 | protected function initializeValues () |
|
| 129 | |||
| 130 | // ================================================================================================================ |
||
| 131 | // Getter functions |
||
| 132 | // ================================================================================================================ |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The resource's URL. |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | 1 | public function getUrl () |
|
| 143 | |||
| 144 | /** |
||
| 145 | * The pulse's name. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | 2 | public function getName () |
|
| 153 | |||
| 154 | /** |
||
| 155 | * The amount of updates a pulse has. |
||
| 156 | * |
||
| 157 | * @return int |
||
| 158 | */ |
||
| 159 | 1 | public function getUpdatesCount () |
|
| 163 | |||
| 164 | /** |
||
| 165 | * The ID of the parent board. |
||
| 166 | * |
||
| 167 | * @return int |
||
| 168 | */ |
||
| 169 | 40 | public function getBoardId () |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Creation time. |
||
| 176 | * |
||
| 177 | * @return \DateTime |
||
| 178 | */ |
||
| 179 | 1 | public function getCreatedAt () |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Last update time. |
||
| 188 | * |
||
| 189 | * @return \DateTime |
||
| 190 | */ |
||
| 191 | 1 | public function getUpdatedAt () |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Get the ID of the group this Pulse is a part of. If this value is not available, an API call will be made to |
||
| 200 | * find the group ID via brute force. |
||
| 201 | * |
||
| 202 | * **Note** The group ID is cached if it is not available. To update the cached value, use $forceFetch to force an |
||
| 203 | * API call to get a new value. |
||
| 204 | * |
||
| 205 | * **Warning** An API call is always slower than using the cached value. |
||
| 206 | * |
||
| 207 | * @param bool $forceFetch Force an API call to get an updated group ID if it has been changed |
||
| 208 | * |
||
| 209 | * @since 0.1.0 |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | 1 | public function getGroupId ($forceFetch = false) |
|
| 231 | |||
| 232 | // ================================================================================================================ |
||
| 233 | // Pulse functions |
||
| 234 | // ================================================================================================================ |
||
| 235 | |||
| 236 | private static function editPulseNameCall ($pulseID, $newName) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Edit the name of a Pulse |
||
| 248 | * |
||
| 249 | * **Tip:** If you do not already have a Pulse object created or given to you, calling this function makes one less |
||
| 250 | * API call than creating a Pulse object and then calling `editName()` on that instance. |
||
| 251 | * |
||
| 252 | * @param int $pulseID |
||
| 253 | * @param string $newName |
||
| 254 | * |
||
| 255 | * @since 0.3.0 |
||
| 256 | * |
||
| 257 | * @return Pulse |
||
| 258 | */ |
||
| 259 | public static function editPulseNameByID ($pulseID, $newName) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Edit the name of the pulse |
||
| 268 | * |
||
| 269 | * **Tip:** Calling `Pulse::editPulseNameByID()` makes one less API call than creating a Pulse object. You should |
||
| 270 | * only call this function if you already have a Pulse instance created for other purposes or it has been given to |
||
| 271 | * you. |
||
| 272 | * |
||
| 273 | * @api |
||
| 274 | * @param string $title |
||
| 275 | * @since 0.1.0 |
||
| 276 | */ |
||
| 277 | public function editName($title) |
||
| 282 | |||
| 283 | private static function archivePulseCall ($pulseID) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Archive a pulse by its ID |
||
| 295 | * |
||
| 296 | * @api |
||
| 297 | * @param int $pulseID |
||
| 298 | * @since 0.3.0 |
||
| 299 | * @return Pulse The pulse that was deleted |
||
| 300 | */ |
||
| 301 | public static function archivePulseByID ($pulseID) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Archive the current pulse |
||
| 310 | * |
||
| 311 | * This is the equivalent of a soft delete and can be restored from the DaPulse website. |
||
| 312 | * |
||
| 313 | * @api |
||
| 314 | * @since 0.3.0 |
||
| 315 | */ |
||
| 316 | public function archivePulse () |
||
| 321 | |||
| 322 | private static function deletePulseCall ($pulseID) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Delete a Pulse by its ID |
||
| 331 | * |
||
| 332 | * @api |
||
| 333 | * @param int $pulseID |
||
| 334 | * @since 0.3.0 |
||
| 335 | * @return Pulse |
||
| 336 | */ |
||
| 337 | public static function deletePulseByID ($pulseID) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Delete the current Pulse |
||
| 348 | * |
||
| 349 | * @api |
||
| 350 | * @throws \allejo\DaPulse\Exceptions\InvalidObjectException |
||
| 351 | */ |
||
| 352 | public function deletePulse () |
||
| 361 | |||
| 362 | public function duplicatePulse ($groupId = NULL, $ownerId = NULL) |
||
| 380 | |||
| 381 | View Code Duplication | private function pulseInjection (&$result) |
|
| 390 | |||
| 391 | // ================================================================================================================ |
||
| 392 | // Column data functions |
||
| 393 | // ================================================================================================================ |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Access a color type column value belonging to this pulse in order to read it or modify. |
||
| 397 | * |
||
| 398 | * This function should only be used to access color type values; an exception will be thrown otherwise. |
||
| 399 | * |
||
| 400 | * @api |
||
| 401 | * |
||
| 402 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
| 403 | * |
||
| 404 | * @since 0.1.0 |
||
| 405 | * |
||
| 406 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
| 407 | * @throws InvalidColumnException The specified column is not a "color" type column |
||
| 408 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
| 409 | 9 | * by this library or the DaPulse API. |
|
| 410 | * |
||
| 411 | 9 | * @return PulseColumnStatusValue A column object with access to its contents |
|
| 412 | */ |
||
| 413 | public function getStatusColumn ($columnId) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Access a date type column value belonging to this pulse in order to read it or modify. |
||
| 420 | * |
||
| 421 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
| 422 | * |
||
| 423 | * @api |
||
| 424 | * |
||
| 425 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
| 426 | * |
||
| 427 | * @since 0.1.0 |
||
| 428 | * |
||
| 429 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
| 430 | * @throws InvalidColumnException The specified column is not a "date" type column |
||
| 431 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
| 432 | 4 | * by this library or the DaPulse API. |
|
| 433 | * |
||
| 434 | 4 | * @return PulseColumnDateValue A column object with access to its contents |
|
| 435 | */ |
||
| 436 | public function getDateColumn ($columnId) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Access a numeric type column value belonging to this pulse in order to read it or modify. |
||
| 443 | * |
||
| 444 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
| 445 | * |
||
| 446 | * @api |
||
| 447 | * |
||
| 448 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
| 449 | * |
||
| 450 | * @since 0.2.0 |
||
| 451 | * |
||
| 452 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
| 453 | * @throws InvalidColumnException The specified column is not a "numeric" type column |
||
| 454 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
| 455 | 5 | * by this library or the DaPulse API. |
|
| 456 | * |
||
| 457 | 5 | * @return PulseColumnNumericValue A column object with access to its contents |
|
| 458 | */ |
||
| 459 | public function getNumericColumn ($columnId) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Access a person type column value belonging to this pulse in order to read it or modify. |
||
| 466 | * |
||
| 467 | * This function should only be used to access person type values; an exception will be thrown otherwise. |
||
| 468 | * |
||
| 469 | * @api |
||
| 470 | * |
||
| 471 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
| 472 | * |
||
| 473 | * @since 0.1.0 |
||
| 474 | * |
||
| 475 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
| 476 | 40 | * @throws InvalidColumnException The specified column is not a "person" type column |
|
| 477 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
| 478 | 40 | * by this library or the DaPulse API. |
|
| 479 | * |
||
| 480 | 40 | * @return PulseColumnPersonValue A column object with access to its contents |
|
| 481 | 40 | */ |
|
| 482 | public function getPersonColumn ($columnId) |
||
| 486 | 40 | ||
| 487 | /** |
||
| 488 | 29 | * Access a text type column value belonging to this pulse in order to read it or modify. |
|
| 489 | * |
||
| 490 | 29 | * This function should only be used to access text type values; an exception will be thrown otherwise. |
|
| 491 | * |
||
| 492 | * @api |
||
| 493 | * |
||
| 494 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
| 495 | 29 | * |
|
| 496 | * @since 0.1.0 |
||
| 497 | 1 | ||
| 498 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
| 499 | 1 | * @throws InvalidColumnException The specified column is not a "text" type column |
|
| 500 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
| 501 | * by this library or the DaPulse API. |
||
| 502 | * |
||
| 503 | * @return PulseColumnTextValue A column object with access to its contents |
||
| 504 | 28 | */ |
|
| 505 | public function getTextColumn ($columnId) |
||
| 509 | 11 | ||
| 510 | /** |
||
| 511 | 11 | * Access a timeline type column value belonging to this pulse in order to read it or modify. |
|
| 512 | * |
||
| 513 | 1 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
|
| 514 | * |
||
| 515 | * @api |
||
| 516 | * |
||
| 517 | 38 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
|
| 518 | 38 | * |
|
| 519 | 38 | * @since 0.2.1 |
|
| 520 | * |
||
| 521 | 38 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
|
| 522 | * @throws InvalidColumnException The specified column is not a "numeric" type column |
||
| 523 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
| 524 | 38 | * by this library or the DaPulse API. |
|
| 525 | * |
||
| 526 | * @return PulseColumnTimelineValue A column object with access to its contents |
||
| 527 | */ |
||
| 528 | public function getTimelineColumn ($columnId) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Build a pulse's column object if it doesn't exist or return the existing column. |
||
| 535 | * |
||
| 536 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column |
||
| 537 | * title |
||
| 538 | * @param string $columnType The type of column being accessed: 'text', 'color', 'person', 'numeric', or 'date' |
||
| 539 | * |
||
| 540 | * @since 0.1.0 |
||
| 541 | * |
||
| 542 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
| 543 | * @throws InvalidColumnException The specified column is not the same type as specified in `$columnType` |
||
| 544 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
| 545 | * by this library or the DaPulse API. |
||
| 546 | * |
||
| 547 | * @return PulseColumnValue The returned object will be a child of this abstract class. |
||
| 548 | */ |
||
| 549 | private function getColumn ($columnId, $columnType) |
||
| 599 | |||
| 600 | 20 | // ================================================================================================================ |
|
| 601 | // Notes functions |
||
| 602 | // ================================================================================================================ |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Create a new note in this project |
||
| 606 | * |
||
| 607 | * @api |
||
| 608 | * |
||
| 609 | * @param string $title The title of the note |
||
| 610 | * @param string $content The body of the note |
||
| 611 | * @param bool $ownersOnly Set to true if only pulse owners can edit this note. |
||
| 612 | * @param int|null $userId The id of the user to be marked as the note's last updater |
||
| 613 | * @param bool $createUpdate Indicates whether to create an update on the pulse notifying subscribers on the |
||
| 614 | * changes (required user_id to be set). |
||
| 615 | * |
||
| 616 | * @since 0.1.0 |
||
| 617 | * @return PulseNote |
||
| 618 | */ |
||
| 619 | public function addNote ($title, $content, $ownersOnly = false, $userId = NULL, $createUpdate = false) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Return all of the notes belonging to this project |
||
| 644 | * |
||
| 645 | * @api |
||
| 646 | * @since 0.1.0 |
||
| 647 | * @return PulseNote[] |
||
| 648 | */ |
||
| 649 | public function getNotes () |
||
| 655 | |||
| 656 | // ================================================================================================================ |
||
| 657 | // Updates functions |
||
| 658 | // ================================================================================================================ |
||
| 659 | |||
| 660 | /** |
||
| 661 | * Get all of the updates that belong to this Pulse in reverse chronological order |
||
| 662 | * |
||
| 663 | * @api |
||
| 664 | * |
||
| 665 | * @since 0.1.0 |
||
| 666 | * |
||
| 667 | * @return PulseUpdate[] |
||
| 668 | */ |
||
| 669 | public function getUpdates () |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Create an update for the current Pulse |
||
| 678 | * |
||
| 679 | * @api |
||
| 680 | * |
||
| 681 | * @param int|PulseUser $user |
||
| 682 | * @param string $text |
||
| 683 | * @param null|bool $announceToAll |
||
| 684 | * |
||
| 685 | * @since 0.2.2 A PulseUpdate object is returned containing the information of the newly created Update |
||
| 686 | * @since 0.1.0 |
||
| 687 | * |
||
| 688 | * @return PulseUpdate |
||
| 689 | */ |
||
| 690 | public function createUpdate ($user, $text, $announceToAll = NULL) |
||
| 694 | |||
| 695 | // ================================================================================================================ |
||
| 696 | // Static functions |
||
| 697 | // ================================================================================================================ |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Get all of the pulses that belong to the organization across all boards. |
||
| 701 | * |
||
| 702 | * To modify the amount of data returned with pagination, use the following values in the array to configure your |
||
| 703 | * pagination or offsets. |
||
| 704 | * |
||
| 705 | * ```php |
||
| 706 | * $params = array( |
||
| 707 | * "page" => 1, // (int) Page offset to fetch |
||
| 708 | * "per_page" => 10, // (int) Number of results per page |
||
| 709 | * "offset" => 5, // (int) Instead of starting at result 0, start counting from result 5 |
||
| 710 | * "order_by_latest" => true // (bool) Order the pulses with the most recent first |
||
| 711 | * ); |
||
| 712 | * ``` |
||
| 713 | * |
||
| 714 | * @api |
||
| 715 | * |
||
| 716 | * @param array $params GET parameters passed to with the query to modify the data returned. |
||
| 717 | * |
||
| 718 | * @since 0.1.0 |
||
| 719 | * @return Pulse[] |
||
| 720 | */ |
||
| 721 | public static function getPulses ($params = array()) |
||
| 727 | } |
||
| 728 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..