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 |
||
17 | class TwitchSDK |
||
18 | { |
||
19 | /** @var array */ |
||
20 | private $config = array(); |
||
21 | |||
22 | /** @var TwitchRequest */ |
||
23 | protected $request; |
||
24 | |||
25 | /** @var Helper */ |
||
26 | protected $helper; |
||
27 | |||
28 | /** |
||
29 | * TwitchAPI URI's |
||
30 | */ |
||
31 | const URI_STREAMS_SEARCH = 'search/streams/'; |
||
32 | |||
33 | /** |
||
34 | * TwitchSDK constructor |
||
35 | * @param array $config |
||
36 | * @throws TwitchException |
||
37 | */ |
||
38 | public function __construct(array $config = array()) |
||
51 | |||
52 | /** |
||
53 | * config setter |
||
54 | * @param array $config |
||
55 | * @return TwitchSDK |
||
56 | * @throws TwitchException |
||
57 | */ |
||
58 | public function setConfig(array $config) |
||
68 | |||
69 | /** |
||
70 | * Check if config is set |
||
71 | * @throws TwitchException |
||
72 | */ |
||
73 | private function checkConfig() |
||
79 | |||
80 | /** |
||
81 | * Get value from config |
||
82 | * @param string $key |
||
83 | * @return mixed |
||
84 | * @throws TwitchException |
||
85 | */ |
||
86 | private function getConfigParam($key) |
||
94 | |||
95 | /** |
||
96 | * Basic information about the API and authentication status |
||
97 | * @param null $token |
||
98 | * @return \stdClass |
||
99 | * @throws TwitchException |
||
100 | */ |
||
101 | public function status($token = null) |
||
115 | |||
116 | /** |
||
117 | * Get the specified user |
||
118 | * @param $username |
||
119 | * @return \stdClass |
||
120 | * @throws TwitchException |
||
121 | */ |
||
122 | public function userGet($username) |
||
128 | |||
129 | /** |
||
130 | * Get a user's list of followed channels |
||
131 | * @param string $user |
||
132 | * @param integer $limit |
||
133 | * @param integer $offset |
||
134 | * @param string $direction |
||
135 | * @param string $sortby |
||
136 | * @return \stdClass |
||
137 | * @throws TwitchException |
||
138 | */ |
||
139 | View Code Duplication | public function userFollowChannels($user, $limit = null, $offset = null, $direction = null, $sortby = null) |
|
152 | |||
153 | /** |
||
154 | * Get the status of a follow relationship |
||
155 | * @param string $user |
||
156 | * @param string $channel |
||
157 | * @return \stdClass |
||
158 | * @throws TwitchException |
||
159 | */ |
||
160 | public function userFollowRelationship($user, $channel) |
||
166 | |||
167 | /** |
||
168 | * Set user to follow given channel |
||
169 | * - requires scope 'user_follows_edit' |
||
170 | * @param string $user |
||
171 | * @param string $channel |
||
172 | * @param string $userToken |
||
173 | * @param bool $notifications |
||
174 | * @return \stdClass |
||
175 | * @throws TwitchException |
||
176 | */ |
||
177 | public function userFollowChannel($user, $channel, $userToken, $notifications = false) |
||
188 | |||
189 | /** |
||
190 | * Set user to unfollow given channel |
||
191 | * - requires scope 'user_follows_edit' |
||
192 | * @param string $user |
||
193 | * @param string $channel |
||
194 | * @param string $userToken |
||
195 | * @return \stdClass |
||
196 | * @throws TwitchException |
||
197 | */ |
||
198 | View Code Duplication | public function userUnfollowChannel($user, $channel, $userToken) |
|
208 | |||
209 | /** |
||
210 | * Get the specified channel |
||
211 | * @param string $channelName |
||
212 | * @return \stdClass |
||
213 | * @throws TwitchException |
||
214 | */ |
||
215 | public function channelGet($channelName) |
||
221 | |||
222 | /** |
||
223 | * Return team list for specified channel |
||
224 | * @param string $channelName |
||
225 | * @return \stdClass |
||
226 | * @throws TwitchException |
||
227 | */ |
||
228 | public function channelTeamsGet($channelName) |
||
234 | |||
235 | /** |
||
236 | * Get the specified team |
||
237 | * @param $teamName |
||
238 | * @return \stdClass |
||
239 | * @throws TwitchException |
||
240 | */ |
||
241 | public function teamGet($teamName) |
||
247 | |||
248 | /** |
||
249 | * Returns a list of active teams |
||
250 | * @param integer $limit |
||
251 | * @param integer $offset |
||
252 | * @return \stdClass |
||
253 | * @throws TwitchException |
||
254 | */ |
||
255 | View Code Duplication | public function teamList($limit = null, $offset = null) |
|
266 | |||
267 | /** |
||
268 | * Get all team members |
||
269 | * @param $teamName |
||
270 | * @return mixed |
||
271 | * @throws TwitchException |
||
272 | */ |
||
273 | public function teamMembersAll($teamName) |
||
277 | |||
278 | /** |
||
279 | * Returns an array of users who follow the specified channel |
||
280 | * @param string $channelName |
||
281 | * @param integer $limit |
||
282 | * @param integer $offset |
||
283 | * @param string $cursor |
||
284 | * @param string $direction |
||
285 | * @return \stdClass |
||
286 | * @throws TwitchException |
||
287 | */ |
||
288 | View Code Duplication | public function channelFollows($channelName, $limit = null, $offset = null, $cursor = null, $direction = null) |
|
301 | |||
302 | /** |
||
303 | * Get the specified channel's stream |
||
304 | * @param $channel |
||
305 | * @return \stdClass |
||
306 | * @throws TwitchException |
||
307 | */ |
||
308 | public function streamGet($channel) |
||
314 | |||
315 | /** |
||
316 | * Search live streams |
||
317 | * @param $query |
||
318 | * @param null $limit |
||
319 | * @param null $offset |
||
320 | * @return \stdClass |
||
321 | * @throws TwitchException |
||
322 | * @deprecated will be replaced by getStreams() function |
||
323 | */ |
||
324 | public function streamSearch($query, $limit = null, $offset = null) |
||
334 | |||
335 | /** |
||
336 | * Summarize streams |
||
337 | * @param null $game |
||
338 | * @param array|null $channels |
||
339 | * @param null $hls |
||
340 | * @return \stdClass |
||
341 | * @throws TwitchException |
||
342 | */ |
||
343 | View Code Duplication | public function streamsSummarize($game = null, array $channels = null, $hls = null) |
|
359 | |||
360 | /** |
||
361 | * Get featured streams |
||
362 | * @param null $limit |
||
363 | * @param null $offset |
||
364 | * @param null $hls |
||
365 | * @return \stdClass |
||
366 | * @throws TwitchException |
||
367 | */ |
||
368 | View Code Duplication | public function streamsFeatured($limit = null, $offset = null, $hls = null) |
|
380 | |||
381 | /** |
||
382 | * Get streams by channel |
||
383 | * @param $channels |
||
384 | * @param null $limit |
||
385 | * @param null $offset |
||
386 | * @param null $embeddable |
||
387 | * @param null $hls |
||
388 | * @return \stdClass |
||
389 | * @throws TwitchException |
||
390 | * @deprecated will be replaced by getStreams() function |
||
391 | */ |
||
392 | public function streamsByChannels($channels, $limit = null, $offset = null, $embeddable = null, $hls = null) |
||
398 | |||
399 | /** |
||
400 | * Get streams by game |
||
401 | * @param $game |
||
402 | * @param null $limit |
||
403 | * @param null $offset |
||
404 | * @param null $embeddable |
||
405 | * @param null $hls |
||
406 | * @return \stdClass |
||
407 | * @throws TwitchException |
||
408 | * @deprecated will be replaced by getStreams() function |
||
409 | */ |
||
410 | public function streamsByGame($game, $limit = null, $offset = null, $embeddable = null, $hls = null) |
||
414 | |||
415 | /** |
||
416 | * Get video |
||
417 | * @param string $videoId |
||
418 | * @return \stdClass |
||
419 | * @throws TwitchException |
||
420 | */ |
||
421 | public function videoGet($videoId) |
||
427 | |||
428 | /** |
||
429 | * Returns top videos |
||
430 | * @param integer $limit |
||
431 | * @param integer $offset |
||
432 | * @param string $game |
||
433 | * @param string $period |
||
434 | * @return \stdClass |
||
435 | * @throws TwitchException |
||
436 | */ |
||
437 | View Code Duplication | public function videosTop($limit = null, $offset = null, $game = null, $period = null) |
|
450 | |||
451 | /** |
||
452 | * Get videos for a channel |
||
453 | * @param $channel |
||
454 | * @param null $limit |
||
455 | * @param null $offset |
||
456 | * @param bool $broadcasts |
||
457 | * @param bool $hls |
||
458 | * @return \stdClass |
||
459 | * @throws TwitchException |
||
460 | */ |
||
461 | View Code Duplication | public function videosByChannel($channel, $limit = null, $offset = null, $broadcasts = null, $hls = null) |
|
474 | |||
475 | /** |
||
476 | * Returns a links object to all other chat endpoints |
||
477 | * @param string $channelName |
||
478 | * @return \stdClass |
||
479 | * @throws TwitchException |
||
480 | */ |
||
481 | public function chatGet($channelName) |
||
487 | |||
488 | /** |
||
489 | * Get a chat's emoticons |
||
490 | * @return \stdClass |
||
491 | * @throws TwitchException |
||
492 | */ |
||
493 | public function chatEmoticons() |
||
499 | |||
500 | /** |
||
501 | * Returns a list of emoticons |
||
502 | * @param string $emoteset |
||
503 | * @return \stdClass |
||
504 | * @throws TwitchException |
||
505 | */ |
||
506 | View Code Duplication | public function chatEmoticonsImages($emoteset = null) |
|
516 | |||
517 | /** |
||
518 | * Returns a list of chat badges |
||
519 | * @param string $channelName |
||
520 | * @return \stdClass |
||
521 | * @throws TwitchException |
||
522 | */ |
||
523 | public function chatBadges($channelName) |
||
529 | |||
530 | /** |
||
531 | * Get top games |
||
532 | * @param integer $limit |
||
533 | * @param integer $offset |
||
534 | * @return \stdClass |
||
535 | * @throws TwitchException |
||
536 | */ |
||
537 | View Code Duplication | public function gamesTop($limit = null, $offset = null) |
|
548 | |||
549 | /** |
||
550 | * Get HTML code for stream embedding |
||
551 | * @param $channel |
||
552 | * @param int $width |
||
553 | * @param int $height |
||
554 | * @param int $volume |
||
555 | * @return string |
||
556 | * @deprecated Will be replaced with Embed method |
||
557 | */ |
||
558 | View Code Duplication | public function embedStream($channel, $width = 620, $height = 378, $volume = 25) |
|
578 | |||
579 | /** |
||
580 | * Get HTML code for video embedding |
||
581 | * @param $channel |
||
582 | * @param $chapterid |
||
583 | * @param int $width |
||
584 | * @param int $height |
||
585 | * @param int $volume |
||
586 | * @return string |
||
587 | * @deprecated Will be replaced with Embed method |
||
588 | */ |
||
589 | View Code Duplication | public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25) |
|
609 | |||
610 | /** |
||
611 | * Get HTML code for chat embedding |
||
612 | * @param $channel |
||
613 | * @param int $width |
||
614 | * @param int $height |
||
615 | * @return string |
||
616 | * @deprecated Will be replaced with Embed method |
||
617 | */ |
||
618 | public function embedChat($channel, $width = 400, $height = 300) |
||
628 | |||
629 | /** |
||
630 | * Get login URL for authentication |
||
631 | * @param string $scope Specify which permissions your app requires (space separated list) |
||
632 | * @return \stdClass |
||
633 | * @throws TwitchException |
||
634 | */ |
||
635 | View Code Duplication | public function authLoginURL($scope) |
|
650 | |||
651 | /** |
||
652 | * Get authentication access token |
||
653 | * @param string $code returned after app authorization by user |
||
654 | * @return \stdClass |
||
655 | * @throws TwitchException |
||
656 | */ |
||
657 | View Code Duplication | public function authAccessTokenGet($code) |
|
673 | |||
674 | /** |
||
675 | * Get the authenticated user |
||
676 | * - requires scope 'user_read' |
||
677 | * @param string |
||
678 | * @return \stdClass |
||
679 | * @throws TwitchException |
||
680 | */ |
||
681 | public function authUserGet($token) |
||
689 | |||
690 | /** |
||
691 | * Returns a list of blocks |
||
692 | * - required scope 'user_blocks_read' |
||
693 | * @param string $token |
||
694 | * @param string $user |
||
695 | * @param integer $limit |
||
696 | * @param integer $offset |
||
697 | * @return \stdClass |
||
698 | * @throws TwitchException |
||
699 | */ |
||
700 | public function authUserBlocks($token, $user, $limit = null, $offset = null) |
||
715 | |||
716 | /** |
||
717 | * Adds $target to $user block list |
||
718 | * - requires scope 'user_blocks_edit' |
||
719 | * @param string $token |
||
720 | * @param string $user |
||
721 | * @param string $target |
||
722 | * @return \stdClass |
||
723 | * @throws TwitchException |
||
724 | */ |
||
725 | View Code Duplication | public function blockTarget($token, $user, $target) |
|
733 | |||
734 | /** |
||
735 | * Removes $target from $user block list |
||
736 | * - requires scope 'user_blocks_edit' |
||
737 | * @param string $token |
||
738 | * @param string $user |
||
739 | * @param string $target |
||
740 | * @return \stdClass |
||
741 | * @throws TwitchException |
||
742 | */ |
||
743 | View Code Duplication | public function removeTarget($token, $user, $target) |
|
751 | |||
752 | /** |
||
753 | * Get the authenticated channel |
||
754 | * - requires scope 'channel_read' |
||
755 | * @param string |
||
756 | * @return \stdClass |
||
757 | * @throws TwitchException |
||
758 | */ |
||
759 | View Code Duplication | public function authChannelGet($token) |
|
767 | |||
768 | /** |
||
769 | * Update channel's status or game |
||
770 | * - requires scope 'channel_editor' |
||
771 | * @param $token |
||
772 | * @param string $channelName |
||
773 | * @param string $status |
||
774 | * @param string $game |
||
775 | * @param integer $delay |
||
776 | * @return \stdClass |
||
777 | * @throws TwitchException |
||
778 | */ |
||
779 | public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null) |
||
794 | |||
795 | /** |
||
796 | * Resets channel's stream key |
||
797 | * - requires scope 'channel_stream' |
||
798 | * @param string $token |
||
799 | * @param string $channelName |
||
800 | * @return \stdClass |
||
801 | * @throws TwitchException |
||
802 | */ |
||
803 | View Code Duplication | public function authChannelResetKey($token, $channelName) |
|
811 | |||
812 | /** |
||
813 | * Returns an array of users who are editors of specified channel |
||
814 | * - requires scope 'channel_read' |
||
815 | * @param string |
||
816 | * @param string |
||
817 | * @return \stdClass |
||
818 | * @throws TwitchException |
||
819 | */ |
||
820 | View Code Duplication | public function authChannelEditors($token, $channel) |
|
828 | |||
829 | /** |
||
830 | * Returns an array of subscriptions who are subscribed to specified channel |
||
831 | * - requires scope 'channel_subscriptions' |
||
832 | * @param string $token - user's access token |
||
833 | * @param string $channel |
||
834 | * @param integer $limit - can be up to 100 |
||
835 | * @param integer $offset |
||
836 | * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
||
837 | * @return \stdClass |
||
838 | * @throws TwitchException |
||
839 | */ |
||
840 | View Code Duplication | public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
|
856 | |||
857 | /** |
||
858 | * Returns user object if that user is subscribed |
||
859 | * - requires scope 'channel_check_subscription' for channel |
||
860 | * @param string $token |
||
861 | * @param string $channel |
||
862 | * @param string $user |
||
863 | * @return \stdClass |
||
864 | * @throws TwitchException |
||
865 | */ |
||
866 | View Code Duplication | public function authSubscribedUser($token, $channel, $user) |
|
874 | |||
875 | /** |
||
876 | * Returns a channel object that user subscribes to |
||
877 | * - requires scope 'user_subscriptions' for user |
||
878 | * @param string $token |
||
879 | * @param string $user |
||
880 | * @param string $channel |
||
881 | * @return \stdClass |
||
882 | * @throws TwitchException |
||
883 | */ |
||
884 | View Code Duplication | public function authSubscribedToChannel($token, $user, $channel) |
|
892 | |||
893 | /** |
||
894 | * List the live streams that the authenticated user is following |
||
895 | * - requires scope 'user_read' |
||
896 | * @param string |
||
897 | * @param integer $limit |
||
898 | * @param integer $offset |
||
899 | * @param bool $hls |
||
900 | * @return \stdClass |
||
901 | * @throws TwitchException |
||
902 | */ |
||
903 | View Code Duplication | public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
|
919 | |||
920 | /** |
||
921 | * Get streams helper |
||
922 | * @param null $game |
||
923 | * @param null $limit |
||
924 | * @param null $offset |
||
925 | * @param string|null $channels |
||
926 | * @param null $embeddable |
||
927 | * @param null $hls |
||
928 | * @return \stdClass |
||
929 | * @throws TwitchException |
||
930 | */ |
||
931 | public function getStreams($game = null, $limit = null, $offset = null, $channels = null, $embeddable = null, $hls = null) |
||
947 | |||
948 | /** |
||
949 | * Validate parameters for authentication |
||
950 | * @param array |
||
951 | * @return boolean |
||
952 | */ |
||
953 | private function configValidate($config) |
||
968 | |||
969 | /** |
||
970 | * Returns string for auth |
||
971 | * @param string $token |
||
972 | * @return null|string |
||
973 | * @throws TwitchException |
||
974 | */ |
||
975 | private function getAuthString($token) |
||
982 | |||
983 | /** |
||
984 | * Configuration exception |
||
985 | * @throws TwitchException |
||
986 | */ |
||
987 | private function configException() |
||
991 | } |
||
992 |