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 | * @throws \InvalidArgumentException |
||
| 32 | */ |
||
| 33 | public function __construct(array $config = array()) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * config setter |
||
| 55 | * @param array $config |
||
| 56 | * @return TwitchSDK |
||
| 57 | * @throws TwitchException |
||
| 58 | */ |
||
| 59 | public function setConfig(array $config) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Check if config is set |
||
| 72 | * @throws TwitchException |
||
| 73 | */ |
||
| 74 | private function checkConfig() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get value from config |
||
| 83 | * @param string $key |
||
| 84 | * @return mixed |
||
| 85 | * @throws TwitchException |
||
| 86 | */ |
||
| 87 | private function getConfigParam($key) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Basic information about the API and authentication status |
||
| 98 | * @param null $token |
||
| 99 | * @return \stdClass |
||
| 100 | * @throws TwitchException |
||
| 101 | */ |
||
| 102 | public function status($token = null) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get the specified user |
||
| 119 | * @param $username |
||
| 120 | * @return \stdClass |
||
| 121 | * @throws TwitchException |
||
| 122 | */ |
||
| 123 | public function userGet($username) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Get a user's list of followed channels |
||
| 132 | * @param string $user |
||
| 133 | * @param integer $limit |
||
| 134 | * @param integer $offset |
||
| 135 | * @param string $direction |
||
| 136 | * @param string $sortby |
||
| 137 | * @return \stdClass |
||
| 138 | * @throws TwitchException |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function userFollowChannels($user, $limit = null, $offset = null, $direction = null, $sortby = null) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Get the status of a follow relationship |
||
| 156 | * @param string $user |
||
| 157 | * @param string $channel |
||
| 158 | * @return \stdClass |
||
| 159 | * @throws TwitchException |
||
| 160 | */ |
||
| 161 | public function userFollowRelationship($user, $channel) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set user to follow given channel |
||
| 170 | * - requires scope 'user_follows_edit' |
||
| 171 | * @param string $user |
||
| 172 | * @param string $channel |
||
| 173 | * @param string $userToken |
||
| 174 | * @param bool $notifications |
||
| 175 | * @return \stdClass |
||
| 176 | * @throws TwitchException |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function authUserFollowChannel($user, $channel, $userToken, $notifications = false) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Set user to unfollow given channel |
||
| 194 | * - requires scope 'user_follows_edit' |
||
| 195 | * @param string $user |
||
| 196 | * @param string $channel |
||
| 197 | * @param string $userToken |
||
| 198 | * @return \stdClass |
||
| 199 | * @throws TwitchException |
||
| 200 | */ |
||
| 201 | View Code Duplication | public function authUserUnfollowChannel($user, $channel, $userToken) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Get the specified channel |
||
| 216 | * @param string $channelName |
||
| 217 | * @return \stdClass |
||
| 218 | * @throws TwitchException |
||
| 219 | */ |
||
| 220 | public function channelGet($channelName) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Return team list for specified channel |
||
| 229 | * @param string $channelName |
||
| 230 | * @return \stdClass |
||
| 231 | * @throws TwitchException |
||
| 232 | */ |
||
| 233 | public function channelTeamsGet($channelName) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Search channels |
||
| 242 | * @param string $query |
||
| 243 | * @param integer $limit |
||
| 244 | * @param integer $offset |
||
| 245 | * @return \stdClass |
||
| 246 | * @throws TwitchException |
||
| 247 | */ |
||
| 248 | View Code Duplication | public function channelSearch($query, $limit = null, $offset = null) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Get the specified team |
||
| 263 | * @param $teamName |
||
| 264 | * @return \stdClass |
||
| 265 | * @throws TwitchException |
||
| 266 | */ |
||
| 267 | public function teamGet($teamName) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Returns a list of active teams |
||
| 276 | * @param integer $limit |
||
| 277 | * @param integer $offset |
||
| 278 | * @return \stdClass |
||
| 279 | * @throws TwitchException |
||
| 280 | */ |
||
| 281 | View Code Duplication | public function teamList($limit = null, $offset = null) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Get all team members |
||
| 295 | * @param $teamName |
||
| 296 | * @return mixed |
||
| 297 | * @throws TwitchException |
||
| 298 | */ |
||
| 299 | public function teamMembersAll($teamName) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Returns an array of users who follow the specified channel |
||
| 306 | * @param string $channelName |
||
| 307 | * @param integer $limit |
||
| 308 | * @param integer $offset |
||
| 309 | * @param string $cursor |
||
| 310 | * @param string $direction |
||
| 311 | * @return \stdClass |
||
| 312 | * @throws TwitchException |
||
| 313 | */ |
||
| 314 | View Code Duplication | public function channelFollows($channelName, $limit = null, $offset = null, $cursor = null, $direction = null) |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Get the specified channel's stream |
||
| 330 | * @param $channel |
||
| 331 | * @return \stdClass |
||
| 332 | * @throws TwitchException |
||
| 333 | */ |
||
| 334 | public function streamGet($channel) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Search live streams |
||
| 343 | * @param string $query |
||
| 344 | * @param null $limit |
||
| 345 | * @param null $offset |
||
| 346 | * @param null $hls |
||
| 347 | * @return \stdClass |
||
| 348 | * @throws TwitchException |
||
| 349 | */ |
||
| 350 | public function streamSearch($query, $limit = null, $offset = null, $hls = null) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Summarize streams |
||
| 366 | * @param null $game |
||
| 367 | * @param array|null $channels |
||
| 368 | * @param null $hls |
||
| 369 | * @return \stdClass |
||
| 370 | * @throws TwitchException |
||
| 371 | */ |
||
| 372 | View Code Duplication | public function streamsSummarize($game = null, array $channels = null, $hls = null) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Get featured streams |
||
| 391 | * @param null $limit |
||
| 392 | * @param null $offset |
||
| 393 | * @param null $hls |
||
| 394 | * @return \stdClass |
||
| 395 | * @throws TwitchException |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function streamsFeatured($limit = null, $offset = null, $hls = null) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Get video |
||
| 412 | * @param string $videoId |
||
| 413 | * @return \stdClass |
||
| 414 | * @throws TwitchException |
||
| 415 | */ |
||
| 416 | public function videoGet($videoId) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Returns top videos |
||
| 425 | * @param integer $limit |
||
| 426 | * @param integer $offset |
||
| 427 | * @param string $game |
||
| 428 | * @param string $period |
||
| 429 | * @return \stdClass |
||
| 430 | * @throws TwitchException |
||
| 431 | */ |
||
| 432 | View Code Duplication | public function videosTop($limit = null, $offset = null, $game = null, $period = null) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Get videos for a channel |
||
| 448 | * @param $channel |
||
| 449 | * @param null $limit |
||
| 450 | * @param null $offset |
||
| 451 | * @param bool $broadcasts |
||
| 452 | * @param bool $hls |
||
| 453 | * @return \stdClass |
||
| 454 | * @throws TwitchException |
||
| 455 | */ |
||
| 456 | View Code Duplication | public function videosByChannel($channel, $limit = null, $offset = null, $broadcasts = null, $hls = null) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Returns a links object to all other chat endpoints |
||
| 472 | * @param string $channelName |
||
| 473 | * @return \stdClass |
||
| 474 | * @throws TwitchException |
||
| 475 | */ |
||
| 476 | public function chatGet($channelName) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Get a chat's emoticons |
||
| 485 | * @return \stdClass |
||
| 486 | * @throws TwitchException |
||
| 487 | */ |
||
| 488 | public function chatEmoticons() |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Returns a list of emoticons |
||
| 497 | * @param string $emoteset |
||
| 498 | * @return \stdClass |
||
| 499 | * @throws TwitchException |
||
| 500 | */ |
||
| 501 | View Code Duplication | public function chatEmoticonsImages($emoteset = null) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Returns a list of chat badges |
||
| 514 | * @param string $channelName |
||
| 515 | * @return \stdClass |
||
| 516 | * @throws TwitchException |
||
| 517 | */ |
||
| 518 | public function chatBadges($channelName) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Get top games |
||
| 527 | * @param integer $limit |
||
| 528 | * @param integer $offset |
||
| 529 | * @return \stdClass |
||
| 530 | * @throws TwitchException |
||
| 531 | */ |
||
| 532 | View Code Duplication | public function gamesTop($limit = null, $offset = null) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Search games |
||
| 546 | * @param string $query |
||
| 547 | * @param null $type |
||
| 548 | * @param null $live |
||
| 549 | * @return \stdClass |
||
| 550 | * @throws TwitchException |
||
| 551 | */ |
||
| 552 | View Code Duplication | public function gameSearch($query, $type = null, $live = null) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Get HTML code for stream embedding |
||
| 567 | * @param $channel |
||
| 568 | * @param int $width |
||
| 569 | * @param int $height |
||
| 570 | * @param int $volume |
||
| 571 | * @return string |
||
| 572 | * @deprecated Will be replaced with Embed method |
||
| 573 | */ |
||
| 574 | View Code Duplication | public function embedStream($channel, $width = 620, $height = 378, $volume = 25) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Get HTML code for video embedding |
||
| 597 | * @param $channel |
||
| 598 | * @param $chapterid |
||
| 599 | * @param int $width |
||
| 600 | * @param int $height |
||
| 601 | * @param int $volume |
||
| 602 | * @return string |
||
| 603 | * @deprecated Will be replaced with Embed method |
||
| 604 | */ |
||
| 605 | View Code Duplication | public function embedVideo($channel, $chapterid, $width = 400, $height = 300, $volume = 25) |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Get HTML code for chat embedding |
||
| 628 | * @param $channel |
||
| 629 | * @param int $width |
||
| 630 | * @param int $height |
||
| 631 | * @return string |
||
| 632 | * @deprecated Will be replaced with Embed method |
||
| 633 | */ |
||
| 634 | public function embedChat($channel, $width = 400, $height = 300) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Get login URL for authentication |
||
| 647 | * @param string $scope Specify which permissions your app requires (space separated list) |
||
| 648 | * @return \stdClass |
||
| 649 | * @throws TwitchException |
||
| 650 | */ |
||
| 651 | View Code Duplication | public function authLoginURL($scope) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Get authentication access token |
||
| 669 | * @param string $code returned after app authorization by user |
||
| 670 | * @return \stdClass |
||
| 671 | * @throws TwitchException |
||
| 672 | */ |
||
| 673 | View Code Duplication | public function authAccessTokenGet($code) |
|
| 689 | |||
| 690 | /** |
||
| 691 | * Get the authenticated user |
||
| 692 | * - requires scope 'user_read' |
||
| 693 | * @param string |
||
| 694 | * @return \stdClass |
||
| 695 | * @throws TwitchException |
||
| 696 | */ |
||
| 697 | public function authUserGet($token) |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Returns a list of blocks |
||
| 708 | * - required scope 'user_blocks_read' |
||
| 709 | * @param string $token |
||
| 710 | * @param string $user |
||
| 711 | * @param integer $limit |
||
| 712 | * @param integer $offset |
||
| 713 | * @return \stdClass |
||
| 714 | * @throws TwitchException |
||
| 715 | */ |
||
| 716 | public function authUserBlocks($token, $user, $limit = null, $offset = null) |
||
| 731 | |||
| 732 | /** |
||
| 733 | * Adds $target to $user block list |
||
| 734 | * - requires scope 'user_blocks_edit' |
||
| 735 | * @param string $token |
||
| 736 | * @param string $user |
||
| 737 | * @param string $target |
||
| 738 | * @return \stdClass |
||
| 739 | * @throws TwitchException |
||
| 740 | */ |
||
| 741 | View Code Duplication | public function authBlockTarget($token, $user, $target) |
|
| 749 | |||
| 750 | /** |
||
| 751 | * Removes $target from $user block list |
||
| 752 | * - requires scope 'user_blocks_edit' |
||
| 753 | * @param string $token |
||
| 754 | * @param string $user |
||
| 755 | * @param string $target |
||
| 756 | * @return \stdClass |
||
| 757 | * @throws TwitchException |
||
| 758 | */ |
||
| 759 | View Code Duplication | public function authRemoveTarget($token, $user, $target) |
|
| 767 | |||
| 768 | /** |
||
| 769 | * Get the authenticated channel |
||
| 770 | * - requires scope 'channel_read' |
||
| 771 | * @param string |
||
| 772 | * @return \stdClass |
||
| 773 | * @throws TwitchException |
||
| 774 | */ |
||
| 775 | View Code Duplication | public function authChannelGet($token) |
|
| 783 | |||
| 784 | /** |
||
| 785 | * Update channel's status or game |
||
| 786 | * - requires scope 'channel_editor' |
||
| 787 | * @param $token |
||
| 788 | * @param string $channelName |
||
| 789 | * @param string $status |
||
| 790 | * @param string $game |
||
| 791 | * @param integer $delay |
||
| 792 | * @return \stdClass |
||
| 793 | * @throws TwitchException |
||
| 794 | */ |
||
| 795 | public function authChannelSet($token, $channelName, $status = null, $game = null, $delay = null) |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Resets channel's stream key |
||
| 813 | * - requires scope 'channel_stream' |
||
| 814 | * @param string $token |
||
| 815 | * @param string $channelName |
||
| 816 | * @return \stdClass |
||
| 817 | * @throws TwitchException |
||
| 818 | */ |
||
| 819 | View Code Duplication | public function authChannelResetKey($token, $channelName) |
|
| 827 | |||
| 828 | /** |
||
| 829 | * Returns an array of users who are editors of specified channel |
||
| 830 | * - requires scope 'channel_read' |
||
| 831 | * @param string |
||
| 832 | * @param string |
||
| 833 | * @return \stdClass |
||
| 834 | * @throws TwitchException |
||
| 835 | */ |
||
| 836 | View Code Duplication | public function authChannelEditors($token, $channel) |
|
| 844 | |||
| 845 | /** |
||
| 846 | * Returns an array of subscriptions who are subscribed to specified channel |
||
| 847 | * - requires scope 'channel_subscriptions' |
||
| 848 | * @param string $token - user's access token |
||
| 849 | * @param string $channel |
||
| 850 | * @param integer $limit - can be up to 100 |
||
| 851 | * @param integer $offset |
||
| 852 | * @param string $direction can be DESC|ASC, if DESC - lasts will be showed first |
||
| 853 | * @return \stdClass |
||
| 854 | * @throws TwitchException |
||
| 855 | */ |
||
| 856 | View Code Duplication | public function authChannelSubscriptions($token, $channel, $limit = 25, $offset = 0, $direction = 'DESC') |
|
| 872 | |||
| 873 | /** |
||
| 874 | * Returns user object if that user is subscribed |
||
| 875 | * - requires scope 'channel_check_subscription' for channel |
||
| 876 | * @param string $token |
||
| 877 | * @param string $channel |
||
| 878 | * @param string $user |
||
| 879 | * @return \stdClass |
||
| 880 | * @throws TwitchException |
||
| 881 | */ |
||
| 882 | View Code Duplication | public function authSubscribedUser($token, $channel, $user) |
|
| 890 | |||
| 891 | /** |
||
| 892 | * Returns a channel object that user subscribes to |
||
| 893 | * - requires scope 'user_subscriptions' for user |
||
| 894 | * @param string $token |
||
| 895 | * @param string $user |
||
| 896 | * @param string $channel |
||
| 897 | * @return \stdClass |
||
| 898 | * @throws TwitchException |
||
| 899 | */ |
||
| 900 | View Code Duplication | public function authSubscribedToChannel($token, $user, $channel) |
|
| 908 | |||
| 909 | /** |
||
| 910 | * List the live streams that the authenticated user is following |
||
| 911 | * - requires scope 'user_read' |
||
| 912 | * @param string |
||
| 913 | * @param integer $limit |
||
| 914 | * @param integer $offset |
||
| 915 | * @param bool $hls |
||
| 916 | * @return \stdClass |
||
| 917 | * @throws TwitchException |
||
| 918 | */ |
||
| 919 | View Code Duplication | public function authStreamsFollowed($token, $limit = 25, $offset = 0, $hls = null) |
|
| 935 | |||
| 936 | /** |
||
| 937 | * Get streams helper |
||
| 938 | * @param null $game |
||
| 939 | * @param string|null $channels |
||
| 940 | * @param null $limit |
||
| 941 | * @param null $offset |
||
| 942 | * @param null $embeddable |
||
| 943 | * @param null $hls |
||
| 944 | * @return \stdClass |
||
| 945 | * @throws TwitchException |
||
| 946 | */ |
||
| 947 | public function getStreams($game = null, $channels = null, $limit = null, $offset = null, $embeddable = null, $hls = null) |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Validate parameters for authentication |
||
| 966 | * @param array |
||
| 967 | * @return boolean |
||
| 968 | */ |
||
| 969 | private function configValidate($config) |
||
| 984 | |||
| 985 | /** |
||
| 986 | * Returns string for auth |
||
| 987 | * @param string $token |
||
| 988 | * @return null|string |
||
| 989 | * @throws TwitchException |
||
| 990 | */ |
||
| 991 | private function getAuthString($token) |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * Configuration exception |
||
| 1001 | * @throws TwitchException |
||
| 1002 | */ |
||
| 1003 | private function configException() |
||
| 1007 | } |
||
| 1008 |