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 |
||
32 | class ContentClient extends AbstractServiceClient |
||
33 | { |
||
34 | /** |
||
35 | * Requested version of API |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $version = 'v1'; |
||
40 | |||
41 | /** |
||
42 | * API domain |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $serviceDomain = 'api.content.market.yandex.ru'; |
||
47 | |||
48 | /** |
||
49 | * Store limits information during each API request |
||
50 | * |
||
51 | * @var array |
||
52 | */ |
||
53 | private $limits = array(); |
||
54 | |||
55 | /** |
||
56 | * Get url to service resource with parameters |
||
57 | * |
||
58 | * @param string $resource |
||
59 | * @see http://api.yandex.ru/market/partner/doc/dg/concepts/method-call.xml |
||
60 | * @return string |
||
61 | */ |
||
62 | 29 | public function getServiceUrl($resource = '') |
|
67 | |||
68 | /** |
||
69 | * @param string $token access token |
||
70 | */ |
||
71 | 29 | public function __construct($token = '') |
|
75 | |||
76 | /** |
||
77 | * @return ClientInterface |
||
78 | */ |
||
79 | View Code Duplication | protected function getClient() |
|
101 | |||
102 | /** |
||
103 | * Sends a request |
||
104 | * |
||
105 | * @param string $method HTTP method |
||
106 | * @param string $uri URI object or string. |
||
107 | * @param array $options Request options to apply. |
||
108 | * |
||
109 | * @return Response |
||
110 | * |
||
111 | * @throws ForbiddenException |
||
112 | * @throws UnauthorizedException |
||
113 | * @throws ContentRequestException |
||
114 | */ |
||
115 | View Code Duplication | protected function sendRequest($method, $uri, array $options = []) |
|
150 | |||
151 | private function setLimits($headers) |
||
172 | |||
173 | /** |
||
174 | * Get information about API limits |
||
175 | * |
||
176 | * @return array|false |
||
177 | */ |
||
178 | public function getLimits() |
||
182 | |||
183 | /** |
||
184 | * Get information about specified API limit |
||
185 | * |
||
186 | * @return array|false |
||
187 | */ |
||
188 | public function getLimit($name) |
||
196 | |||
197 | /** |
||
198 | * Returns API service response. |
||
199 | * |
||
200 | * @param string $resource |
||
201 | * @return array |
||
202 | * @throws ContentRequestException |
||
203 | * @throws ForbiddenException |
||
204 | * @throws UnauthorizedException |
||
205 | */ |
||
206 | 29 | protected function getServiceResponse($resource) |
|
213 | |||
214 | /** |
||
215 | * Returns URL-encoded query string |
||
216 | * |
||
217 | * @note: similar to http_build_query(), |
||
218 | * but transform key=>value where key == value to "?key" param. |
||
219 | * |
||
220 | * @param array|object $queryData |
||
221 | * @param string $prefix |
||
222 | * @param string $argSeparator |
||
223 | * @param int $encType |
||
224 | * |
||
225 | * @return string $queryString |
||
226 | */ |
||
227 | 25 | protected function buildQueryString($queryData, $prefix = '', $argSeparator = '&', $encType = PHP_QUERY_RFC3986) |
|
245 | } |
||
246 |
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.