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 |
||
30 | class Pulse extends ApiObject |
||
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 unique identifier. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $id; |
||
54 | |||
55 | /** |
||
56 | * The pulse's name. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $name; |
||
61 | |||
62 | /** |
||
63 | * The board's subscribers. |
||
64 | * |
||
65 | * @var PulseUser[] |
||
66 | */ |
||
67 | protected $subscribers; |
||
68 | |||
69 | /** |
||
70 | * The amount of updates a pulse has. |
||
71 | * |
||
72 | * @var int |
||
73 | */ |
||
74 | protected $updates_count; |
||
75 | |||
76 | /** |
||
77 | * The ID of the parent board. |
||
78 | * |
||
79 | * @var int |
||
80 | */ |
||
81 | protected $board_id; |
||
82 | |||
83 | /** |
||
84 | * Creation time. |
||
85 | * |
||
86 | * @var \DateTime |
||
87 | */ |
||
88 | protected $created_at; |
||
89 | |||
90 | /** |
||
91 | * Last update time. |
||
92 | * |
||
93 | * @var \DateTime |
||
94 | */ |
||
95 | protected $updated_at; |
||
96 | |||
97 | /** |
||
98 | * The ID of the group this pulse belongs to |
||
99 | * |
||
100 | * @var string |
||
101 | */ |
||
102 | protected $group_id; |
||
103 | |||
104 | /** |
||
105 | * @var PulseColumn[] |
||
106 | */ |
||
107 | protected $column_structure; |
||
108 | |||
109 | /** |
||
110 | * An array containing all of the values a pulse has for each column |
||
111 | * |
||
112 | * @var mixed |
||
113 | */ |
||
114 | protected $raw_column_values; |
||
115 | |||
116 | /** |
||
117 | * An array containing objects extended from PulseColumnValue storing all of the values for each column |
||
118 | * |
||
119 | * @var array |
||
120 | */ |
||
121 | protected $column_values; |
||
122 | |||
123 | /** |
||
124 | * The common URL path for retrieving objects relating a pulse such as subscribers, notes, or updates |
||
125 | * |
||
126 | * @var string |
||
127 | */ |
||
128 | private $urlSyntax = "%s/%s/%s.json"; |
||
129 | |||
130 | // ================================================================================================================ |
||
131 | // Overloaded functions |
||
132 | // ================================================================================================================ |
||
133 | |||
134 | protected function initializeValues () |
||
140 | |||
141 | // ================================================================================================================ |
||
142 | // Getter functions |
||
143 | // ================================================================================================================ |
||
144 | |||
145 | /** |
||
146 | * The resource's URL. |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getUrl() |
||
154 | |||
155 | /** |
||
156 | * The pulse's unique identifier. |
||
157 | * |
||
158 | * @return int |
||
159 | */ |
||
160 | public function getId() |
||
164 | |||
165 | /** |
||
166 | * The pulse's name. |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getName() |
||
174 | |||
175 | /** |
||
176 | * The amount of updates a pulse has. |
||
177 | * |
||
178 | * @return int |
||
179 | */ |
||
180 | public function getUpdatesCount() |
||
184 | |||
185 | /** |
||
186 | * The ID of the parent board. |
||
187 | * |
||
188 | * @return int |
||
189 | */ |
||
190 | public function getBoardId() |
||
194 | |||
195 | /** |
||
196 | * Creation time. |
||
197 | * |
||
198 | * @return \DateTime |
||
199 | */ |
||
200 | public function getCreatedAt() |
||
204 | |||
205 | /** |
||
206 | * Last update time. |
||
207 | * |
||
208 | * @return \DateTime |
||
209 | */ |
||
210 | public function getUpdatedAt() |
||
214 | |||
215 | /** |
||
216 | * 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 |
||
217 | * find the group ID via brute force. |
||
218 | * |
||
219 | * **Note** The group ID is cached if it is not available. To update the cached value, use $forceFetch to force an |
||
220 | * API call to get a new value. |
||
221 | * |
||
222 | * **Warning** An API call is always slower than using the cached value. |
||
223 | * |
||
224 | * @param bool $forceFetch Force an API call to get an updated group ID if it has been changed |
||
225 | * @since 0.1.0 |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getGroupId($forceFetch = false) |
||
247 | |||
248 | // ================================================================================================================ |
||
249 | // Pulse functions |
||
250 | // ================================================================================================================ |
||
251 | |||
252 | /** |
||
253 | * Delete the current Pulse |
||
254 | * |
||
255 | * @api |
||
256 | * @throws \allejo\DaPulse\Exceptions\InvalidObjectException |
||
257 | */ |
||
258 | View Code Duplication | public function deletePulse () |
|
268 | |||
269 | public function duplicatePulse ($group_id = null, $owner_id = null) |
||
287 | |||
288 | View Code Duplication | private function pulseInjection (&$result) |
|
297 | |||
298 | // ================================================================================================================ |
||
299 | // Column data functions |
||
300 | // ================================================================================================================ |
||
301 | |||
302 | /** |
||
303 | * Access a pulse's specific column to either access their value or to modify the value. |
||
304 | * |
||
305 | * See the related functions to see the appropriate replacements. |
||
306 | * |
||
307 | * @todo This function only exists for legacy applications. Remove in 0.1.1 |
||
308 | * |
||
309 | * @api |
||
310 | * @deprecated 0.0.1 This function will be removed by 0.1.1. New stricter functions are available |
||
311 | * |
||
312 | * @param string $columnId The ID of the column to access. It's typically a slugified version of the column title |
||
313 | * |
||
314 | * @see Pulse::getStatusColumn() getColorColumn() |
||
315 | * @see Pulse::getDateColumn() getDateColumn() |
||
316 | * @see Pulse::getPersonColumn() getPersonColumn() |
||
317 | * @see Pulse::getTextColumn() getTextColumn() |
||
318 | * @since 0.1.0 |
||
319 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
320 | * by this library or the DaPulse API. |
||
321 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
322 | * @return PulseColumnValue The returned object will be a child of this abstract class. |
||
323 | */ |
||
324 | public function getColumnValue ($columnId) |
||
342 | |||
343 | /** |
||
344 | * Access a color type column value belonging to this pulse in order to read it or modify. |
||
345 | * |
||
346 | * This function should only be used to access color type values; an exception will be thrown otherwise. |
||
347 | * |
||
348 | * @api |
||
349 | * |
||
350 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
351 | * |
||
352 | * @since 0.1.0 |
||
353 | * @throws InvalidColumnException The specified column is not a "color" type column |
||
354 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
355 | * by this library or the DaPulse API. |
||
356 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
357 | * @return PulseColumnStatusValue A column object with access to its contents |
||
358 | */ |
||
359 | public function getStatusColumn ($columnId) |
||
363 | |||
364 | /** |
||
365 | * Access a date type column value belonging to this pulse in order to read it or modify. |
||
366 | * |
||
367 | * This function should only be used to access data type values; an exception will be thrown otherwise. |
||
368 | * |
||
369 | * @api |
||
370 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
371 | * @since 0.1.0 |
||
372 | * @throws InvalidColumnException The specified column is not a "date" type column |
||
373 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
374 | * by this library or the DaPulse API. |
||
375 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
376 | * @return PulseColumnDateValue A column object with access to its contents |
||
377 | */ |
||
378 | public function getDateColumn ($columnId) |
||
382 | |||
383 | /** |
||
384 | * Access a person type column value belonging to this pulse in order to read it or modify. |
||
385 | * |
||
386 | * This function should only be used to access person type values; an exception will be thrown otherwise. |
||
387 | * |
||
388 | * @api |
||
389 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
390 | * @since 0.1.0 |
||
391 | * @throws InvalidColumnException The specified column is not a "person" type column |
||
392 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
393 | * by this library or the DaPulse API. |
||
394 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
395 | * @return PulseColumnPersonValue A column object with access to its contents |
||
396 | */ |
||
397 | public function getPersonColumn ($columnId) |
||
401 | |||
402 | /** |
||
403 | * Access a text type column value belonging to this pulse in order to read it or modify. |
||
404 | * |
||
405 | * This function should only be used to access text type values; an exception will be thrown otherwise. |
||
406 | * |
||
407 | * @api |
||
408 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column name |
||
409 | * @since 0.1.0 |
||
410 | * @throws InvalidColumnException The specified column is not a "text" type column |
||
411 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
412 | * by this library or the DaPulse API. |
||
413 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
414 | * @return PulseColumnTextValue A column object with access to its contents |
||
415 | */ |
||
416 | public function getTextColumn ($columnId) |
||
420 | |||
421 | /** |
||
422 | * Build a pulse's column object if it doesn't exist or return the existing column. |
||
423 | * |
||
424 | * @param string $columnId The ID of the column to access. This is typically a slugified version of the column |
||
425 | * title |
||
426 | * @param string $columnType The type of column being accessed: 'text', 'color', 'person', or 'date' |
||
427 | * |
||
428 | * @since 0.1.0 |
||
429 | * |
||
430 | * @throws InvalidColumnException The specified column is not the same type as specified in `$columnType` |
||
431 | * @throws InvalidObjectException The specified column exists but modification of its value is unsupported either |
||
432 | * by this library or the DaPulse API. |
||
433 | * @throws InvalidColumnException The specified column ID does not exist for this Pulse |
||
434 | * |
||
435 | * @return PulseColumnValue The returned object will be a child of this abstract class. |
||
436 | */ |
||
437 | private function getColumn ($columnId, $columnType) |
||
486 | |||
487 | // ================================================================================================================ |
||
488 | // Subscribers functions |
||
489 | // ================================================================================================================ |
||
490 | |||
491 | /** |
||
492 | * Subscribe a user to a pulse |
||
493 | * |
||
494 | * @api |
||
495 | * |
||
496 | * @param int|PulseUser $user_id The user that will be subscribed |
||
497 | * @param bool|null $as_admin True to make them an admin of the Pulse |
||
498 | * |
||
499 | * @since 0.1.0 |
||
500 | * |
||
501 | * @throws HttpException Subscribing a user failed; access the exception for more information. |
||
502 | */ |
||
503 | public function addSubscriber ($user_id, $as_admin = null) |
||
518 | |||
519 | /** |
||
520 | * Access a pulse's subscribers |
||
521 | * |
||
522 | * To modify the amount of data returned with pagination, use the following values in the array to configure your |
||
523 | * pagination or offsets. |
||
524 | * |
||
525 | * ```php |
||
526 | * $params = array( |
||
527 | * "page" => 1, // (int) Page offset to fetch |
||
528 | * "per_page" => 10, // (int) Number of results per page |
||
529 | * "offset" => 5, // (int) Instead of starting at result 0, start counting from result 5 |
||
530 | * ); |
||
531 | * ``` |
||
532 | * |
||
533 | * @api |
||
534 | * @param array $params GET parameters passed to with the query to modify the data returned. |
||
535 | * @since 0.1.0 |
||
536 | * @return PulseUser[] |
||
537 | */ |
||
538 | public function getSubscribers ($params = array()) |
||
544 | |||
545 | /** |
||
546 | * Unsubscribe a person from a pulse |
||
547 | * |
||
548 | * @api |
||
549 | * |
||
550 | * @param int|PulseUser $user_id The user that will be subscribed |
||
551 | * |
||
552 | * @since 0.1.0 |
||
553 | * |
||
554 | * @throws HttpException Removing a user failed; access the exception for more information. |
||
555 | */ |
||
556 | public function removeSubscriber ($user_id) |
||
567 | |||
568 | // ================================================================================================================ |
||
569 | // Notes functions |
||
570 | // ================================================================================================================ |
||
571 | |||
572 | /** |
||
573 | * Create a new note in this project |
||
574 | * |
||
575 | * @api |
||
576 | * @param string $title The title of the note |
||
577 | * @param string $content The body of the note |
||
578 | * @param bool $owners_only Set to true if only pulse owners can edit this note. |
||
579 | * @param int|null $user_id The id of the user to be marked as the note’s last updater |
||
580 | * @param bool $create_update Indicates whether to create an update on the pulse notifying subscribers on the |
||
581 | * changes (required user_id to be set). |
||
582 | * @since 0.1.0 |
||
583 | * @return PulseNote |
||
584 | */ |
||
585 | public function addNote ($title, $content, $owners_only = false, $user_id = NULL, $create_update = false) |
||
607 | |||
608 | /** |
||
609 | * Return all of the notes belonging to this project |
||
610 | * |
||
611 | * @api |
||
612 | * @since 0.1.0 |
||
613 | * @return PulseNote[] |
||
614 | */ |
||
615 | public function getNotes () |
||
621 | |||
622 | // ================================================================================================================ |
||
623 | // Updates functions |
||
624 | // ================================================================================================================ |
||
625 | |||
626 | /** |
||
627 | * Get all of the updates that belong this Pulse |
||
628 | * |
||
629 | * @api |
||
630 | * @since 0.1.0 |
||
631 | * @return PulseUpdate[] |
||
632 | */ |
||
633 | public function getUpdates () |
||
639 | |||
640 | // ================================================================================================================ |
||
641 | // Static functions |
||
642 | // ================================================================================================================ |
||
643 | |||
644 | /** |
||
645 | * Get all of the pulses that belong to the organization across all boards. |
||
646 | * |
||
647 | * To modify the amount of data returned with pagination, use the following values in the array to configure your |
||
648 | * pagination or offsets. |
||
649 | * |
||
650 | * ```php |
||
651 | * $params = array( |
||
652 | * "page" => 1, // (int) Page offset to fetch |
||
653 | * "per_page" => 10, // (int) Number of results per page |
||
654 | * "offset" => 5, // (int) Instead of starting at result 0, start counting from result 5 |
||
655 | * "order_by_latest" => true // (bool) Order the pulses with the most recent first |
||
656 | * ); |
||
657 | * ``` |
||
658 | * |
||
659 | * @api |
||
660 | * @param array $params GET parameters passed to with the query to modify the data returned. |
||
661 | * @since 0.1.0 |
||
662 | * @return Pulse[] |
||
663 | */ |
||
664 | public static function getPulses ($params = array()) |
||
670 | } |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.