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 |
||
31 | class Pulse extends SubscribableObject |
||
32 | { |
||
33 | /** |
||
34 | * @ignore |
||
35 | */ |
||
36 | const API_PREFIX = "pulses"; |
||
37 | |||
38 | // ================================================================================================================ |
||
39 | // Instance Variables |
||
40 | // ================================================================================================================ |
||
41 | |||
42 | /** |
||
43 | * The resource's URL. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $url; |
||
48 | |||
49 | /** |
||
50 | * The pulse's name. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $name; |
||
55 | |||
56 | /** |
||
57 | * The amount of updates a pulse has. |
||
58 | * |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $updates_count; |
||
62 | |||
63 | /** |
||
64 | * The ID of the parent board. |
||
65 | * |
||
66 | * @var int |
||
67 | */ |
||
68 | protected $board_id; |
||
69 | |||
70 | /** |
||
71 | * Creation time. |
||
72 | * |
||
73 | * @var \DateTime |
||
74 | */ |
||
75 | protected $created_at; |
||
76 | |||
77 | /** |
||
78 | * Last update time. |
||
79 | * |
||
80 | * @var \DateTime |
||
81 | */ |
||
82 | protected $updated_at; |
||
83 | |||
84 | /** |
||
85 | * The ID of the group this pulse belongs to |
||
86 | * |
||
87 | * @var string |
||
88 | */ |
||
89 | protected $group_id; |
||
90 | |||
91 | /** |
||
92 | * @var PulseColumn[] |
||
93 | */ |
||
94 | protected $column_structure; |
||
95 | |||
96 | /** |
||
97 | * An array containing all of the values a pulse has for each column |
||
98 | * |
||
99 | * @var mixed |
||
100 | */ |
||
101 | protected $raw_column_values; |
||
102 | |||
103 | /** |
||
104 | * An array containing objects extended from PulseColumnValue storing all of the values for each column |
||
105 | * |
||
106 | * @var array |
||
107 | */ |
||
108 | protected $column_values; |
||
109 | |||
110 | /** |
||
111 | * The common URL path for retrieving objects relating a pulse such as subscribers, notes, or updates |
||
112 | * |
||
113 | * @var string |
||
114 | */ |
||
115 | private $urlSyntax = "%s/%s/%s.json"; |
||
116 | |||
117 | // ================================================================================================================ |
||
118 | // Overloaded functions |
||
119 | // ================================================================================================================ |
||
120 | |||
121 | protected function initializeValues () |
||
127 | 70 | ||
128 | 70 | // ================================================================================================================ |
|
129 | // Getter functions |
||
130 | // ================================================================================================================ |
||
131 | |||
132 | /** |
||
133 | * The resource's URL. |
||
134 | * |
||
135 | * @api |
||
136 | * |
||
137 | * @since 0.1.0 |
||
138 | * |
||
139 | 1 | * @return string |
|
140 | */ |
||
141 | 1 | public function getUrl () |
|
147 | |||
148 | /** |
||
149 | 2 | * The pulse's name. |
|
150 | * |
||
151 | 2 | * @api |
|
152 | * |
||
153 | * @since 0.1.0 |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | public function getName () |
||
163 | |||
164 | /** |
||
165 | * The amount of updates a pulse has. |
||
166 | * |
||
167 | * @api |
||
168 | * |
||
169 | 40 | * @since 0.1.0 |
|
170 | * |
||
171 | 40 | * @return int |
|
172 | */ |
||
173 | public function getUpdatesCount () |
||
179 | 1 | ||
180 | /** |
||
181 | 1 | * The ID of the parent board. |
|
182 | * |
||
183 | 1 | * @api |
|
184 | * |
||
185 | * @since 0.1.0 |
||
186 | * |
||
187 | * @return int |
||
188 | */ |
||
189 | public function getBoardId () |
||
195 | 1 | ||
196 | /** |
||
197 | * Creation time. |
||
198 | * |
||
199 | * @api |
||
200 | * |
||
201 | * @since 0.1.0 |
||
202 | * |
||
203 | * @return \DateTime |
||
204 | */ |
||
205 | public function getCreatedAt () |
||
212 | 1 | ||
213 | /** |
||
214 | 1 | * Last update time. |
|
215 | * |
||
216 | 1 | * @api |
|
217 | 1 | * |
|
218 | * @since 0.1.0 |
||
219 | 1 | * |
|
220 | * @return \DateTime |
||
221 | 1 | */ |
|
222 | public function getUpdatedAt () |
||
229 | 1 | ||
230 | /** |
||
231 | * 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 |
||
232 | * find the group ID via brute force. |
||
233 | * |
||
234 | * **Note** The group ID is cached if it is not available. To update the cached value, use $forceFetch to force an |
||
235 | * API call to get a new value. |
||
236 | * |
||
237 | * **Warning** An API call is always slower than using the cached value. |
||
238 | * |
||
239 | * @api |
||
240 | * |
||
241 | * @param bool $forceFetch Force an API call to get an updated group ID if it has been changed |
||
242 | * |
||
243 | * @since 0.1.0 |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getGroupId ($forceFetch = false) |
||
268 | |||
269 | // ================================================================================================================ |
||
270 | // Pulse functions |
||
271 | // ================================================================================================================ |
||
272 | |||
273 | /** |
||
274 | * Edit the name of the pulse |
||
275 | * |
||
276 | * @api |
||
277 | * |
||
278 | * @param string $title |
||
279 | * |
||
280 | * @since 0.1.0 |
||
281 | */ |
||
282 | public function editName ($title) |
||
292 | |||
293 | /** |
||
294 | * Archive the current pulse |
||
295 | * |
||
296 | * This is the equivalent of a soft delete and can be restored from the DaPulse website. |
||
297 | * |
||
298 | * @api |
||
299 | * |
||
300 | * @since 0.1.0 |
||
301 | */ |
||
302 | public function archivePulse () |
||
312 | |||
313 | /** |
||
314 | * Delete the current Pulse |
||
315 | * |
||
316 | * @api |
||
317 | * |
||
318 | * @since 0.1.0 |
||
319 | * |
||
320 | * @throws InvalidObjectException |
||
321 | */ |
||
322 | View Code Duplication | public function deletePulse () |
|
332 | |||
333 | public function duplicatePulse ($groupId = null, $ownerId = null) |
||
351 | |||
352 | View Code Duplication | private function pulseInjection (&$result) |
|
361 | |||
362 | // ================================================================================================================ |
||
363 | 9 | // Column data functions |
|
364 | // ================================================================================================================ |
||
365 | 9 | ||
366 | /** |
||
367 | * Access a color type column value belonging to this pulse in order to read it or modify. |
||
368 | * |
||
369 | * This function should only be used to access color type values; an exception will be thrown otherwise. |
||
370 | * |
||
371 | * @api |
||
372 | * |
||
373 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
374 | * |
||
375 | * @since 0.1.0 |
||
376 | * |
||
377 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
378 | * @throws InvalidColumnException The specified column is not a "color" type column |
||
379 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
380 | * by this library or the DaPulse API. |
||
381 | * |
||
382 | * @return PulseColumnStatusValue A column object with access to its contents |
||
383 | */ |
||
384 | public function getStatusColumn ($columnId) |
||
388 | 7 | ||
389 | /** |
||
390 | * Access a date type column value belonging to this pulse in order to read it or modify. |
||
391 | * |
||
392 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
393 | * |
||
394 | * @api |
||
395 | * |
||
396 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
397 | * |
||
398 | * @since 0.1.0 |
||
399 | * |
||
400 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
401 | * @throws InvalidColumnException The specified column is not a "date" type column |
||
402 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
403 | * by this library or the DaPulse API. |
||
404 | * |
||
405 | * @return PulseColumnDateValue A column object with access to its contents |
||
406 | */ |
||
407 | public function getDateColumn ($columnId) |
||
411 | 9 | ||
412 | /** |
||
413 | * Access a numeric type column value belonging to this pulse in order to read it or modify. |
||
414 | * |
||
415 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
416 | * |
||
417 | * @api |
||
418 | * |
||
419 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
420 | * |
||
421 | * @since 0.2.0 |
||
422 | * |
||
423 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
424 | * @throws InvalidColumnException The specified column is not a "numeric" type column |
||
425 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
426 | * by this library or the DaPulse API. |
||
427 | * |
||
428 | * @return PulseColumnNumericValue A column object with access to its contents |
||
429 | */ |
||
430 | public function getNumericColumn ($columnId) |
||
434 | 4 | ||
435 | /** |
||
436 | * Access a person type column value belonging to this pulse in order to read it or modify. |
||
437 | * |
||
438 | * This function should only be used to access person type values; an exception will be thrown otherwise. |
||
439 | * |
||
440 | * @api |
||
441 | * |
||
442 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
443 | * |
||
444 | * @since 0.1.0 |
||
445 | * |
||
446 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
447 | * @throws InvalidColumnException The specified column is not a "person" type column |
||
448 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
449 | * by this library or the DaPulse API. |
||
450 | * |
||
451 | * @return PulseColumnPersonValue A column object with access to its contents |
||
452 | */ |
||
453 | public function getPersonColumn ($columnId) |
||
457 | 5 | ||
458 | /** |
||
459 | * Access a text type column value belonging to this pulse in order to read it or modify. |
||
460 | * |
||
461 | * This function should only be used to access text type values; an exception will be thrown otherwise. |
||
462 | * |
||
463 | * @api |
||
464 | * |
||
465 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
466 | * |
||
467 | * @since 0.1.0 |
||
468 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
469 | * @throws InvalidColumnException The specified column is not a "text" type column |
||
470 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
471 | * by this library or the DaPulse API. |
||
472 | * |
||
473 | * @return PulseColumnTextValue A column object with access to its contents |
||
474 | */ |
||
475 | public function getTextColumn ($columnId) |
||
479 | |||
480 | 40 | /** |
|
481 | 40 | * Access a timeline type column value belonging to this pulse in order to read it or modify. |
|
482 | * |
||
483 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
484 | * |
||
485 | * @api |
||
486 | 40 | * |
|
487 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
488 | 29 | * |
|
489 | * @since 0.2.1 |
||
490 | 29 | * |
|
491 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
492 | * @throws InvalidColumnException The specified column is not a "numeric" type column |
||
493 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
494 | * by this library or the DaPulse API. |
||
495 | 29 | * |
|
496 | * @return PulseColumnTimelineValue A column object with access to its contents |
||
497 | 1 | */ |
|
498 | public function getTimelineColumn ($columnId) |
||
502 | |||
503 | /** |
||
504 | 28 | * Build a pulse's column object if it doesn't exist or return the existing column. |
|
505 | * |
||
506 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column |
||
507 | * title |
||
508 | 11 | * @param string $columnType The type of column being accessed: 'text', 'color', 'person', 'numeric', or 'date' |
|
509 | 11 | * |
|
510 | * @since 0.1.0 |
||
511 | 11 | * |
|
512 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
513 | 1 | * @throws InvalidColumnException The specified column is not the same type as specified in `$columnType` |
|
514 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
515 | * by this library or the DaPulse API. |
||
516 | * |
||
517 | 38 | * @return PulseColumnValue The returned object will be a child of this abstract class. |
|
518 | 38 | */ |
|
519 | 38 | private function getColumn ($columnId, $columnType) |
|
569 | |||
570 | // ================================================================================================================ |
||
571 | // Notes functions |
||
572 | // ================================================================================================================ |
||
573 | |||
574 | /** |
||
575 | * Create a new note in this project |
||
576 | * |
||
577 | * @api |
||
578 | * |
||
579 | * @param string $title The title of the note |
||
580 | * @param string $content The body of the note |
||
581 | * @param bool $ownersOnly Set to true if only pulse owners can edit this note. |
||
582 | * @param int|null $user The id of the user to be marked as the note's last updater |
||
583 | * @param bool $createUpdate Indicates whether to create an update on the pulse notifying subscribers on the |
||
584 | * changes (required user_id to be set). |
||
585 | * |
||
586 | * @throws \InvalidArgumentException if $createUpdate is true and $user is null or $user is not a valid user ID or |
||
587 | * PulseUser object |
||
588 | * |
||
589 | * @since 0.1.0 |
||
590 | * |
||
591 | * @return PulseNote |
||
592 | */ |
||
593 | public function addNote ($title, $content, $ownersOnly = false, $user = null, $createUpdate = false) |
||
620 | |||
621 | /** |
||
622 | * Return all of the notes belonging to this project |
||
623 | * |
||
624 | * @api |
||
625 | * |
||
626 | * @since 0.1.0 |
||
627 | * |
||
628 | * @return PulseNote[] |
||
629 | */ |
||
630 | public function getNotes () |
||
636 | |||
637 | // ================================================================================================================ |
||
638 | // Updates functions |
||
639 | // ================================================================================================================ |
||
640 | |||
641 | /** |
||
642 | * Get all of the updates that belong to this Pulse in reverse chronological order |
||
643 | * |
||
644 | * @api |
||
645 | * |
||
646 | * @since 0.1.0 |
||
647 | * |
||
648 | * @return PulseUpdate[] |
||
649 | */ |
||
650 | public function getUpdates () |
||
656 | |||
657 | /** |
||
658 | * Create an update for the current Pulse |
||
659 | * |
||
660 | * @api |
||
661 | * |
||
662 | * @param int|PulseUser $user |
||
663 | * @param string $text |
||
664 | * @param null|bool $announceToAll |
||
665 | * |
||
666 | * @since 0.3.0 A PulseUpdate object is returned containing the information of the newly created Update |
||
667 | * @since 0.1.0 |
||
668 | * |
||
669 | * @return PulseUpdate |
||
670 | */ |
||
671 | public function createUpdate ($user, $text, $announceToAll = null) |
||
675 | |||
676 | // ================================================================================================================ |
||
677 | // Static functions |
||
678 | // ================================================================================================================ |
||
679 | |||
680 | /** |
||
681 | * Get all of the pulses that belong to the organization across all boards. |
||
682 | * |
||
683 | * To modify the amount of data returned with pagination, use the following values in the array to configure your |
||
684 | * pagination or offsets. |
||
685 | * |
||
686 | * ```php |
||
687 | * $params = array( |
||
688 | * "page" => 1, // (int) Page offset to fetch |
||
689 | * "per_page" => 10, // (int) Number of results per page |
||
690 | * "offset" => 5, // (int) Instead of starting at result 0, start counting from result 5 |
||
691 | * "order_by_latest" => true // (bool) Order the pulses with the most recent first |
||
692 | * ); |
||
693 | * ``` |
||
694 | * |
||
695 | * @api |
||
696 | * |
||
697 | * @param array $params GET parameters passed to with the query to modify the data returned. |
||
698 | * |
||
699 | * @since 0.1.0 |
||
700 | * |
||
701 | * @return Pulse[] |
||
702 | */ |
||
703 | public static function getPulses ($params = []) |
||
709 | } |
||
710 |
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..