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 |
||
30 | class Pulse extends SubscribableObject |
||
31 | { |
||
32 | /** |
||
33 | * @ignore |
||
34 | */ |
||
35 | const API_PREFIX = "pulses"; |
||
36 | |||
37 | // ================================================================================================================ |
||
38 | // Instance Variables |
||
39 | // ================================================================================================================ |
||
40 | |||
41 | /** |
||
42 | * The resource's URL. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $url; |
||
47 | |||
48 | /** |
||
49 | * The pulse's name. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $name; |
||
54 | |||
55 | /** |
||
56 | * The board's subscribers. |
||
57 | * |
||
58 | * @var PulseUser[] |
||
59 | */ |
||
60 | protected $subscribers; |
||
61 | |||
62 | /** |
||
63 | * The amount of updates a pulse has. |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $updates_count; |
||
68 | |||
69 | /** |
||
70 | * The ID of the parent board. |
||
71 | * |
||
72 | * @var int |
||
73 | */ |
||
74 | protected $board_id; |
||
75 | |||
76 | /** |
||
77 | * Creation time. |
||
78 | * |
||
79 | * @var \DateTime |
||
80 | */ |
||
81 | protected $created_at; |
||
82 | |||
83 | /** |
||
84 | * Last update time. |
||
85 | * |
||
86 | * @var \DateTime |
||
87 | */ |
||
88 | protected $updated_at; |
||
89 | |||
90 | /** |
||
91 | * The ID of the group this pulse belongs to |
||
92 | * |
||
93 | * @var string |
||
94 | */ |
||
95 | protected $group_id; |
||
96 | |||
97 | /** |
||
98 | * @var PulseColumn[] |
||
99 | */ |
||
100 | protected $column_structure; |
||
101 | |||
102 | /** |
||
103 | * An array containing all of the values a pulse has for each column |
||
104 | * |
||
105 | * @var mixed |
||
106 | */ |
||
107 | protected $raw_column_values; |
||
108 | |||
109 | /** |
||
110 | * An array containing objects extended from PulseColumnValue storing all of the values for each column |
||
111 | * |
||
112 | * @var array |
||
113 | */ |
||
114 | protected $column_values; |
||
115 | |||
116 | /** |
||
117 | * The common URL path for retrieving objects relating a pulse such as subscribers, notes, or updates |
||
118 | * |
||
119 | * @var string |
||
120 | */ |
||
121 | private $urlSyntax = "%s/%s/%s.json"; |
||
122 | |||
123 | // ================================================================================================================ |
||
124 | // Overloaded functions |
||
125 | // ================================================================================================================ |
||
126 | |||
127 | protected function initializeValues () |
||
133 | |||
134 | // ================================================================================================================ |
||
135 | // Getter functions |
||
136 | // ================================================================================================================ |
||
137 | |||
138 | /** |
||
139 | * The resource's URL. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getUrl () |
||
147 | |||
148 | /** |
||
149 | * The pulse's name. |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getName () |
||
157 | |||
158 | /** |
||
159 | * The amount of updates a pulse has. |
||
160 | * |
||
161 | * @return int |
||
162 | */ |
||
163 | public function getUpdatesCount () |
||
167 | |||
168 | /** |
||
169 | * The ID of the parent board. |
||
170 | * |
||
171 | * @return int |
||
172 | */ |
||
173 | public function getBoardId () |
||
177 | |||
178 | /** |
||
179 | * Creation time. |
||
180 | * |
||
181 | * @return \DateTime |
||
182 | */ |
||
183 | public function getCreatedAt () |
||
189 | |||
190 | /** |
||
191 | * Last update time. |
||
192 | * |
||
193 | * @return \DateTime |
||
194 | */ |
||
195 | public function getUpdatedAt () |
||
201 | |||
202 | /** |
||
203 | * 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 |
||
204 | * find the group ID via brute force. |
||
205 | * |
||
206 | * **Note** The group ID is cached if it is not available. To update the cached value, use $forceFetch to force an |
||
207 | * API call to get a new value. |
||
208 | * |
||
209 | * **Warning** An API call is always slower than using the cached value. |
||
210 | * |
||
211 | * @param bool $forceFetch Force an API call to get an updated group ID if it has been changed |
||
212 | * |
||
213 | * @since 0.1.0 |
||
214 | * @return string |
||
215 | */ |
||
216 | public function getGroupId ($forceFetch = false) |
||
235 | |||
236 | // ================================================================================================================ |
||
237 | // Pulse functions |
||
238 | // ================================================================================================================ |
||
239 | |||
240 | /** |
||
241 | * Delete the current Pulse |
||
242 | * |
||
243 | * @api |
||
244 | * @throws \allejo\DaPulse\Exceptions\InvalidObjectException |
||
245 | */ |
||
246 | View Code Duplication | public function deletePulse () |
|
256 | |||
257 | public function duplicatePulse ($groupId = NULL, $ownerId = NULL) |
||
275 | |||
276 | View Code Duplication | private function pulseInjection (&$result) |
|
285 | |||
286 | // ================================================================================================================ |
||
287 | // Column data functions |
||
288 | // ================================================================================================================ |
||
289 | |||
290 | /** |
||
291 | * Access a pulse's specific column to either access their value or to modify the value. |
||
292 | * |
||
293 | * See the related functions to see the appropriate replacements. |
||
294 | * |
||
295 | * @todo This function only exists for legacy applications. Remove in 0.1.1 |
||
296 | * |
||
297 | * @api |
||
298 | * @deprecated 0.0.1 This function will be removed by 0.1.1. New stricter functions are available |
||
299 | * |
||
300 | * @param string $columnId The ID of the column to access. It's typically a slugified version of the column title |
||
301 | * |
||
302 | * @see Pulse::getStatusColumn() getColorColumn() |
||
303 | * @see Pulse::getDateColumn() getDateColumn() |
||
304 | * @see Pulse::getPersonColumn() getPersonColumn() |
||
305 | * @see Pulse::getTextColumn() getTextColumn() |
||
306 | * @since 0.1.0 |
||
307 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
308 | * by this library or the DaPulse API. |
||
309 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
310 | * @return PulseColumnValue The returned object will be a child of this abstract class. |
||
311 | */ |
||
312 | public function getColumnValue ($columnId) |
||
330 | |||
331 | /** |
||
332 | * Access a color type column value belonging to this pulse in order to read it or modify. |
||
333 | * |
||
334 | * This function should only be used to access color type values; an exception will be thrown otherwise. |
||
335 | * |
||
336 | * @api |
||
337 | * |
||
338 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
339 | * |
||
340 | * @since 0.1.0 |
||
341 | * @throws InvalidColumnException The specified column is not a "color" type column |
||
342 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
343 | * by this library or the DaPulse API. |
||
344 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
345 | * @return PulseColumnStatusValue A column object with access to its contents |
||
346 | */ |
||
347 | public function getStatusColumn ($columnId) |
||
351 | |||
352 | /** |
||
353 | * Access a date type column value belonging to this pulse in order to read it or modify. |
||
354 | * |
||
355 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
356 | * |
||
357 | * @api |
||
358 | * |
||
359 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
360 | * |
||
361 | * @since 0.1.0 |
||
362 | * @throws InvalidColumnException The specified column is not a "date" type column |
||
363 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
364 | * by this library or the DaPulse API. |
||
365 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
366 | * @return PulseColumnDateValue A column object with access to its contents |
||
367 | */ |
||
368 | public function getDateColumn ($columnId) |
||
372 | |||
373 | /** |
||
374 | * Access a person type column value belonging to this pulse in order to read it or modify. |
||
375 | * |
||
376 | * This function should only be used to access person type values; an exception will be thrown otherwise. |
||
377 | * |
||
378 | * @api |
||
379 | * |
||
380 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
381 | * |
||
382 | * @since 0.1.0 |
||
383 | * @throws InvalidColumnException The specified column is not a "person" type column |
||
384 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
385 | * by this library or the DaPulse API. |
||
386 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
387 | * @return PulseColumnPersonValue A column object with access to its contents |
||
388 | */ |
||
389 | public function getPersonColumn ($columnId) |
||
393 | |||
394 | /** |
||
395 | * Access a text type column value belonging to this pulse in order to read it or modify. |
||
396 | * |
||
397 | * This function should only be used to access text type values; an exception will be thrown otherwise. |
||
398 | * |
||
399 | * @api |
||
400 | * |
||
401 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
402 | * |
||
403 | * @since 0.1.0 |
||
404 | * @throws InvalidColumnException The specified column is not a "text" type column |
||
405 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
406 | * by this library or the DaPulse API. |
||
407 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
408 | * @return PulseColumnTextValue A column object with access to its contents |
||
409 | */ |
||
410 | public function getTextColumn ($columnId) |
||
414 | |||
415 | /** |
||
416 | * Build a pulse's column object if it doesn't exist or return the existing column. |
||
417 | * |
||
418 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column |
||
419 | * title |
||
420 | * @param string $columnType The type of column being accessed: 'text', 'color', 'person', or 'date' |
||
421 | * |
||
422 | * @since 0.1.0 |
||
423 | * |
||
424 | * @throws InvalidColumnException The specified column is not the same type as specified in `$columnType` |
||
425 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
426 | * by this library or the DaPulse API. |
||
427 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
428 | * |
||
429 | * @return PulseColumnValue The returned object will be a child of this abstract class. |
||
430 | */ |
||
431 | private function getColumn ($columnId, $columnType) |
||
481 | |||
482 | // ================================================================================================================ |
||
483 | // Notes functions |
||
484 | // ================================================================================================================ |
||
485 | |||
486 | /** |
||
487 | * Create a new note in this project |
||
488 | * |
||
489 | * @api |
||
490 | * |
||
491 | * @param string $title The title of the note |
||
492 | * @param string $content The body of the note |
||
493 | * @param bool $ownersOnly Set to true if only pulse owners can edit this note. |
||
494 | * @param int|null $userId The id of the user to be marked as the note's last updater |
||
495 | * @param bool $createUpdate Indicates whether to create an update on the pulse notifying subscribers on the |
||
496 | * changes (required user_id to be set). |
||
497 | * |
||
498 | * @since 0.1.0 |
||
499 | * @return PulseNote |
||
500 | */ |
||
501 | public function addNote ($title, $content, $ownersOnly = false, $userId = NULL, $createUpdate = false) |
||
523 | |||
524 | /** |
||
525 | * Return all of the notes belonging to this project |
||
526 | * |
||
527 | * @api |
||
528 | * @since 0.1.0 |
||
529 | * @return PulseNote[] |
||
530 | */ |
||
531 | public function getNotes () |
||
537 | |||
538 | // ================================================================================================================ |
||
539 | // Updates functions |
||
540 | // ================================================================================================================ |
||
541 | |||
542 | /** |
||
543 | * Get all of the updates that belong this Pulse |
||
544 | * |
||
545 | * @api |
||
546 | * @since 0.1.0 |
||
547 | * @return PulseUpdate[] |
||
548 | */ |
||
549 | public function getUpdates () |
||
555 | |||
556 | /** |
||
557 | * Create an update for the current Pulse |
||
558 | * |
||
559 | * @api |
||
560 | * |
||
561 | * @param int|PulseUser $user |
||
562 | * @param string $text |
||
563 | * @param null|bool $announceToAll |
||
564 | * |
||
565 | * @since 0.1.0 |
||
566 | */ |
||
567 | public function createUpdate ($user, $text, $announceToAll = NULL) |
||
571 | |||
572 | // ================================================================================================================ |
||
573 | // Static functions |
||
574 | // ================================================================================================================ |
||
575 | |||
576 | /** |
||
577 | * Get all of the pulses that belong to the organization across all boards. |
||
578 | * |
||
579 | * To modify the amount of data returned with pagination, use the following values in the array to configure your |
||
580 | * pagination or offsets. |
||
581 | * |
||
582 | * ```php |
||
583 | * $params = array( |
||
584 | * "page" => 1, // (int) Page offset to fetch |
||
585 | * "per_page" => 10, // (int) Number of results per page |
||
586 | * "offset" => 5, // (int) Instead of starting at result 0, start counting from result 5 |
||
587 | * "order_by_latest" => true // (bool) Order the pulses with the most recent first |
||
588 | * ); |
||
589 | * ``` |
||
590 | * |
||
591 | * @api |
||
592 | * |
||
593 | * @param array $params GET parameters passed to with the query to modify the data returned. |
||
594 | * |
||
595 | * @since 0.1.0 |
||
596 | * @return Pulse[] |
||
597 | */ |
||
598 | public static function getPulses ($params = array()) |
||
604 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.