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 |
||
18 | class TwitchSDK |
||
19 | { |
||
20 | /** @var array */ |
||
21 | private $config = array(); |
||
22 | |||
23 | /** @var TwitchRequest */ |
||
24 | protected $request; |
||
25 | |||
26 | /** @var Helper */ |
||
27 | protected $helper; |
||
28 | |||
29 | /** |
||
30 | * TwitchAPI URI's |
||
31 | */ |
||
32 | const URI_STREAMS_SEARCH = 'search/streams/'; |
||
33 | |||
34 | /** |
||
35 | * TwitchSDK constructor |
||
36 | * @param array $config |
||
37 | * @throws TwitchException |
||
38 | */ |
||
39 | public function __construct(array $config = array()) |
||
56 | |||
57 | /** |
||
58 | * config setter |
||
59 | * @param array $config |
||
60 | * @return TwitchSDK |
||
61 | * @throws TwitchException |
||
62 | */ |
||
63 | public function setConfig(array $config) |
||
73 | |||
74 | /** |
||
75 | * Check if config is set |
||
76 | * @throws TwitchException |
||
77 | */ |
||
78 | private function checkConfig() |
||
84 | |||
85 | /** |
||
86 | * Get value from config |
||
87 | * @param string $key |
||
88 | * @return mixed |
||
89 | * @throws TwitchException |
||
90 | */ |
||
91 | private function getConfigParam($key) |
||
99 | |||
100 | /** |
||
101 | * Basic information about the API and authentication status |
||
102 | * @param null $token |
||
103 | * @return \stdClass |
||
104 | * @throws TwitchException |
||
105 | */ |
||
106 | public function status($token = null) |
||
120 | |||
121 | /** |
||
122 | * Get the specified user |
||
123 | * @param $username |
||
124 | * @return \stdClass |
||
125 | * @throws TwitchException |
||
126 | */ |
||
127 | public function userGet($username) |
||
132 | |||
133 | /** |
||
134 | * Get a user's list of followed channels |
||
135 | * @param string $user |
||
136 | * @param integer $limit |
||
137 | * @param integer $offset |
||
138 | * @param string $direction |
||
139 | * @param string $sortby |
||
140 | * @return \stdClass |
||
141 | * @throws TwitchException |
||
142 | */ |
||
143 | View Code Duplication | public function userFollowChannels($user, $limit = null, $offset = null, $direction = null, $sortby = null) |
|
156 | |||
157 | /** |
||
158 | * Get the status of a follow relationship |
||
159 | * @param string $user |
||
160 | * @param string $channel |
||
161 | * @return \stdClass |
||
162 | * @throws TwitchException |
||
163 | */ |
||
164 | public function userFollowRelationship($user, $channel) |
||
170 | |||
171 | /** |
||
172 | * Set user to follow given channel |
||
173 | * - requires scope 'user_follows_edit' |
||
174 | * @param string $user |
||
175 | * @param string $channel |
||
176 | * @param string $userToken |
||
177 | * @param bool $notifications |
||
178 | * @return \stdClass |
||
179 | * @throws TwitchException |
||
180 | */ |
||
181 | View Code Duplication | public function userFollowChannel($user, $channel, $userToken, $notifications = false) |
|
192 | |||
193 | /** |
||
194 | * Set user to unfollow given channel |
||
195 | * - requires scope 'user_follows_edit' |
||
196 | * @param string $user |
||
197 | * @param string $channel |
||
198 | * @param string $userToken |
||
199 | * @return \stdClass |
||
200 | * @throws TwitchException |
||
201 | */ |
||
202 | View Code Duplication | public function userUnfollowChannel($user, $channel, $userToken) |
|
212 | |||
213 | /** |
||
214 | * Get the specified channel |
||
215 | * @param string $channelName |
||
216 | * @return \stdClass |
||
217 | * @throws TwitchException |
||
218 | */ |
||
219 | public function channelGet($channelName) |
||
225 | |||
226 | /** |
||
227 | * Return team list for specified channel |
||
228 | * @param string $channelName |
||
229 | * @return \stdClass |
||
230 | * @throws TwitchException |
||
231 | */ |
||
232 | public function channelTeamsGet($channelName) |
||
238 | |||
239 | /** |
||
240 | * Get the specified team |
||
241 | * @param $teamName |
||
242 | * @return \stdClass |
||
243 | * @throws TwitchException |
||
244 | */ |
||
245 | public function teamGet($teamName) |
||
251 | |||
252 | /** |
||
253 | * Returns a list of active teams |
||
254 | * @param integer $limit |
||
255 | * @param integer $offset |
||
256 | * @return \stdClass |
||
257 | * @throws TwitchException |
||
258 | */ |
||
259 | View Code Duplication | public function teamList($limit = null, $offset = null) |
|
270 | |||
271 | /** |
||
272 | * Get all team members |
||
273 | * @param $teamName |
||
274 | * @return mixed |
||
275 | * @throws TwitchException |
||
276 | */ |
||
277 | public function teamMembersAll($teamName) |
||
281 | |||
282 | /** |
||
283 | * Returns an array of users who follow the specified channel |
||
284 | * @param string $channelName |
||
285 | * @param integer $limit |
||
286 | * @param integer $offset |
||
287 | * @param string $cursor |
||
288 | * @param string $direction |
||
289 | * @return \stdClass |
||
290 | * @throws TwitchException |
||
291 | */ |
||
292 | View Code Duplication | public function channelFollows($channelName, $limit = null, $offset = null, $cursor = null, $direction = null) |
|
305 | |||
306 | /** |
||
307 | * Get the specified channel's stream |
||
308 | * @param $channel |
||
309 | * @return \stdClass |
||
310 | * @throws TwitchException |
||
311 | */ |
||
312 | public function streamGet($channel) |
||
318 | |||
319 | /** |
||
320 | * Search live streams |
||
321 | * @param $query |
||
322 | * @param null $limit |
||
323 | * @param null $offset |
||
324 | * @return \stdClass |
||
325 | * @throws TwitchException |
||
326 | * @deprecated will be replaced by getStreams() function |
||
327 | */ |
||
328 | View Code Duplication | public function streamSearch($query, $limit = null, $offset = null) |
|
338 | |||
339 | /** |
||
340 | * Summarize streams |
||
341 | * @param null $game |
||
342 | * @param array|null $channels |
||
343 | * @param null $hls |
||
344 | * @return \stdClass |
||
345 | * @throws TwitchException |
||
346 | */ |
||
347 | View Code Duplication | public function streamsSummarize($game = null, array $channels = null, $hls = null) |
|
363 | |||
364 | /** |
||
365 | * Get featured streams |
||
366 | * @param null $limit |
||
367 | * @param null $offset |
||
368 | * @param null $hls |
||
369 | * @return \stdClass |
||
370 | * @throws TwitchException |
||
371 | */ |
||
372 | View Code Duplication | public function streamsFeatured($limit = null, $offset = null, $hls = null) |
|
384 | |||
385 | /** |
||
386 | * Get streams by channel |
||
387 | * @param $channels |
||
388 | * @param null $limit |
||
389 | * @param null $offset |
||
390 | * @param null $embeddable |
||
391 | * @param null $hls |
||
392 | * @return \stdClass |
||
393 | * @deprecated will be replaced by getStreams() function |
||
394 | */ |
||
395 | public function streamsByChannels($channels, $limit = null, $offset = null, $embeddable = null, $hls = null) |
||
401 | |||
402 | /** |
||
403 | * Get streams by game |
||
404 | * @param $game |
||
405 | * @param null $limit |
||
406 | * @param null $offset |
||
407 | * @param null $embeddable |
||
408 | * @param null $hls |
||
409 | * @return \stdClass |
||
410 | * @deprecated will be replaced by getStreams() function |
||
411 | */ |
||
412 | public function streamsByGame($game, $limit = null, $offset = null, $embeddable = null, $hls = null) |
||
416 | |||
417 | /** |
||
418 | * Get video |
||
419 | * @param string $videoId |
||
420 | * @return \stdClass |
||
421 | * @throws TwitchException |
||
422 | */ |
||
423 | public function videoGet($videoId) |
||
429 | |||
430 | /** |
||
431 | * Returns top videos |
||
432 | * @param integer $limit |
||
433 | * @param integer $offset |
||
434 | * @param string $game |
||
435 | * @param string $period |
||
436 | * @return \stdClass |
||
437 | * @throws TwitchException |
||
438 | */ |
||
439 | public function videosTop($limit = null, $offset = null, $game = null, $period = null) |
||
452 | |||
453 | /** |
||
454 | * Get videos for a channel |
||
455 | * @param $channel |
||
456 | * @param null $limit |
||
457 | * @param null $offset |
||
458 | * @param bool $broadcasts |
||
459 | * @param bool $hls |
||
460 | * @return \stdClass |
||
461 | * @throws TwitchException |
||
462 | */ |
||
463 | View Code Duplication | public function videosByChannel($channel, $limit = null, $offset = null, $broadcasts = null, $hls = null) |
|
476 | |||
477 | /** |
||
478 | * Returns a links object to all other chat endpoints |
||
479 | * @param string $channelName |
||
480 | * @return \stdClass |
||
481 | * @throws TwitchException |
||
482 | */ |
||
483 | public function chatGet($channelName) |
||
489 | |||
490 | /** |
||
491 | * Get a chat's emoticons |
||
492 | * @return \stdClass |
||
493 | * @throws TwitchException |
||
494 | */ |
||
495 | public function chatEmoticons() |
||
501 | |||
502 | /** |
||
503 | * Returns a list of emoticons |
||
504 | * @param string $emoteset |
||
505 | * @return \stdClass |
||
506 | * @throws TwitchException |
||
507 | */ |
||
508 | View Code Duplication | public function chatEmoticonsImages($emoteset = null) |
|
518 | |||
519 | /** |
||
520 | * Returns a list of chat badges |
||
521 | * @param string $channelName |
||
522 | * @return \stdClass |
||
523 | * @throws TwitchException |
||
524 | */ |
||
525 | public function chatBadges($channelName) |
||
531 | |||
532 | /** |
||
533 | * Get top games |
||
534 | * @param integer $limit |
||
535 | * @param integer $offset |
||
536 | * @return \stdClass |
||
537 | * @throws TwitchException |
||
538 | */ |
||
539 | View Code Duplication | public function gamesTop($limit = null, $offset = null) |
|
550 | |||
551 | /** |
||
552 | * Get HTML code for stream embedding |
||
553 | * @param $channel |
||
554 | * @param int $width |
||
555 | * @param int $height |
||
556 | * @param int $volume |
||
557 | * @return string |
||
558 | */ |
||
559 | View Code Duplication | public function embedStream($channel, $width = 620, $height = 378, $volume = 25) |
|
579 | |||
580 | /** |
||
581 | * Get HTML code for video embedding |
||
582 | * @param $channel |
||
583 | * @param $chapterid |
||
584 | * @param int $width |
||
585 | * @param int $height |
||
586 | * @param int $volume |
||
587 | * @return string |
||
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 | */ |
||
617 | public function embedChat($channel, $width = 400, $height = 300) |
||
627 | |||
628 | /** |
||
629 | * Get login URL for authentication |
||
630 | * @param string $scope Specify which permissions your app requires (space separated list) |
||
631 | * @return \stdClass |
||
632 | * @throws TwitchException |
||
633 | */ |
||
634 | View Code Duplication | public function authLoginURL($scope) |
|
649 | |||
650 | /** |
||
651 | * Get authentication access token |
||
652 | * @param string $code returned after app authorization by user |
||
653 | * @return \stdClass |
||
654 | * @throws TwitchException |
||
655 | */ |
||
656 | View Code Duplication | public function authAccessTokenGet($code) |
|
672 | |||
673 | /** |
||
674 | * Get the authenticated user |
||
675 | * - requires scope 'user_read' |
||
676 | * @param string |
||
677 | * @return \stdClass |
||
678 | * @throws TwitchException |
||
679 | */ |
||
680 | public function authUserGet($token) |
||
689 | |||
690 | /** |
||
691 | * Get the authenticated channel |
||
692 | * - requires scope 'channel_read' |
||
693 | * @param string |
||
694 | * @return \stdClass |
||
695 | * @throws TwitchException |
||
696 | */ |
||
697 | View Code Duplication | public function authChannelGet($token) |
|
707 | |||
708 | /** |
||
709 | * Update channel's status or game |
||
710 | * - requires scope 'channel_editor' |
||
711 | * @param $token |
||
712 | * @param string $channelName |
||
713 | * @param string $status |
||
714 | * @param string $game |
||
715 | * @param integer $delay |
||
716 | * @return \stdClass |
||
717 | * @throws TwitchException |
||
718 | */ |
||
719 | public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null) |
||
735 | |||
736 | /** |
||
737 | * Resets channel's stream key |
||
738 | * - requires scope 'channel_stream' |
||
739 | * @param string $token |
||
740 | * @param string $channelName |
||
741 | * @return \stdClass |
||
742 | * @throws TwitchException |
||
743 | */ |
||
744 | View Code Duplication | public function authChannelResetKey($token, $channelName) |
|
754 | |||
755 | /** |
||
756 | * Returns an array of users who are editors of specified channel |
||
757 | * - requires scope 'channel_read' |
||
758 | * @param string |
||
759 | * @param string |
||
760 | * @return \stdClass |
||
761 | * @throws TwitchException |
||
762 | */ |
||
763 | View Code Duplication | public function authChannelEditors($token, $channel) |
|
773 | |||
774 | /** |
||
775 | * Returns an array of subscriptions who are subscribed to specified channel |
||
776 | * - requires scope 'channel_subscriptions' |
||
777 | * @param string $token - user's access token |
||
778 | * @param string $channel |
||
779 | * @param integer $limit - can be up to 100 |
||
780 | * @param integer $offset |
||
781 | * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
||
782 | * @return \stdClass |
||
783 | * @throws TwitchException |
||
784 | */ |
||
785 | View Code Duplication | public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
|
801 | |||
802 | /** |
||
803 | * Returns user object if that user is subscribed |
||
804 | * - requires scope 'channel_check_subscription' for channel |
||
805 | * @param string $token |
||
806 | * @param string $channel |
||
807 | * @param string $user |
||
808 | * @return \stdClass |
||
809 | * @throws TwitchException |
||
810 | */ |
||
811 | View Code Duplication | public function authSubscribedUser($token, $channel, $user) |
|
821 | |||
822 | /** |
||
823 | * Returns a channel object that user subscribes to |
||
824 | * - requires scope 'user_subscriptions' for user |
||
825 | * @param string $token |
||
826 | * @param string $user |
||
827 | * @param string $channel |
||
828 | * @return \stdClass |
||
829 | * @throws TwitchException |
||
830 | */ |
||
831 | View Code Duplication | public function authSubscribedToChannel($token, $user, $channel) |
|
841 | |||
842 | /** |
||
843 | * List the live streams that the authenticated user is following |
||
844 | * - requires scope 'user_read' |
||
845 | * @param string |
||
846 | * @param integer $limit |
||
847 | * @param integer $offset |
||
848 | * @param bool $hls |
||
849 | * @return \stdClass |
||
850 | * @throws TwitchException |
||
851 | */ |
||
852 | View Code Duplication | public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
|
867 | |||
868 | /** |
||
869 | * Get streams helper |
||
870 | * @param null $game |
||
871 | * @param null $limit |
||
872 | * @param null $offset |
||
873 | * @param string|null $channels |
||
874 | * @param null $embeddable |
||
875 | * @param null $hls |
||
876 | * @return \stdClass |
||
877 | * @throws TwitchException |
||
878 | */ |
||
879 | public function getStreams($game = null, $limit = null, $offset = null, $channels = null, $embeddable = null, $hls = null) |
||
895 | |||
896 | /** |
||
897 | * Validate parameters for authentication |
||
898 | * @param array |
||
899 | * @return boolean |
||
900 | */ |
||
901 | private function configValidate($config) |
||
916 | |||
917 | /** |
||
918 | * Returns string for auth |
||
919 | * @param string $token |
||
920 | * @return null|string |
||
921 | * @throws TwitchException |
||
922 | */ |
||
923 | private function getAuthString($token) |
||
930 | |||
931 | /** |
||
932 | * Configuration exception |
||
933 | * @throws TwitchException |
||
934 | */ |
||
935 | private function configException() |
||
939 | } |
||
940 |
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.