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 TwitchSDK 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 TwitchSDK, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TwitchSDK |
||
17 | { |
||
18 | /** @var array */ |
||
19 | private $config = array(); |
||
20 | |||
21 | /** @var TwitchRequest */ |
||
22 | protected $request; |
||
23 | |||
24 | /** @var Helper */ |
||
25 | protected $helper; |
||
26 | |||
27 | /** |
||
28 | * TwitchSDK constructor |
||
29 | * @param array $config |
||
30 | * @throws TwitchException |
||
31 | */ |
||
32 | public function __construct(array $config = array()) |
||
43 | |||
44 | /** |
||
45 | * config setter |
||
46 | * @param array $config |
||
47 | * @return TwitchSDK |
||
48 | * @throws TwitchException |
||
49 | */ |
||
50 | public function setConfig(array $config) |
||
60 | |||
61 | /** |
||
62 | * Check if config is set |
||
63 | * @throws TwitchException |
||
64 | */ |
||
65 | private function checkConfig() |
||
71 | |||
72 | /** |
||
73 | * Get value from config |
||
74 | * @param string $key |
||
75 | * @return mixed |
||
76 | * @throws TwitchException |
||
77 | */ |
||
78 | private function getConfigParam($key) |
||
86 | |||
87 | /** |
||
88 | * Basic information about the API and authentication status |
||
89 | * @param null $token |
||
90 | * @return \stdClass |
||
91 | * @throws TwitchException |
||
92 | */ |
||
93 | public function status($token = null) |
||
107 | |||
108 | /** |
||
109 | * Get the specified user |
||
110 | * @param $username |
||
111 | * @return \stdClass |
||
112 | * @throws TwitchException |
||
113 | */ |
||
114 | public function userGet($username) |
||
120 | |||
121 | /** |
||
122 | * Get a user's list of followed channels |
||
123 | * @param string $user |
||
124 | * @param integer $limit |
||
125 | * @param integer $offset |
||
126 | * @param string $direction |
||
127 | * @param string $sortby |
||
128 | * @return \stdClass |
||
129 | * @throws TwitchException |
||
130 | */ |
||
131 | View Code Duplication | public function userFollowChannels($user, $limit = null, $offset = null, $direction = null, $sortby = null) |
|
144 | |||
145 | /** |
||
146 | * Get the status of a follow relationship |
||
147 | * @param string $user |
||
148 | * @param string $channel |
||
149 | * @return \stdClass |
||
150 | * @throws TwitchException |
||
151 | */ |
||
152 | public function userFollowRelationship($user, $channel) |
||
158 | |||
159 | /** |
||
160 | * Set user to follow given channel |
||
161 | * - requires scope 'user_follows_edit' |
||
162 | * @param string $user |
||
163 | * @param string $channel |
||
164 | * @param string $userToken |
||
165 | * @param bool $notifications |
||
166 | * @return \stdClass |
||
167 | * @throws TwitchException |
||
168 | */ |
||
169 | View Code Duplication | public function authUserFollowChannel($user, $channel, $userToken, $notifications = false) |
|
182 | |||
183 | /** |
||
184 | * Set user to unfollow given channel |
||
185 | * - requires scope 'user_follows_edit' |
||
186 | * @param string $user |
||
187 | * @param string $channel |
||
188 | * @param string $userToken |
||
189 | * @return \stdClass |
||
190 | * @throws TwitchException |
||
191 | */ |
||
192 | View Code Duplication | public function authUserUnfollowChannel($user, $channel, $userToken) |
|
204 | |||
205 | /** |
||
206 | * Get the specified channel |
||
207 | * @param string $channelName |
||
208 | * @return \stdClass |
||
209 | * @throws TwitchException |
||
210 | */ |
||
211 | public function channelGet($channelName) |
||
217 | |||
218 | /** |
||
219 | * Return team list for specified channel |
||
220 | * @param string $channelName |
||
221 | * @return \stdClass |
||
222 | * @throws TwitchException |
||
223 | */ |
||
224 | public function channelTeamsGet($channelName) |
||
230 | |||
231 | /** |
||
232 | * Search channels |
||
233 | * @param string $query |
||
234 | * @param integer $limit |
||
235 | * @param integer $offset |
||
236 | * @return \stdClass |
||
237 | * @throws TwitchException |
||
238 | */ |
||
239 | View Code Duplication | public function channelSearch($query, $limit = null, $offset = null) |
|
251 | |||
252 | /** |
||
253 | * Get the specified team |
||
254 | * @param $teamName |
||
255 | * @return \stdClass |
||
256 | * @throws TwitchException |
||
257 | */ |
||
258 | public function teamGet($teamName) |
||
264 | |||
265 | /** |
||
266 | * Returns a list of active teams |
||
267 | * @param integer $limit |
||
268 | * @param integer $offset |
||
269 | * @return \stdClass |
||
270 | * @throws TwitchException |
||
271 | */ |
||
272 | View Code Duplication | public function teamList($limit = null, $offset = null) |
|
283 | |||
284 | /** |
||
285 | * Get all team members |
||
286 | * @param $teamName |
||
287 | * @return mixed |
||
288 | * @throws TwitchException |
||
289 | */ |
||
290 | public function teamMembersAll($teamName) |
||
294 | |||
295 | /** |
||
296 | * Returns an array of users who follow the specified channel |
||
297 | * @param string $channelName |
||
298 | * @param integer $limit |
||
299 | * @param integer $offset |
||
300 | * @param string $cursor |
||
301 | * @param string $direction |
||
302 | * @return \stdClass |
||
303 | * @throws TwitchException |
||
304 | */ |
||
305 | View Code Duplication | public function channelFollows($channelName, $limit = null, $offset = null, $cursor = null, $direction = null) |
|
318 | |||
319 | /** |
||
320 | * Get the specified channel's stream |
||
321 | * @param $channel |
||
322 | * @return \stdClass |
||
323 | * @throws TwitchException |
||
324 | */ |
||
325 | public function streamGet($channel) |
||
331 | |||
332 | /** |
||
333 | * Search live streams |
||
334 | * @param string $query |
||
335 | * @param null $limit |
||
336 | * @param null $offset |
||
337 | * @param null $hls |
||
338 | * @return \stdClass |
||
339 | * @throws TwitchException |
||
340 | */ |
||
341 | public function streamSearch($query, $limit = null, $offset = null, $hls = null) |
||
354 | |||
355 | /** |
||
356 | * Summarize streams |
||
357 | * @param null $game |
||
358 | * @param array|null $channels |
||
359 | * @param null $hls |
||
360 | * @return \stdClass |
||
361 | * @throws TwitchException |
||
362 | */ |
||
363 | View Code Duplication | public function streamsSummarize($game = null, array $channels = null, $hls = null) |
|
379 | |||
380 | /** |
||
381 | * Get featured streams |
||
382 | * @param null $limit |
||
383 | * @param null $offset |
||
384 | * @param null $hls |
||
385 | * @return \stdClass |
||
386 | * @throws TwitchException |
||
387 | */ |
||
388 | View Code Duplication | public function streamsFeatured($limit = null, $offset = null, $hls = null) |
|
400 | |||
401 | /** |
||
402 | * Get video |
||
403 | * @param string $videoId |
||
404 | * @return \stdClass |
||
405 | * @throws TwitchException |
||
406 | */ |
||
407 | public function videoGet($videoId) |
||
413 | |||
414 | /** |
||
415 | * Returns top videos |
||
416 | * @param integer $limit |
||
417 | * @param integer $offset |
||
418 | * @param string $game |
||
419 | * @param string $period |
||
420 | * @return \stdClass |
||
421 | * @throws TwitchException |
||
422 | */ |
||
423 | View Code Duplication | public function videosTop($limit = null, $offset = null, $game = null, $period = null) |
|
436 | |||
437 | /** |
||
438 | * Get videos for a channel |
||
439 | * @param $channel |
||
440 | * @param null $limit |
||
441 | * @param null $offset |
||
442 | * @param bool $broadcasts |
||
443 | * @param bool $hls |
||
444 | * @return \stdClass |
||
445 | * @throws TwitchException |
||
446 | */ |
||
447 | View Code Duplication | public function videosByChannel($channel, $limit = null, $offset = null, $broadcasts = null, $hls = null) |
|
460 | |||
461 | /** |
||
462 | * Returns a links object to all other chat endpoints |
||
463 | * @param string $channelName |
||
464 | * @return \stdClass |
||
465 | * @throws TwitchException |
||
466 | */ |
||
467 | public function chatGet($channelName) |
||
473 | |||
474 | /** |
||
475 | * Get a chat's emoticons |
||
476 | * @return \stdClass |
||
477 | * @throws TwitchException |
||
478 | */ |
||
479 | public function chatEmoticons() |
||
485 | |||
486 | /** |
||
487 | * Returns a list of emoticons |
||
488 | * @param string $emoteset |
||
489 | * @return \stdClass |
||
490 | * @throws TwitchException |
||
491 | */ |
||
492 | View Code Duplication | public function chatEmoticonsImages($emoteset = null) |
|
502 | |||
503 | /** |
||
504 | * Returns a list of chat badges |
||
505 | * @param string $channelName |
||
506 | * @return \stdClass |
||
507 | * @throws TwitchException |
||
508 | */ |
||
509 | public function chatBadges($channelName) |
||
515 | |||
516 | /** |
||
517 | * Get top games |
||
518 | * @param integer $limit |
||
519 | * @param integer $offset |
||
520 | * @return \stdClass |
||
521 | * @throws TwitchException |
||
522 | */ |
||
523 | View Code Duplication | public function gamesTop($limit = null, $offset = null) |
|
534 | |||
535 | /** |
||
536 | * Search games |
||
537 | * @param string $query |
||
538 | * @param null $type |
||
539 | * @param null $live |
||
540 | * @return \stdClass |
||
541 | * @throws TwitchException |
||
542 | */ |
||
543 | View Code Duplication | public function gameSearch($query, $type = null, $live = null) |
|
555 | |||
556 | /** |
||
557 | * Get HTML code for stream embedding |
||
558 | * @param $channel |
||
559 | * @param int $width |
||
560 | * @param int $height |
||
561 | * @param int $volume |
||
562 | * @return string |
||
563 | * @deprecated Will be replaced with Embed method |
||
564 | */ |
||
565 | View Code Duplication | public function embedStream($channel, $width = 620, $height = 378, $volume = 25) |
|
585 | |||
586 | /** |
||
587 | * Get HTML code for video embedding |
||
588 | * @param $channel |
||
589 | * @param $chapterid |
||
590 | * @param int $width |
||
591 | * @param int $height |
||
592 | * @param int $volume |
||
593 | * @return string |
||
594 | * @deprecated Will be replaced with Embed method |
||
595 | */ |
||
596 | View Code Duplication | public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25) |
|
616 | |||
617 | /** |
||
618 | * Get HTML code for chat embedding |
||
619 | * @param $channel |
||
620 | * @param int $width |
||
621 | * @param int $height |
||
622 | * @return string |
||
623 | * @deprecated Will be replaced with Embed method |
||
624 | */ |
||
625 | public function embedChat($channel, $width = 400, $height = 300) |
||
635 | |||
636 | /** |
||
637 | * Get login URL for authentication |
||
638 | * @param string $scope Specify which permissions your app requires (space separated list) |
||
639 | * @return \stdClass |
||
640 | * @throws TwitchException |
||
641 | */ |
||
642 | View Code Duplication | public function authLoginURL($scope) |
|
657 | |||
658 | /** |
||
659 | * Get authentication access token |
||
660 | * @param string $code returned after app authorization by user |
||
661 | * @return \stdClass |
||
662 | * @throws TwitchException |
||
663 | */ |
||
664 | View Code Duplication | public function authAccessTokenGet($code) |
|
680 | |||
681 | /** |
||
682 | * Get the authenticated user |
||
683 | * - requires scope 'user_read' |
||
684 | * @param string |
||
685 | * @return \stdClass |
||
686 | * @throws TwitchException |
||
687 | */ |
||
688 | public function authUserGet($token) |
||
696 | |||
697 | /** |
||
698 | * Returns a list of blocks |
||
699 | * - required scope 'user_blocks_read' |
||
700 | * @param string $token |
||
701 | * @param string $user |
||
702 | * @param integer $limit |
||
703 | * @param integer $offset |
||
704 | * @return \stdClass |
||
705 | * @throws TwitchException |
||
706 | */ |
||
707 | public function authUserBlocks($token, $user, $limit = null, $offset = null) |
||
722 | |||
723 | /** |
||
724 | * Adds $target to $user block list |
||
725 | * - requires scope 'user_blocks_edit' |
||
726 | * @param string $token |
||
727 | * @param string $user |
||
728 | * @param string $target |
||
729 | * @return \stdClass |
||
730 | * @throws TwitchException |
||
731 | */ |
||
732 | View Code Duplication | public function authBlockTarget($token, $user, $target) |
|
740 | |||
741 | /** |
||
742 | * Removes $target from $user block list |
||
743 | * - requires scope 'user_blocks_edit' |
||
744 | * @param string $token |
||
745 | * @param string $user |
||
746 | * @param string $target |
||
747 | * @return \stdClass |
||
748 | * @throws TwitchException |
||
749 | */ |
||
750 | View Code Duplication | public function authRemoveTarget($token, $user, $target) |
|
758 | |||
759 | /** |
||
760 | * Get the authenticated channel |
||
761 | * - requires scope 'channel_read' |
||
762 | * @param string |
||
763 | * @return \stdClass |
||
764 | * @throws TwitchException |
||
765 | */ |
||
766 | View Code Duplication | public function authChannelGet($token) |
|
774 | |||
775 | /** |
||
776 | * Update channel's status or game |
||
777 | * - requires scope 'channel_editor' |
||
778 | * @param $token |
||
779 | * @param string $channelName |
||
780 | * @param string $status |
||
781 | * @param string $game |
||
782 | * @param integer $delay |
||
783 | * @return \stdClass |
||
784 | * @throws TwitchException |
||
785 | */ |
||
786 | public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null) |
||
801 | |||
802 | /** |
||
803 | * Resets channel's stream key |
||
804 | * - requires scope 'channel_stream' |
||
805 | * @param string $token |
||
806 | * @param string $channelName |
||
807 | * @return \stdClass |
||
808 | * @throws TwitchException |
||
809 | */ |
||
810 | View Code Duplication | public function authChannelResetKey($token, $channelName) |
|
818 | |||
819 | /** |
||
820 | * Returns an array of users who are editors of specified channel |
||
821 | * - requires scope 'channel_read' |
||
822 | * @param string |
||
823 | * @param string |
||
824 | * @return \stdClass |
||
825 | * @throws TwitchException |
||
826 | */ |
||
827 | View Code Duplication | public function authChannelEditors($token, $channel) |
|
835 | |||
836 | /** |
||
837 | * Returns an array of subscriptions who are subscribed to specified channel |
||
838 | * - requires scope 'channel_subscriptions' |
||
839 | * @param string $token - user's access token |
||
840 | * @param string $channel |
||
841 | * @param integer $limit - can be up to 100 |
||
842 | * @param integer $offset |
||
843 | * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
||
844 | * @return \stdClass |
||
845 | * @throws TwitchException |
||
846 | */ |
||
847 | View Code Duplication | public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
|
863 | |||
864 | /** |
||
865 | * Returns user object if that user is subscribed |
||
866 | * - requires scope 'channel_check_subscription' for channel |
||
867 | * @param string $token |
||
868 | * @param string $channel |
||
869 | * @param string $user |
||
870 | * @return \stdClass |
||
871 | * @throws TwitchException |
||
872 | */ |
||
873 | View Code Duplication | public function authSubscribedUser($token, $channel, $user) |
|
881 | |||
882 | /** |
||
883 | * Returns a channel object that user subscribes to |
||
884 | * - requires scope 'user_subscriptions' for user |
||
885 | * @param string $token |
||
886 | * @param string $user |
||
887 | * @param string $channel |
||
888 | * @return \stdClass |
||
889 | * @throws TwitchException |
||
890 | */ |
||
891 | View Code Duplication | public function authSubscribedToChannel($token, $user, $channel) |
|
899 | |||
900 | /** |
||
901 | * List the live streams that the authenticated user is following |
||
902 | * - requires scope 'user_read' |
||
903 | * @param string |
||
904 | * @param integer $limit |
||
905 | * @param integer $offset |
||
906 | * @param bool $hls |
||
907 | * @return \stdClass |
||
908 | * @throws TwitchException |
||
909 | */ |
||
910 | View Code Duplication | public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
|
926 | |||
927 | /** |
||
928 | * Get streams helper |
||
929 | * @param null $game |
||
930 | * @param string|null $channels |
||
931 | * @param null $limit |
||
932 | * @param null $offset |
||
933 | * @param null $embeddable |
||
934 | * @param null $hls |
||
935 | * @return \stdClass |
||
936 | * @throws TwitchException |
||
937 | */ |
||
938 | public function getStreams($game = null, $channels = null, $limit = null, $offset = null, $embeddable = null, $hls = null) |
||
954 | |||
955 | /** |
||
956 | * Validate parameters for authentication |
||
957 | * @param array |
||
958 | * @return boolean |
||
959 | */ |
||
960 | private function configValidate($config) |
||
975 | |||
976 | /** |
||
977 | * Returns string for auth |
||
978 | * @param string $token |
||
979 | * @return null|string |
||
980 | * @throws TwitchException |
||
981 | */ |
||
982 | private function getAuthString($token) |
||
989 | |||
990 | /** |
||
991 | * Configuration exception |
||
992 | * @throws TwitchException |
||
993 | */ |
||
994 | private function configException() |
||
998 | } |
||
999 |