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 = []) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Send custom GET request to API resource |
||
| 165 | * |
||
| 166 | * @param string $url |
||
| 167 | * @param array $data |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function getNextPartOfList($url, $data = []) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Send POST request to API resource |
||
| 199 | * |
||
| 200 | * @param string $resource |
||
| 201 | * @param array $params |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 7 | View Code Duplication | protected function sendPostRequest($resource, $params) |
| 220 | |||
| 221 | /** |
||
| 222 | * Send PUT request to API resource |
||
| 223 | * |
||
| 224 | * @param string $resource |
||
| 225 | * @param array $params |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | 7 | View Code Duplication | protected function sendPutRequest($resource, $params) |
| 244 | |||
| 245 | /** |
||
| 246 | * Send DELETE request to API resource |
||
| 247 | * |
||
| 248 | * @param string $resource |
||
| 249 | * @return array |
||
| 250 | */ |
||
| 251 | 7 | View Code Duplication | protected function sendDeleteRequest($resource) |
| 266 | } |
||
| 267 |
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.