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 |
||
| 34 | class MetricaClient extends AbstractServiceClient |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * API domain |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $serviceDomain = 'api-metrika.yandex.ru/management/v1'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param string $token access token |
||
| 45 | * @param ClientInterface $client |
||
| 46 | */ |
||
| 47 | 58 | public function __construct($token = '', ClientInterface $client = null) |
|
| 48 | { |
||
| 49 | 58 | $this->setAccessToken($token); |
|
| 50 | 58 | if (!is_null($client)) { |
|
| 51 | 1 | $this->setClient($client); |
|
| 52 | 1 | } |
|
| 53 | 58 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * Get url to service resource with parameters |
||
| 57 | * |
||
| 58 | * @param string $resource |
||
| 59 | * @param array $params |
||
| 60 | * @see http://api.yandex.ru/metrika/doc/ref/concepts/method-call.xml |
||
| 61 | * @return string |
||
| 62 | */ |
||
| 63 | 29 | public function getServiceUrl($resource = '', $params = []) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Sends a request |
||
| 78 | * |
||
| 79 | * @param string $method HTTP method |
||
| 80 | * @param string|UriInterface $uri URI object or string. |
||
| 81 | * @param array $options Request options to apply. |
||
| 82 | * |
||
| 83 | * @return Response |
||
| 84 | * |
||
| 85 | * @throws BadRequestException |
||
| 86 | * @throws ForbiddenException |
||
| 87 | * @throws MetricaException |
||
| 88 | * @throws TooManyRequestsException |
||
| 89 | * @throws UnauthorizedException |
||
| 90 | */ |
||
| 91 | 27 | protected function sendRequest($method, $uri, array $options = []) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Send GET request to API resource |
||
| 135 | * |
||
| 136 | * @param string $resource |
||
| 137 | * @param array $params |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | 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 = []) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Send POST request to API resource |
||
| 200 | * |
||
| 201 | * @param string $resource |
||
| 202 | * @param array $params |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | 7 | View Code Duplication | protected function sendPostRequest($resource, $params) |
| 221 | |||
| 222 | /** |
||
| 223 | * Send PUT request to API resource |
||
| 224 | * |
||
| 225 | * @param string $resource |
||
| 226 | * @param array $params |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | 7 | View Code Duplication | protected function sendPutRequest($resource, $params) |
| 245 | |||
| 246 | /** |
||
| 247 | * Send DELETE request to API resource |
||
| 248 | * |
||
| 249 | * @param string $resource |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | 7 | View Code Duplication | protected function sendDeleteRequest($resource) |
| 267 | } |
||
| 268 |
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.