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 |
||
9 | class Yikes_Inc_Easy_MailChimp_API { |
||
|
|||
10 | |||
11 | /** |
||
12 | * The API key. |
||
13 | * |
||
14 | * @since %VERSION% |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $api_key = ''; |
||
18 | |||
19 | /** |
||
20 | * The URL for the API. |
||
21 | * |
||
22 | * @since %VERSION% |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $api_url = ''; |
||
26 | |||
27 | /** |
||
28 | * The API version. |
||
29 | * |
||
30 | * @since %VERSION% |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $api_version = ''; |
||
34 | |||
35 | /** |
||
36 | * The datacenter where the MailChimp account is located. |
||
37 | * |
||
38 | * This is typically part of the API Key. |
||
39 | * |
||
40 | * @since %VERSION% |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $datacenter = ''; |
||
44 | |||
45 | /** |
||
46 | * Yikes_Inc_Easy_MailChimp_API constructor. |
||
47 | * |
||
48 | * @since %VERSION% |
||
49 | * |
||
50 | * @param string $datacenter The datacenter string where the MailChimp account is located. |
||
51 | * @param string $api_key The base API key, without the datacenter appended. |
||
52 | * @param string $api_version The API version to use. |
||
53 | */ |
||
54 | public function __construct( $datacenter, $api_key, $api_version ) { |
||
60 | |||
61 | /** |
||
62 | * Send a DELETE request to the MailChimp API. |
||
63 | * |
||
64 | * @author Jeremy Pry |
||
65 | * @since %VERSION% |
||
66 | * |
||
67 | * @param string $path The relative path for the request. |
||
68 | * @param array $headers Array of headers to send with the request. |
||
69 | * @param array $params An array of additional parameters to pass to the request. See WP_Http::request(). |
||
70 | * |
||
71 | * @return array|WP_Error |
||
72 | */ |
||
73 | public function delete( $path = '', $headers = array(), $params = array() ) { |
||
76 | |||
77 | /** |
||
78 | * Send a GET request to the MailChimp API. |
||
79 | * |
||
80 | * @author Jeremy Pry |
||
81 | * @since %VERSION% |
||
82 | * |
||
83 | * @param string $path The relative path for the request. |
||
84 | * @param array $headers Array of headers to send with the request. |
||
85 | * @param array $params An array of additional parameters to pass to the request. See WP_Http::request(). |
||
86 | * |
||
87 | * @return array|WP_Error |
||
88 | */ |
||
89 | public function get( $path = '', $headers = array(), $params = array() ) { |
||
92 | |||
93 | /** |
||
94 | * Send a PATCH request to the MailChimp API. |
||
95 | * |
||
96 | * @author Jeremy Pry |
||
97 | * @since %VERSION% |
||
98 | * |
||
99 | * @param string $path The relative path for the request. |
||
100 | * @param array $headers Array of headers to send with the request. |
||
101 | * @param array $params An array of additional parameters to pass to the request. See WP_Http::request(). |
||
102 | * |
||
103 | * @return array|WP_Error |
||
104 | */ |
||
105 | View Code Duplication | public function patch( $path = '', $headers = array(), $params = array() ) { |
|
119 | |||
120 | /** |
||
121 | * Send a POST request to the MailChimp API. |
||
122 | * |
||
123 | * @author Jeremy Pry |
||
124 | * @since %VERSION% |
||
125 | * |
||
126 | * @param string $path The relative path for the request. |
||
127 | * @param array $headers Array of headers to send with the request. |
||
128 | * @param array $params An array of additional parameters to pass to the request. See WP_Http::request(). |
||
129 | * |
||
130 | * @return array|WP_Error |
||
131 | */ |
||
132 | View Code Duplication | public function post( $path = '', $headers = array(), $params = array() ) { |
|
146 | |||
147 | /** |
||
148 | * Send a PUT request to the MailChimp API. |
||
149 | * |
||
150 | * @author Jeremy Pry |
||
151 | * @since %VERSION% |
||
152 | * |
||
153 | * @param string $path The relative path for the request. |
||
154 | * @param array $headers Array of headers to send with the request. |
||
155 | * @param array $params An array of additional parameters to pass to the request. See WP_Http::request(). |
||
156 | * |
||
157 | * @return array|WP_Error |
||
158 | */ |
||
159 | View Code Duplication | public function put( $path = '', $headers = array(), $params = array() ) { |
|
173 | |||
174 | /** |
||
175 | * Send a request to the MailChimp API. |
||
176 | * |
||
177 | * @author Jeremy Pry |
||
178 | * @since %VERSION% |
||
179 | * |
||
180 | * @param string $path The relative path for the request. |
||
181 | * @param string $method The method to use for the request. |
||
182 | * @param array $headers Array of headers to send with the request. |
||
183 | * @param array $params An array of additional parameters to pass to the request. See WP_Http::request(). |
||
184 | * |
||
185 | * @return array|WP_Error |
||
186 | */ |
||
187 | protected function send_request( $path, $method, $headers = array(), $params = array() ) { |
||
206 | |||
207 | /** |
||
208 | * Get the user agent to use with a request to the API. |
||
209 | * |
||
210 | * @author Jeremy Pry |
||
211 | * @since %VERSION% |
||
212 | * @return string The user agent string. |
||
213 | */ |
||
214 | protected function get_user_agent() { |
||
229 | |||
230 | /** |
||
231 | * Get the Auth Headers for an API requests. |
||
232 | * |
||
233 | * @author Jeremy Pry |
||
234 | * @since %VERSION% |
||
235 | * @return array The array of auth headers for an API request. |
||
236 | */ |
||
237 | protected function get_auth_headers() { |
||
257 | |||
258 | /** |
||
259 | * Get a body array with authorization included. |
||
260 | * |
||
261 | * @author Jeremy Pry |
||
262 | * @return array |
||
263 | */ |
||
264 | protected function get_auth_body() { |
||
269 | |||
270 | /** |
||
271 | * Build the arguments for the request. |
||
272 | * |
||
273 | * @author Jeremy Pry |
||
274 | * @since %VERSION% |
||
275 | * |
||
276 | * @param string $path The relative path for the request. |
||
277 | * @param string $method The method to use for the request. |
||
278 | * @param array $headers Array of headers to send with the request. |
||
279 | * @param array $params An array of additional parameters to pass to the request. See WP_Http::request(). |
||
280 | * |
||
281 | * @return array |
||
282 | */ |
||
283 | protected function build_request_args( $path, $method, $headers = array(), $params = array() ) { |
||
345 | |||
346 | /** |
||
347 | * Get an authorized request based on the API version. |
||
348 | * |
||
349 | * @author Jeremy Pry |
||
350 | * @since %VERSION% |
||
351 | * @return array |
||
352 | */ |
||
353 | protected function get_authorized_args() { |
||
371 | |||
372 | /** |
||
373 | * Get the API version for this instance. |
||
374 | * |
||
375 | * @author Jeremy Pry |
||
376 | * @since %VERSION% |
||
377 | * @return string The API version. |
||
378 | */ |
||
379 | public function get_version() { |
||
382 | } |
||
383 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.