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 | public function userFollowChannel($user, $channel, $userToken, $notifications = false) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set user to unfollow given channel |
||
| 186 | * - requires scope 'user_follows_edit' |
||
| 187 | * @param string $user |
||
| 188 | * @param string $channel |
||
| 189 | * @param string $userToken |
||
| 190 | * @return \stdClass |
||
| 191 | * @throws TwitchException |
||
| 192 | */ |
||
| 193 | View Code Duplication | public function userUnfollowChannel($user, $channel, $userToken) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Get the specified channel |
||
| 206 | * @param string $channelName |
||
| 207 | * @return \stdClass |
||
| 208 | * @throws TwitchException |
||
| 209 | */ |
||
| 210 | public function channelGet($channelName) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Return team list for specified channel |
||
| 219 | * @param string $channelName |
||
| 220 | * @return \stdClass |
||
| 221 | * @throws TwitchException |
||
| 222 | */ |
||
| 223 | public function channelTeamsGet($channelName) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Search channels |
||
| 232 | * @param string $query |
||
| 233 | * @param integer $limit |
||
| 234 | * @param integer $offset |
||
| 235 | * @return \stdClass |
||
| 236 | * @throws TwitchException |
||
| 237 | */ |
||
| 238 | View Code Duplication | public function channelSearch($query, $limit = null, $offset = null) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Get the specified team |
||
| 253 | * @param $teamName |
||
| 254 | * @return \stdClass |
||
| 255 | * @throws TwitchException |
||
| 256 | */ |
||
| 257 | public function teamGet($teamName) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Returns a list of active teams |
||
| 266 | * @param integer $limit |
||
| 267 | * @param integer $offset |
||
| 268 | * @return \stdClass |
||
| 269 | * @throws TwitchException |
||
| 270 | */ |
||
| 271 | View Code Duplication | public function teamList($limit = null, $offset = null) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Get all team members |
||
| 285 | * @param $teamName |
||
| 286 | * @return mixed |
||
| 287 | * @throws TwitchException |
||
| 288 | */ |
||
| 289 | public function teamMembersAll($teamName) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Returns an array of users who follow the specified channel |
||
| 296 | * @param string $channelName |
||
| 297 | * @param integer $limit |
||
| 298 | * @param integer $offset |
||
| 299 | * @param string $cursor |
||
| 300 | * @param string $direction |
||
| 301 | * @return \stdClass |
||
| 302 | * @throws TwitchException |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function channelFollows($channelName, $limit = null, $offset = null, $cursor = null, $direction = null) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Get the specified channel's stream |
||
| 320 | * @param $channel |
||
| 321 | * @return \stdClass |
||
| 322 | * @throws TwitchException |
||
| 323 | */ |
||
| 324 | public function streamGet($channel) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Search live streams |
||
| 333 | * @param string $query |
||
| 334 | * @param null $limit |
||
| 335 | * @param null $offset |
||
| 336 | * @param null $hls |
||
| 337 | * @return \stdClass |
||
| 338 | * @throws TwitchException |
||
| 339 | */ |
||
| 340 | public function streamSearch($query, $limit = null, $offset = null, $hls = null) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Summarize streams |
||
| 356 | * @param null $game |
||
| 357 | * @param array|null $channels |
||
| 358 | * @param null $hls |
||
| 359 | * @return \stdClass |
||
| 360 | * @throws TwitchException |
||
| 361 | */ |
||
| 362 | View Code Duplication | public function streamsSummarize($game = null, array $channels = null, $hls = null) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Get featured streams |
||
| 381 | * @param null $limit |
||
| 382 | * @param null $offset |
||
| 383 | * @param null $hls |
||
| 384 | * @return \stdClass |
||
| 385 | * @throws TwitchException |
||
| 386 | */ |
||
| 387 | View Code Duplication | public function streamsFeatured($limit = null, $offset = null, $hls = null) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Get streams by channel |
||
| 402 | * @param $channels |
||
| 403 | * @param null $limit |
||
| 404 | * @param null $offset |
||
| 405 | * @param null $embeddable |
||
| 406 | * @param null $hls |
||
| 407 | * @return \stdClass |
||
| 408 | * @throws TwitchException |
||
| 409 | * @deprecated will be replaced by getStreams() function |
||
| 410 | */ |
||
| 411 | public function streamsByChannels($channels, $limit = null, $offset = null, $embeddable = null, $hls = null) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Get streams by game |
||
| 420 | * @param $game |
||
| 421 | * @param null $limit |
||
| 422 | * @param null $offset |
||
| 423 | * @param null $embeddable |
||
| 424 | * @param null $hls |
||
| 425 | * @return \stdClass |
||
| 426 | * @throws TwitchException |
||
| 427 | * @deprecated will be replaced by getStreams() function |
||
| 428 | */ |
||
| 429 | public function streamsByGame($game, $limit = null, $offset = null, $embeddable = null, $hls = null) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Get video |
||
| 436 | * @param string $videoId |
||
| 437 | * @return \stdClass |
||
| 438 | * @throws TwitchException |
||
| 439 | */ |
||
| 440 | public function videoGet($videoId) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Returns top videos |
||
| 449 | * @param integer $limit |
||
| 450 | * @param integer $offset |
||
| 451 | * @param string $game |
||
| 452 | * @param string $period |
||
| 453 | * @return \stdClass |
||
| 454 | * @throws TwitchException |
||
| 455 | */ |
||
| 456 | View Code Duplication | public function videosTop($limit = null, $offset = null, $game = null, $period = null) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Get videos for a channel |
||
| 472 | * @param $channel |
||
| 473 | * @param null $limit |
||
| 474 | * @param null $offset |
||
| 475 | * @param bool $broadcasts |
||
| 476 | * @param bool $hls |
||
| 477 | * @return \stdClass |
||
| 478 | * @throws TwitchException |
||
| 479 | */ |
||
| 480 | View Code Duplication | public function videosByChannel($channel, $limit = null, $offset = null, $broadcasts = null, $hls = null) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Returns a links object to all other chat endpoints |
||
| 496 | * @param string $channelName |
||
| 497 | * @return \stdClass |
||
| 498 | * @throws TwitchException |
||
| 499 | */ |
||
| 500 | public function chatGet($channelName) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get a chat's emoticons |
||
| 509 | * @return \stdClass |
||
| 510 | * @throws TwitchException |
||
| 511 | */ |
||
| 512 | public function chatEmoticons() |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns a list of emoticons |
||
| 521 | * @param string $emoteset |
||
| 522 | * @return \stdClass |
||
| 523 | * @throws TwitchException |
||
| 524 | */ |
||
| 525 | View Code Duplication | public function chatEmoticonsImages($emoteset = null) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Returns a list of chat badges |
||
| 538 | * @param string $channelName |
||
| 539 | * @return \stdClass |
||
| 540 | * @throws TwitchException |
||
| 541 | */ |
||
| 542 | public function chatBadges($channelName) |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Get top games |
||
| 551 | * @param integer $limit |
||
| 552 | * @param integer $offset |
||
| 553 | * @return \stdClass |
||
| 554 | * @throws TwitchException |
||
| 555 | */ |
||
| 556 | View Code Duplication | public function gamesTop($limit = null, $offset = null) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Search games |
||
| 570 | * @param string $query |
||
| 571 | * @param null $type |
||
| 572 | * @param null $live |
||
| 573 | * @return \stdClass |
||
| 574 | * @throws TwitchException |
||
| 575 | */ |
||
| 576 | View Code Duplication | public function gameSearch($query, $type = null, $live = null) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Get HTML code for stream embedding |
||
| 591 | * @param $channel |
||
| 592 | * @param int $width |
||
| 593 | * @param int $height |
||
| 594 | * @param int $volume |
||
| 595 | * @return string |
||
| 596 | * @deprecated Will be replaced with Embed method |
||
| 597 | */ |
||
| 598 | View Code Duplication | public function embedStream($channel, $width = 620, $height = 378, $volume = 25) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Get HTML code for video embedding |
||
| 621 | * @param $channel |
||
| 622 | * @param $chapterid |
||
| 623 | * @param int $width |
||
| 624 | * @param int $height |
||
| 625 | * @param int $volume |
||
| 626 | * @return string |
||
| 627 | * @deprecated Will be replaced with Embed method |
||
| 628 | */ |
||
| 629 | View Code Duplication | public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25) |
|
| 649 | |||
| 650 | /** |
||
| 651 | * Get HTML code for chat embedding |
||
| 652 | * @param $channel |
||
| 653 | * @param int $width |
||
| 654 | * @param int $height |
||
| 655 | * @return string |
||
| 656 | * @deprecated Will be replaced with Embed method |
||
| 657 | */ |
||
| 658 | public function embedChat($channel, $width = 400, $height = 300) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Get login URL for authentication |
||
| 671 | * @param string $scope Specify which permissions your app requires (space separated list) |
||
| 672 | * @return \stdClass |
||
| 673 | * @throws TwitchException |
||
| 674 | */ |
||
| 675 | View Code Duplication | public function authLoginURL($scope) |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Get authentication access token |
||
| 693 | * @param string $code returned after app authorization by user |
||
| 694 | * @return \stdClass |
||
| 695 | * @throws TwitchException |
||
| 696 | */ |
||
| 697 | View Code Duplication | public function authAccessTokenGet($code) |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Get the authenticated user |
||
| 716 | * - requires scope 'user_read' |
||
| 717 | * @param string |
||
| 718 | * @return \stdClass |
||
| 719 | * @throws TwitchException |
||
| 720 | */ |
||
| 721 | public function authUserGet($token) |
||
| 729 | |||
| 730 | /** |
||
| 731 | * Returns a list of blocks |
||
| 732 | * - required scope 'user_blocks_read' |
||
| 733 | * @param string $token |
||
| 734 | * @param string $user |
||
| 735 | * @param integer $limit |
||
| 736 | * @param integer $offset |
||
| 737 | * @return \stdClass |
||
| 738 | * @throws TwitchException |
||
| 739 | */ |
||
| 740 | public function authUserBlocks($token, $user, $limit = null, $offset = null) |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Adds $target to $user block list |
||
| 758 | * - requires scope 'user_blocks_edit' |
||
| 759 | * @param string $token |
||
| 760 | * @param string $user |
||
| 761 | * @param string $target |
||
| 762 | * @return \stdClass |
||
| 763 | * @throws TwitchException |
||
| 764 | */ |
||
| 765 | View Code Duplication | public function authBlockTarget($token, $user, $target) |
|
| 766 | { |
||
| 767 | $this->checkConfig(); |
||
| 768 | $queryString = $this->getAuthString($token); |
||
| 769 | $block = new Methods\Block($this->request); |
||
| 770 | |||
| 771 | return $block->blockTarget($user, $target, $queryString); |
||
| 772 | } |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Removes $target from $user block list |
||
| 776 | * - requires scope 'user_blocks_edit' |
||
| 777 | * @param string $token |
||
| 778 | * @param string $user |
||
| 779 | * @param string $target |
||
| 780 | * @return \stdClass |
||
| 781 | * @throws TwitchException |
||
| 782 | */ |
||
| 783 | View Code Duplication | public function authRemoveTarget($token, $user, $target) |
|
| 784 | { |
||
| 785 | $this->checkConfig(); |
||
| 786 | $queryString = $this->getAuthString($token); |
||
| 787 | $block = new Methods\Block($this->request); |
||
| 788 | |||
| 789 | return $block->removeTarget($user, $target, $queryString); |
||
| 790 | } |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Get the authenticated channel |
||
| 794 | * - requires scope 'channel_read' |
||
| 795 | * @param string |
||
| 796 | * @return \stdClass |
||
| 797 | * @throws TwitchException |
||
| 798 | */ |
||
| 799 | View Code Duplication | public function authChannelGet($token) |
|
| 807 | |||
| 808 | /** |
||
| 809 | * Update channel's status or game |
||
| 810 | * - requires scope 'channel_editor' |
||
| 811 | * @param $token |
||
| 812 | * @param string $channelName |
||
| 813 | * @param string $status |
||
| 814 | * @param string $game |
||
| 815 | * @param integer $delay |
||
| 816 | * @return \stdClass |
||
| 817 | * @throws TwitchException |
||
| 818 | */ |
||
| 819 | public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null) |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Resets channel's stream key |
||
| 837 | * - requires scope 'channel_stream' |
||
| 838 | * @param string $token |
||
| 839 | * @param string $channelName |
||
| 840 | * @return \stdClass |
||
| 841 | * @throws TwitchException |
||
| 842 | */ |
||
| 843 | View Code Duplication | public function authChannelResetKey($token, $channelName) |
|
| 851 | |||
| 852 | /** |
||
| 853 | * Returns an array of users who are editors of specified channel |
||
| 854 | * - requires scope 'channel_read' |
||
| 855 | * @param string |
||
| 856 | * @param string |
||
| 857 | * @return \stdClass |
||
| 858 | * @throws TwitchException |
||
| 859 | */ |
||
| 860 | View Code Duplication | public function authChannelEditors($token, $channel) |
|
| 868 | |||
| 869 | /** |
||
| 870 | * Returns an array of subscriptions who are subscribed to specified channel |
||
| 871 | * - requires scope 'channel_subscriptions' |
||
| 872 | * @param string $token - user's access token |
||
| 873 | * @param string $channel |
||
| 874 | * @param integer $limit - can be up to 100 |
||
| 875 | * @param integer $offset |
||
| 876 | * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
||
| 877 | * @return \stdClass |
||
| 878 | * @throws TwitchException |
||
| 879 | */ |
||
| 880 | View Code Duplication | public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
|
| 896 | |||
| 897 | /** |
||
| 898 | * Returns user object if that user is subscribed |
||
| 899 | * - requires scope 'channel_check_subscription' for channel |
||
| 900 | * @param string $token |
||
| 901 | * @param string $channel |
||
| 902 | * @param string $user |
||
| 903 | * @return \stdClass |
||
| 904 | * @throws TwitchException |
||
| 905 | */ |
||
| 906 | View Code Duplication | public function authSubscribedUser($token, $channel, $user) |
|
| 914 | |||
| 915 | /** |
||
| 916 | * Returns a channel object that user subscribes to |
||
| 917 | * - requires scope 'user_subscriptions' for user |
||
| 918 | * @param string $token |
||
| 919 | * @param string $user |
||
| 920 | * @param string $channel |
||
| 921 | * @return \stdClass |
||
| 922 | * @throws TwitchException |
||
| 923 | */ |
||
| 924 | View Code Duplication | public function authSubscribedToChannel($token, $user, $channel) |
|
| 932 | |||
| 933 | /** |
||
| 934 | * List the live streams that the authenticated user is following |
||
| 935 | * - requires scope 'user_read' |
||
| 936 | * @param string |
||
| 937 | * @param integer $limit |
||
| 938 | * @param integer $offset |
||
| 939 | * @param bool $hls |
||
| 940 | * @return \stdClass |
||
| 941 | * @throws TwitchException |
||
| 942 | */ |
||
| 943 | View Code Duplication | public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
|
| 959 | |||
| 960 | /** |
||
| 961 | * Get streams helper |
||
| 962 | * @param null $game |
||
| 963 | * @param null $limit |
||
| 964 | * @param null $offset |
||
| 965 | * @param string|null $channels |
||
| 966 | * @param null $embeddable |
||
| 967 | * @param null $hls |
||
| 968 | * @return \stdClass |
||
| 969 | * @throws TwitchException |
||
| 970 | */ |
||
| 971 | public function getStreams($game = null, $limit = null, $offset = null, $channels = null, $embeddable = null, $hls = null) |
||
| 987 | |||
| 988 | /** |
||
| 989 | * Validate parameters for authentication |
||
| 990 | * @param array |
||
| 991 | * @return boolean |
||
| 992 | */ |
||
| 993 | private function configValidate($config) |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Returns string for auth |
||
| 1011 | * @param string $token |
||
| 1012 | * @return null|string |
||
| 1013 | * @throws TwitchException |
||
| 1014 | */ |
||
| 1015 | private function getAuthString($token) |
||
| 1022 | |||
| 1023 | /** |
||
| 1024 | * Configuration exception |
||
| 1025 | * @throws TwitchException |
||
| 1026 | */ |
||
| 1027 | private function configException() |
||
| 1031 | } |
||
| 1032 |