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 | * TwitchSDK constructor |
||
| 30 | * @param array $config |
||
| 31 | * @throws TwitchException |
||
| 32 | */ |
||
| 33 | public function __construct(array $config = array()) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * config setter |
||
| 49 | * @param array $config |
||
| 50 | * @return TwitchSDK |
||
| 51 | * @throws TwitchException |
||
| 52 | */ |
||
| 53 | public function setConfig(array $config) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Check if config is set |
||
| 66 | * @throws TwitchException |
||
| 67 | */ |
||
| 68 | private function checkConfig() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get value from config |
||
| 77 | * @param string $key |
||
| 78 | * @return mixed |
||
| 79 | * @throws TwitchException |
||
| 80 | */ |
||
| 81 | private function getConfigParam($key) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Basic information about the API and authentication status |
||
| 92 | * @param null $token |
||
| 93 | * @return \stdClass |
||
| 94 | * @throws TwitchException |
||
| 95 | */ |
||
| 96 | public function status($token = null) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get the specified user |
||
| 113 | * @param $username |
||
| 114 | * @return \stdClass |
||
| 115 | * @throws TwitchException |
||
| 116 | */ |
||
| 117 | public function userGet($username) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get a user's list of followed channels |
||
| 126 | * @param string $user |
||
| 127 | * @param integer $limit |
||
| 128 | * @param integer $offset |
||
| 129 | * @param string $direction |
||
| 130 | * @param string $sortby |
||
| 131 | * @return \stdClass |
||
| 132 | * @throws TwitchException |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function userFollowChannels($user, $limit = null, $offset = null, $direction = null, $sortby = null) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Get the status of a follow relationship |
||
| 150 | * @param string $user |
||
| 151 | * @param string $channel |
||
| 152 | * @return \stdClass |
||
| 153 | * @throws TwitchException |
||
| 154 | */ |
||
| 155 | public function userFollowRelationship($user, $channel) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set user to follow given channel |
||
| 164 | * - requires scope 'user_follows_edit' |
||
| 165 | * @param string $user |
||
| 166 | * @param string $channel |
||
| 167 | * @param string $userToken |
||
| 168 | * @param bool $notifications |
||
| 169 | * @return \stdClass |
||
| 170 | * @throws TwitchException |
||
| 171 | */ |
||
| 172 | View Code Duplication | public function authUserFollowChannel($user, $channel, $userToken, $notifications = false) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Set user to unfollow given channel |
||
| 188 | * - requires scope 'user_follows_edit' |
||
| 189 | * @param string $user |
||
| 190 | * @param string $channel |
||
| 191 | * @param string $userToken |
||
| 192 | * @return \stdClass |
||
| 193 | * @throws TwitchException |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function authUserUnfollowChannel($user, $channel, $userToken) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Get the specified channel |
||
| 210 | * @param string $channelName |
||
| 211 | * @return \stdClass |
||
| 212 | * @throws TwitchException |
||
| 213 | */ |
||
| 214 | public function channelGet($channelName) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Return team list for specified channel |
||
| 223 | * @param string $channelName |
||
| 224 | * @return \stdClass |
||
| 225 | * @throws TwitchException |
||
| 226 | */ |
||
| 227 | public function channelTeamsGet($channelName) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Search channels |
||
| 236 | * @param string $query |
||
| 237 | * @param integer $limit |
||
| 238 | * @param integer $offset |
||
| 239 | * @return \stdClass |
||
| 240 | * @throws TwitchException |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function channelSearch($query, $limit = null, $offset = null) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Get the specified team |
||
| 257 | * @param $teamName |
||
| 258 | * @return \stdClass |
||
| 259 | * @throws TwitchException |
||
| 260 | */ |
||
| 261 | public function teamGet($teamName) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Returns a list of active teams |
||
| 270 | * @param integer $limit |
||
| 271 | * @param integer $offset |
||
| 272 | * @return \stdClass |
||
| 273 | * @throws TwitchException |
||
| 274 | */ |
||
| 275 | View Code Duplication | public function teamList($limit = null, $offset = null) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Get all team members |
||
| 289 | * @param $teamName |
||
| 290 | * @return mixed |
||
| 291 | * @throws TwitchException |
||
| 292 | */ |
||
| 293 | public function teamMembersAll($teamName) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Returns an array of users who follow the specified channel |
||
| 300 | * @param string $channelName |
||
| 301 | * @param integer $limit |
||
| 302 | * @param integer $offset |
||
| 303 | * @param string $cursor |
||
| 304 | * @param string $direction |
||
| 305 | * @return \stdClass |
||
| 306 | * @throws TwitchException |
||
| 307 | */ |
||
| 308 | View Code Duplication | public function channelFollows($channelName, $limit = null, $offset = null, $cursor = null, $direction = null) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Get the specified channel's stream |
||
| 324 | * @param $channel |
||
| 325 | * @return \stdClass |
||
| 326 | * @throws TwitchException |
||
| 327 | */ |
||
| 328 | public function streamGet($channel) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Search live streams |
||
| 337 | * @param string $query |
||
| 338 | * @param null $limit |
||
| 339 | * @param null $offset |
||
| 340 | * @param null $hls |
||
| 341 | * @return \stdClass |
||
| 342 | * @throws TwitchException |
||
| 343 | */ |
||
| 344 | public function streamSearch($query, $limit = null, $offset = null, $hls = null) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Summarize streams |
||
| 360 | * @param null $game |
||
| 361 | * @param array|null $channels |
||
| 362 | * @param null $hls |
||
| 363 | * @return \stdClass |
||
| 364 | * @throws TwitchException |
||
| 365 | */ |
||
| 366 | View Code Duplication | public function streamsSummarize($game = null, array $channels = null, $hls = null) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Get featured streams |
||
| 385 | * @param null $limit |
||
| 386 | * @param null $offset |
||
| 387 | * @param null $hls |
||
| 388 | * @return \stdClass |
||
| 389 | * @throws TwitchException |
||
| 390 | */ |
||
| 391 | View Code Duplication | public function streamsFeatured($limit = null, $offset = null, $hls = null) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Get video |
||
| 406 | * @param string $videoId |
||
| 407 | * @return \stdClass |
||
| 408 | * @throws TwitchException |
||
| 409 | */ |
||
| 410 | public function videoGet($videoId) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Returns top videos |
||
| 419 | * @param integer $limit |
||
| 420 | * @param integer $offset |
||
| 421 | * @param string $game |
||
| 422 | * @param string $period |
||
| 423 | * @return \stdClass |
||
| 424 | * @throws TwitchException |
||
| 425 | */ |
||
| 426 | View Code Duplication | public function videosTop($limit = null, $offset = null, $game = null, $period = null) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Get videos for a channel |
||
| 442 | * @param $channel |
||
| 443 | * @param null $limit |
||
| 444 | * @param null $offset |
||
| 445 | * @param bool $broadcasts |
||
| 446 | * @param bool $hls |
||
| 447 | * @return \stdClass |
||
| 448 | * @throws TwitchException |
||
| 449 | */ |
||
| 450 | View Code Duplication | public function videosByChannel($channel, $limit = null, $offset = null, $broadcasts = null, $hls = null) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Returns a links object to all other chat endpoints |
||
| 466 | * @param string $channelName |
||
| 467 | * @return \stdClass |
||
| 468 | * @throws TwitchException |
||
| 469 | */ |
||
| 470 | public function chatGet($channelName) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get a chat's emoticons |
||
| 479 | * @return \stdClass |
||
| 480 | * @throws TwitchException |
||
| 481 | */ |
||
| 482 | public function chatEmoticons() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Returns a list of emoticons |
||
| 491 | * @param string $emoteset |
||
| 492 | * @return \stdClass |
||
| 493 | * @throws TwitchException |
||
| 494 | */ |
||
| 495 | View Code Duplication | public function chatEmoticonsImages($emoteset = null) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Returns a list of chat badges |
||
| 508 | * @param string $channelName |
||
| 509 | * @return \stdClass |
||
| 510 | * @throws TwitchException |
||
| 511 | */ |
||
| 512 | public function chatBadges($channelName) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Get top games |
||
| 521 | * @param integer $limit |
||
| 522 | * @param integer $offset |
||
| 523 | * @return \stdClass |
||
| 524 | * @throws TwitchException |
||
| 525 | */ |
||
| 526 | View Code Duplication | public function gamesTop($limit = null, $offset = null) |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Search games |
||
| 540 | * @param string $query |
||
| 541 | * @param null $type |
||
| 542 | * @param null $live |
||
| 543 | * @return \stdClass |
||
| 544 | * @throws TwitchException |
||
| 545 | */ |
||
| 546 | View Code Duplication | public function gameSearch($query, $type = null, $live = null) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Get HTML code for stream embedding |
||
| 561 | * @param $channel |
||
| 562 | * @param int $width |
||
| 563 | * @param int $height |
||
| 564 | * @param int $volume |
||
| 565 | * @return string |
||
| 566 | * @deprecated Will be replaced with Embed method |
||
| 567 | */ |
||
| 568 | View Code Duplication | public function embedStream($channel, $width = 620, $height = 378, $volume = 25) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Get HTML code for video embedding |
||
| 591 | * @param $channel |
||
| 592 | * @param $chapterid |
||
| 593 | * @param int $width |
||
| 594 | * @param int $height |
||
| 595 | * @param int $volume |
||
| 596 | * @return string |
||
| 597 | * @deprecated Will be replaced with Embed method |
||
| 598 | */ |
||
| 599 | View Code Duplication | public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25) |
|
| 619 | |||
| 620 | /** |
||
| 621 | * Get HTML code for chat embedding |
||
| 622 | * @param $channel |
||
| 623 | * @param int $width |
||
| 624 | * @param int $height |
||
| 625 | * @return string |
||
| 626 | * @deprecated Will be replaced with Embed method |
||
| 627 | */ |
||
| 628 | public function embedChat($channel, $width = 400, $height = 300) |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Get login URL for authentication |
||
| 641 | * @param string $scope Specify which permissions your app requires (space separated list) |
||
| 642 | * @return \stdClass |
||
| 643 | * @throws TwitchException |
||
| 644 | */ |
||
| 645 | View Code Duplication | public function authLoginURL($scope) |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Get authentication access token |
||
| 663 | * @param string $code returned after app authorization by user |
||
| 664 | * @return \stdClass |
||
| 665 | * @throws TwitchException |
||
| 666 | */ |
||
| 667 | View Code Duplication | public function authAccessTokenGet($code) |
|
| 683 | |||
| 684 | /** |
||
| 685 | * Get the authenticated user |
||
| 686 | * - requires scope 'user_read' |
||
| 687 | * @param string |
||
| 688 | * @return \stdClass |
||
| 689 | * @throws TwitchException |
||
| 690 | */ |
||
| 691 | public function authUserGet($token) |
||
| 699 | |||
| 700 | /** |
||
| 701 | * Returns a list of blocks |
||
| 702 | * - required scope 'user_blocks_read' |
||
| 703 | * @param string $token |
||
| 704 | * @param string $user |
||
| 705 | * @param integer $limit |
||
| 706 | * @param integer $offset |
||
| 707 | * @return \stdClass |
||
| 708 | * @throws TwitchException |
||
| 709 | */ |
||
| 710 | public function authUserBlocks($token, $user, $limit = null, $offset = null) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Adds $target to $user block list |
||
| 728 | * - requires scope 'user_blocks_edit' |
||
| 729 | * @param string $token |
||
| 730 | * @param string $user |
||
| 731 | * @param string $target |
||
| 732 | * @return \stdClass |
||
| 733 | * @throws TwitchException |
||
| 734 | */ |
||
| 735 | View Code Duplication | public function authBlockTarget($token, $user, $target) |
|
| 743 | |||
| 744 | /** |
||
| 745 | * Removes $target from $user block list |
||
| 746 | * - requires scope 'user_blocks_edit' |
||
| 747 | * @param string $token |
||
| 748 | * @param string $user |
||
| 749 | * @param string $target |
||
| 750 | * @return \stdClass |
||
| 751 | * @throws TwitchException |
||
| 752 | */ |
||
| 753 | View Code Duplication | public function authRemoveTarget($token, $user, $target) |
|
| 761 | |||
| 762 | /** |
||
| 763 | * Get the authenticated channel |
||
| 764 | * - requires scope 'channel_read' |
||
| 765 | * @param string |
||
| 766 | * @return \stdClass |
||
| 767 | * @throws TwitchException |
||
| 768 | */ |
||
| 769 | View Code Duplication | public function authChannelGet($token) |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Update channel's status or game |
||
| 780 | * - requires scope 'channel_editor' |
||
| 781 | * @param $token |
||
| 782 | * @param string $channelName |
||
| 783 | * @param string $status |
||
| 784 | * @param string $game |
||
| 785 | * @param integer $delay |
||
| 786 | * @return \stdClass |
||
| 787 | * @throws TwitchException |
||
| 788 | */ |
||
| 789 | public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null) |
||
| 804 | |||
| 805 | /** |
||
| 806 | * Resets channel's stream key |
||
| 807 | * - requires scope 'channel_stream' |
||
| 808 | * @param string $token |
||
| 809 | * @param string $channelName |
||
| 810 | * @return \stdClass |
||
| 811 | * @throws TwitchException |
||
| 812 | */ |
||
| 813 | View Code Duplication | public function authChannelResetKey($token, $channelName) |
|
| 821 | |||
| 822 | /** |
||
| 823 | * Returns an array of users who are editors of specified channel |
||
| 824 | * - requires scope 'channel_read' |
||
| 825 | * @param string |
||
| 826 | * @param string |
||
| 827 | * @return \stdClass |
||
| 828 | * @throws TwitchException |
||
| 829 | */ |
||
| 830 | View Code Duplication | public function authChannelEditors($token, $channel) |
|
| 838 | |||
| 839 | /** |
||
| 840 | * Returns an array of subscriptions who are subscribed to specified channel |
||
| 841 | * - requires scope 'channel_subscriptions' |
||
| 842 | * @param string $token - user's access token |
||
| 843 | * @param string $channel |
||
| 844 | * @param integer $limit - can be up to 100 |
||
| 845 | * @param integer $offset |
||
| 846 | * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
||
| 847 | * @return \stdClass |
||
| 848 | * @throws TwitchException |
||
| 849 | */ |
||
| 850 | View Code Duplication | public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
|
| 866 | |||
| 867 | /** |
||
| 868 | * Returns user object if that user is subscribed |
||
| 869 | * - requires scope 'channel_check_subscription' for channel |
||
| 870 | * @param string $token |
||
| 871 | * @param string $channel |
||
| 872 | * @param string $user |
||
| 873 | * @return \stdClass |
||
| 874 | * @throws TwitchException |
||
| 875 | */ |
||
| 876 | View Code Duplication | public function authSubscribedUser($token, $channel, $user) |
|
| 884 | |||
| 885 | /** |
||
| 886 | * Returns a channel object that user subscribes to |
||
| 887 | * - requires scope 'user_subscriptions' for user |
||
| 888 | * @param string $token |
||
| 889 | * @param string $user |
||
| 890 | * @param string $channel |
||
| 891 | * @return \stdClass |
||
| 892 | * @throws TwitchException |
||
| 893 | */ |
||
| 894 | View Code Duplication | public function authSubscribedToChannel($token, $user, $channel) |
|
| 902 | |||
| 903 | /** |
||
| 904 | * List the live streams that the authenticated user is following |
||
| 905 | * - requires scope 'user_read' |
||
| 906 | * @param string |
||
| 907 | * @param integer $limit |
||
| 908 | * @param integer $offset |
||
| 909 | * @param bool $hls |
||
| 910 | * @return \stdClass |
||
| 911 | * @throws TwitchException |
||
| 912 | */ |
||
| 913 | View Code Duplication | public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
|
| 929 | |||
| 930 | /** |
||
| 931 | * Get streams helper |
||
| 932 | * @param null $game |
||
| 933 | * @param string|null $channels |
||
| 934 | * @param null $limit |
||
| 935 | * @param null $offset |
||
| 936 | * @param null $embeddable |
||
| 937 | * @param null $hls |
||
| 938 | * @return \stdClass |
||
| 939 | * @throws TwitchException |
||
| 940 | */ |
||
| 941 | public function getStreams($game = null, $channels = null, $limit = null, $offset = null, $embeddable = null, $hls = null) |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Validate parameters for authentication |
||
| 960 | * @param array |
||
| 961 | * @return boolean |
||
| 962 | */ |
||
| 963 | private function configValidate($config) |
||
| 978 | |||
| 979 | /** |
||
| 980 | * Returns string for auth |
||
| 981 | * @param string $token |
||
| 982 | * @return null|string |
||
| 983 | * @throws TwitchException |
||
| 984 | */ |
||
| 985 | private function getAuthString($token) |
||
| 992 | |||
| 993 | /** |
||
| 994 | * Configuration exception |
||
| 995 | * @throws TwitchException |
||
| 996 | */ |
||
| 997 | private function configException() |
||
| 1001 | } |
||
| 1002 |