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 |
||
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 | 46 | 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 | 23 | 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) |
|
213 | { |
||
214 | 1 | if (empty($this->group_id) || $forceFetch) |
|
215 | { |
||
216 | 1 | $parentBoard = new PulseBoard($this->board_id); |
|
217 | 1 | $pulses = $parentBoard->getPulses(); |
|
218 | |||
219 | 1 | foreach ($pulses as $pulse) |
|
220 | { |
||
221 | 1 | if ($this->getId() === $pulse->getId()) |
|
222 | { |
||
223 | 1 | $this->group_id = $pulse->getGroupId(); |
|
224 | 1 | break; |
|
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | 1 | return $this->group_id; |
|
230 | } |
||
231 | |||
232 | // ================================================================================================================ |
||
233 | // Pulse functions |
||
234 | // ================================================================================================================ |
||
235 | |||
236 | /** |
||
237 | * Edit the name of the pulse |
||
238 | * |
||
239 | * @api |
||
240 | * @param string $title |
||
241 | * @since 0.1.0 |
||
242 | */ |
||
243 | public function editName($title) |
||
244 | { |
||
245 | $editUrl = sprintf("%s/%d.json", self::apiEndpoint(), $this->getId()); |
||
246 | $postParams = array( |
||
247 | 'name' => $title |
||
248 | ); |
||
249 | |||
250 | $this->jsonResponse = self::sendPut($editUrl, $postParams); |
||
|
|||
251 | $this->assignResults(); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Archive the current pulse. |
||
256 | * |
||
257 | * This is the equivalent of a soft delete and can be restored from the DaPulse website. |
||
258 | * |
||
259 | * @api |
||
260 | * @since 0.1.0 |
||
261 | */ |
||
262 | public function archivePulse() |
||
263 | { |
||
264 | $archiveURL = sprintf("%s/%d.json", self::apiEndpoint(), $this->getId()); |
||
265 | $getParams = array( |
||
266 | 'archive' => true |
||
267 | ); |
||
268 | |||
269 | self::sendDelete($archiveURL, $getParams); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Delete the current Pulse |
||
274 | * |
||
275 | * @api |
||
276 | * @throws \allejo\DaPulse\Exceptions\InvalidObjectException |
||
277 | */ |
||
278 | View Code Duplication | public function deletePulse () |
|
279 | { |
||
280 | $this->checkInvalid(); |
||
281 | |||
282 | $deleteURL = sprintf("%s/%d.json", self::apiEndpoint(), $this->getId()); |
||
283 | |||
284 | self::sendDelete($deleteURL); |
||
285 | |||
286 | $this->deletedObject = true; |
||
287 | } |
||
288 | |||
289 | public function duplicatePulse ($groupId = NULL, $ownerId = NULL) |
||
290 | { |
||
291 | $url = sprintf("%s/%s/pulses/%s/duplicate.json", self::apiEndpoint("boards"), $this->getBoardId(), $this->getId()); |
||
292 | $postParams = array(); |
||
293 | |||
294 | if ($ownerId instanceof PulseUser) |
||
295 | { |
||
296 | $ownerId = $ownerId->getId(); |
||
297 | } |
||
298 | |||
299 | self::setIfNotNullOrEmpty($postParams, "group_id", $groupId); |
||
300 | self::setIfNotNullOrEmpty($postParams, "owner_id", $ownerId); |
||
301 | |||
302 | $result = self::sendPost($url, $postParams); |
||
303 | $this->pulseInjection($result); |
||
304 | |||
305 | return (new Pulse($result['pulse'])); |
||
306 | } |
||
307 | |||
308 | View Code Duplication | private function pulseInjection (&$result) |
|
309 | { |
||
310 | $parentBoard = new PulseBoard($this->getBoardId()); |
||
311 | |||
312 | // Inject some information so a Pulse object can survive on its own |
||
313 | $result["pulse"]["group_id"] = $result["board_meta"]["group_id"]; |
||
314 | $result["pulse"]["column_structure"] = $parentBoard->getColumns(); |
||
315 | $result["pulse"]["raw_column_values"] = $result["column_values"]; |
||
316 | } |
||
317 | |||
318 | // ================================================================================================================ |
||
319 | // Column data functions |
||
320 | // ================================================================================================================ |
||
321 | |||
322 | /** |
||
323 | * Access a color type column value belonging to this pulse in order to read it or modify. |
||
324 | * |
||
325 | * This function should only be used to access color type values; an exception will be thrown otherwise. |
||
326 | * |
||
327 | * @api |
||
328 | * |
||
329 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
330 | * |
||
331 | * @since 0.1.0 |
||
332 | * |
||
333 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
334 | * @throws InvalidColumnException The specified column is not a "color" type column |
||
335 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
336 | * by this library or the DaPulse API. |
||
337 | * |
||
338 | * @return PulseColumnStatusValue A column object with access to its contents |
||
339 | */ |
||
340 | 6 | public function getStatusColumn ($columnId) |
|
344 | |||
345 | /** |
||
346 | * Access a date type column value belonging to this pulse in order to read it or modify. |
||
347 | * |
||
348 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
349 | * |
||
350 | * @api |
||
351 | * |
||
352 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
353 | * |
||
354 | * @since 0.1.0 |
||
355 | * |
||
356 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
357 | * @throws InvalidColumnException The specified column is not a "date" type column |
||
358 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
359 | * by this library or the DaPulse API. |
||
360 | * |
||
361 | * @return PulseColumnDateValue A column object with access to its contents |
||
362 | */ |
||
363 | 5 | public function getDateColumn ($columnId) |
|
367 | |||
368 | /** |
||
369 | * Access a numeric type column value belonging to this pulse in order to read it or modify. |
||
370 | * |
||
371 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
372 | * |
||
373 | * @api |
||
374 | * |
||
375 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
376 | * |
||
377 | * @since 0.2.0 |
||
378 | * |
||
379 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
380 | * @throws InvalidColumnException The specified column is not a "numeric" type column |
||
381 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
382 | * by this library or the DaPulse API. |
||
383 | * |
||
384 | * @return PulseColumnNumericValue A column object with access to its contents |
||
385 | */ |
||
386 | 3 | public function getNumericColumn ($columnId) |
|
390 | |||
391 | /** |
||
392 | * Access a person type column value belonging to this pulse in order to read it or modify. |
||
393 | * |
||
394 | * This function should only be used to access person type values; an exception will be thrown otherwise. |
||
395 | * |
||
396 | * @api |
||
397 | * |
||
398 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
399 | * |
||
400 | * @since 0.1.0 |
||
401 | * |
||
402 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
403 | * @throws InvalidColumnException The specified column is not a "person" type column |
||
404 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
405 | * by this library or the DaPulse API. |
||
406 | * |
||
407 | * @return PulseColumnPersonValue A column object with access to its contents |
||
408 | */ |
||
409 | 4 | public function getPersonColumn ($columnId) |
|
413 | |||
414 | /** |
||
415 | * Access a text type column value belonging to this pulse in order to read it or modify. |
||
416 | * |
||
417 | * This function should only be used to access text type values; an exception will be thrown otherwise. |
||
418 | * |
||
419 | * @api |
||
420 | * |
||
421 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
422 | * |
||
423 | * @since 0.1.0 |
||
424 | |||
425 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
426 | * @throws InvalidColumnException The specified column is not a "text" type column |
||
427 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
428 | * by this library or the DaPulse API. |
||
429 | * |
||
430 | * @return PulseColumnTextValue A column object with access to its contents |
||
431 | */ |
||
432 | 2 | public function getTextColumn ($columnId) |
|
436 | |||
437 | /** |
||
438 | * Access a timeline type column value belonging to this pulse in order to read it or modify. |
||
439 | * |
||
440 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
441 | * |
||
442 | * @api |
||
443 | * |
||
444 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
445 | * |
||
446 | * @since 0.2.1 |
||
447 | * |
||
448 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
449 | * @throws InvalidColumnException The specified column is not a "numeric" type column |
||
450 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
451 | * by this library or the DaPulse API. |
||
452 | * |
||
453 | * @return PulseColumnTimelineValue A column object with access to its contents |
||
454 | */ |
||
455 | 3 | public function getTimelineColumn ($columnId) |
|
459 | |||
460 | /** |
||
461 | * Build a pulse's column object if it doesn't exist or return the existing column. |
||
462 | * |
||
463 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column |
||
464 | * title |
||
465 | * @param string $columnType The type of column being accessed: 'text', 'color', 'person', 'numeric', or 'date' |
||
466 | * |
||
467 | * @since 0.1.0 |
||
468 | * |
||
469 | * @throws ColumnNotFoundException The specified column ID does not exist for this Pulse |
||
470 | * @throws InvalidColumnException The specified column is not the same type as specified in `$columnType` |
||
471 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
472 | * by this library or the DaPulse API. |
||
473 | * |
||
474 | * @return PulseColumnValue The returned object will be a child of this abstract class. |
||
475 | */ |
||
476 | 23 | private function getColumn ($columnId, $columnType) |
|
526 | |||
527 | // ================================================================================================================ |
||
528 | // Notes functions |
||
529 | // ================================================================================================================ |
||
530 | |||
531 | /** |
||
532 | * Create a new note in this project |
||
533 | * |
||
534 | * @api |
||
535 | * |
||
536 | * @param string $title The title of the note |
||
537 | * @param string $content The body of the note |
||
538 | * @param bool $ownersOnly Set to true if only pulse owners can edit this note. |
||
539 | * @param int|null $userId The id of the user to be marked as the note's last updater |
||
540 | * @param bool $createUpdate Indicates whether to create an update on the pulse notifying subscribers on the |
||
541 | * changes (required user_id to be set). |
||
542 | * |
||
543 | * @since 0.1.0 |
||
544 | * @return PulseNote |
||
545 | */ |
||
546 | public function addNote ($title, $content, $ownersOnly = false, $userId = NULL, $createUpdate = false) |
||
568 | |||
569 | /** |
||
570 | * Return all of the notes belonging to this project |
||
571 | * |
||
572 | * @api |
||
573 | * @since 0.1.0 |
||
574 | * @return PulseNote[] |
||
575 | */ |
||
576 | public function getNotes () |
||
582 | |||
583 | // ================================================================================================================ |
||
584 | // Updates functions |
||
585 | // ================================================================================================================ |
||
586 | |||
587 | /** |
||
588 | * Get all of the updates that belong to this Pulse in reverse chronological order |
||
589 | * |
||
590 | * @api |
||
591 | * |
||
592 | * @since 0.1.0 |
||
593 | * |
||
594 | * @return PulseUpdate[] |
||
595 | */ |
||
596 | 13 | public function getUpdates () |
|
602 | |||
603 | /** |
||
604 | * Create an update for the current Pulse |
||
605 | * |
||
606 | * @api |
||
607 | * |
||
608 | * @param int|PulseUser $user |
||
609 | * @param string $text |
||
610 | * @param null|bool $announceToAll |
||
611 | * |
||
612 | * @since 0.2.2 A PulseUpdate object is returned containing the information of the newly created Update |
||
613 | * @since 0.1.0 |
||
614 | * |
||
615 | * @return PulseUpdate |
||
616 | */ |
||
617 | public function createUpdate ($user, $text, $announceToAll = NULL) |
||
621 | |||
622 | // ================================================================================================================ |
||
623 | // Static functions |
||
624 | // ================================================================================================================ |
||
625 | |||
626 | /** |
||
627 | * Get all of the pulses that belong to the organization across all boards. |
||
628 | * |
||
629 | * To modify the amount of data returned with pagination, use the following values in the array to configure your |
||
630 | * pagination or offsets. |
||
631 | * |
||
632 | * ```php |
||
633 | * $params = array( |
||
634 | * "page" => 1, // (int) Page offset to fetch |
||
635 | * "per_page" => 10, // (int) Number of results per page |
||
636 | * "offset" => 5, // (int) Instead of starting at result 0, start counting from result 5 |
||
637 | * "order_by_latest" => true // (bool) Order the pulses with the most recent first |
||
638 | * ); |
||
639 | * ``` |
||
640 | * |
||
641 | * @api |
||
642 | * |
||
643 | * @param array $params GET parameters passed to with the query to modify the data returned. |
||
644 | * |
||
645 | * @since 0.1.0 |
||
646 | * @return Pulse[] |
||
647 | */ |
||
648 | public static function getPulses ($params = array()) |
||
654 | } |
||
655 |
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..