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:
1 | <?php |
||
15 | View Code Duplication | class Subscription |
|
1 ignored issue
–
show
|
|||
16 | { |
||
17 | /** @var TwitchRequest */ |
||
18 | protected $request; |
||
19 | |||
20 | const URI_CHANNEL_SUBSCRIPTIONS = 'channels/%s/subscriptions'; |
||
21 | const URI_CHANNEL_SUBSCRIPTIONS_USER = 'channels/%s/subscriptions/%s'; |
||
22 | const URI_USER_SUBSCRIPTIONS_CHANNEL = 'users/%s/subscriptions/%s'; |
||
23 | |||
24 | public function __construct(TwitchRequest $request) |
||
28 | |||
29 | /** |
||
30 | * @description Returns an array of subscriptions who are subscribed to specified channel |
||
31 | * - requires scope 'channel_subscriptions' |
||
32 | * @see https://github.com/justintv/Twitch-API/blob/master/v3_resources/subscriptions.md#get-channelschannelsubscriptions |
||
33 | * @param string $channel |
||
34 | * @param string $queryString |
||
35 | * @return \stdClass |
||
36 | * @throws TwitchException |
||
37 | */ |
||
38 | public function getSubscriptions($channel, $queryString) |
||
42 | |||
43 | /** |
||
44 | * Returns user object if that user is subscribed |
||
45 | * - requires scope 'channel_check_subscription' for channel |
||
46 | * @see https://github.com/justintv/Twitch-API/blob/master/v3_resources/subscriptions.md#get-channelschannelsubscriptionsuser |
||
47 | * @param string $channel |
||
48 | * @param string $user |
||
49 | * @param string $queryString |
||
50 | * @return \stdClass |
||
51 | * @throws TwitchException |
||
52 | */ |
||
53 | public function getSubscribedUser($channel, $user, $queryString) |
||
57 | |||
58 | /** |
||
59 | * Returns a channel object that user subscribes to |
||
60 | * - requires scope 'user_subscriptions' for user |
||
61 | * @see https://github.com/justintv/Twitch-API/blob/master/v3_resources/subscriptions.md#get-usersusersubscriptionschannel |
||
62 | * @param string $user |
||
63 | * @param string $channel |
||
64 | * @param string $queryString |
||
65 | * @return \stdClass |
||
66 | * @throws TwitchException |
||
67 | */ |
||
68 | public function getSubscribedToChannel($user, $channel, $queryString) |
||
72 | } |
||
73 |
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.