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 | class Client |
||
16 | { |
||
17 | /** |
||
18 | * Twitch alerts API base URI |
||
19 | */ |
||
20 | const BASE_URI = 'https://www.twitchalerts.com'; |
||
21 | |||
22 | /** |
||
23 | * Twitch alerts Authorize endpoint path |
||
24 | */ |
||
25 | const AUTHORIZE_ENDPOINT = '/api/v1.0/authorize'; |
||
26 | |||
27 | /** |
||
28 | * Twitch alerts Token endpoint path |
||
29 | */ |
||
30 | const TOKEN_ENDPOINT = '/api/v1.0/token'; |
||
31 | |||
32 | /** |
||
33 | * Twitch alerts Donations endpoint path |
||
34 | */ |
||
35 | const DONATION_ENDPOINT = '/api/v1.0/donations'; |
||
36 | |||
37 | /** |
||
38 | * Twitch alerts Alert endpoint path |
||
39 | */ |
||
40 | const ALERT_ENDPOINT = '/api/v1.0/alerts'; |
||
41 | |||
42 | /** |
||
43 | * Twitch alerts API scopes |
||
44 | */ |
||
45 | const SCOPES = [ |
||
46 | 'donations.create', |
||
47 | 'donations.read', |
||
48 | 'alerts.create', |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $clientId; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $clientSecret; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $redirectUrl; |
||
65 | |||
66 | /** |
||
67 | * @var HttpClient |
||
68 | */ |
||
69 | protected $httpClient; |
||
70 | |||
71 | /** |
||
72 | * Client constructor. |
||
73 | * |
||
74 | * @param string $clientId |
||
75 | * @param string $clientSecret |
||
76 | * @param string $redirectUrl |
||
77 | */ |
||
78 | public function __construct($clientId, $clientSecret, $redirectUrl) |
||
85 | |||
86 | /** |
||
87 | * @param array $scopes |
||
88 | * |
||
89 | * @return string |
||
90 | * @throws InvalidScopeException |
||
91 | */ |
||
92 | public function getAuthorizeUrl(array $scopes = self::SCOPES) |
||
109 | |||
110 | /** |
||
111 | * @param string $code |
||
112 | * |
||
113 | * @return array |
||
114 | * @throws ApiException |
||
115 | */ |
||
116 | View Code Duplication | public function getAccessToken($code) |
|
134 | |||
135 | /** |
||
136 | * @param string $refreshToken |
||
137 | * |
||
138 | * @return array |
||
139 | * @throws ApiException |
||
140 | */ |
||
141 | View Code Duplication | public function refreshAccessToken($refreshToken) |
|
159 | |||
160 | /** |
||
161 | * @param string $accessToken |
||
162 | * @param int $limit |
||
163 | * @param string $currency |
||
164 | * |
||
165 | * @return array |
||
166 | * @throws ApiException |
||
167 | */ |
||
168 | public function getDonations($accessToken, $limit, $currency) |
||
184 | |||
185 | /** |
||
186 | * @param string $accessToken |
||
187 | * @param string $name |
||
188 | * @param string $email |
||
189 | * @param int $amount |
||
190 | * @param string $currency |
||
191 | * @param string $message |
||
192 | * |
||
193 | * @return array |
||
194 | * @throws ApiException |
||
195 | */ |
||
196 | View Code Duplication | public function postDonation($accessToken, $name, $email, $amount, $currency, $message) |
|
215 | |||
216 | /** |
||
217 | * @param string $accessToken |
||
218 | * @param string $type |
||
219 | * @param string $message |
||
220 | * @param string $imageUrl |
||
221 | * @param string $soundUrl |
||
222 | * @param string $textColor |
||
223 | * @param int $duration |
||
224 | * |
||
225 | * @return array |
||
226 | * @throws ApiException |
||
227 | */ |
||
228 | View Code Duplication | public function postAlert($accessToken, $type, $message, $imageUrl, $soundUrl, $textColor, $duration) |
|
248 | } |
||
249 |
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.