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 |
||
33 | class MetricaClient extends AbstractServiceClient |
||
34 | { |
||
35 | /** |
||
36 | * API domain |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $serviceDomain = 'api-metrika.yandex.ru/management/v1'; |
||
41 | |||
42 | /** |
||
43 | * @param string $token access token |
||
44 | * @param ClientInterface $client |
||
45 | */ |
||
46 | 58 | public function __construct($token = '', ClientInterface $client = null) |
|
53 | |||
54 | /** |
||
55 | * Get url to service resource with parameters |
||
56 | * |
||
57 | * @param string $resource |
||
58 | * @param array $params |
||
59 | * @see http://api.yandex.ru/metrika/doc/ref/concepts/method-call.xml |
||
60 | * @return string |
||
61 | */ |
||
62 | 29 | public function getServiceUrl($resource = '', $params = []) |
|
74 | |||
75 | /** |
||
76 | * Sends a request |
||
77 | * |
||
78 | * @param string $method HTTP method |
||
79 | * @param string $uri URI object or string. |
||
80 | * @param array $options Request options to apply. |
||
81 | * |
||
82 | * @return Response |
||
83 | * |
||
84 | * @throws BadRequestException |
||
85 | * @throws ForbiddenException |
||
86 | * @throws MetricaException |
||
87 | * @throws TooManyRequestsException |
||
88 | * @throws UnauthorizedException |
||
89 | */ |
||
90 | 27 | protected function sendRequest($method, $uri, array $options = []) |
|
131 | |||
132 | /** |
||
133 | * Send GET request to API resource |
||
134 | * |
||
135 | * @param string $resource |
||
136 | * @param array $params |
||
137 | * @return array |
||
138 | */ |
||
139 | 6 | protected function sendGetRequest($resource, $params = []) |
|
163 | |||
164 | /** |
||
165 | * Send custom GET request to API resource |
||
166 | * |
||
167 | * @param string $url |
||
168 | * @param array $data |
||
169 | * @return array |
||
170 | */ |
||
171 | protected function getNextPartOfList($url, $data = []) |
||
198 | |||
199 | /** |
||
200 | * Send POST request to API resource |
||
201 | * |
||
202 | * @param string $resource |
||
203 | * @param array $params |
||
204 | * @return array |
||
205 | */ |
||
206 | 7 | View Code Duplication | protected function sendPostRequest($resource, $params) |
223 | |||
224 | /** |
||
225 | * Send PUT request to API resource |
||
226 | * |
||
227 | * @param string $resource |
||
228 | * @param array $params |
||
229 | * @return array |
||
230 | */ |
||
231 | 7 | View Code Duplication | protected function sendPutRequest($resource, $params) |
248 | |||
249 | /** |
||
250 | * Send DELETE request to API resource |
||
251 | * |
||
252 | * @param string $resource |
||
253 | * @return array |
||
254 | */ |
||
255 | 7 | protected function sendDeleteRequest($resource) |
|
271 | } |
||
272 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.