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 | 56 | */ |
|
| 45 | public function __construct($token = '') |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get url to service resource with parameters |
||
| 52 | * |
||
| 53 | * @param string $resource |
||
| 54 | * @param array $params |
||
| 55 | * @see http://api.yandex.ru/metrika/doc/ref/concepts/method-call.xml |
||
| 56 | * @return string |
||
| 57 | 28 | */ |
|
| 58 | public function getServiceUrl($resource = '', $params = []) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Sends a request |
||
| 73 | * |
||
| 74 | * @param string $method HTTP method |
||
| 75 | * @param string|UriInterface $uri URI object or string. |
||
| 76 | * @param array $options Request options to apply. |
||
| 77 | * |
||
| 78 | * @return Response |
||
| 79 | * |
||
| 80 | * @throws BadRequestException |
||
| 81 | * @throws ForbiddenException |
||
| 82 | * @throws MetricaException |
||
| 83 | 26 | * @throws TooManyRequestsException |
|
| 84 | * @throws UnauthorizedException |
||
| 85 | */ |
||
| 86 | 26 | protected function sendRequest($method, $uri, array $options = []) |
|
| 127 | 5 | ||
| 128 | 5 | /** |
|
| 129 | * Send GET request to API resource |
||
| 130 | 5 | * |
|
| 131 | 5 | * @param string $resource |
|
| 132 | * @param array $params |
||
| 133 | 1 | * @return array |
|
| 134 | */ |
||
| 135 | 1 | protected function sendGetRequest($resource, $params = []) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Send custom GET request to API resource |
||
| 161 | * |
||
| 162 | * @param string $url |
||
| 163 | * @param array $data |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | protected function getNextPartOfList($url, $data = []) |
||
| 192 | 7 | ||
| 193 | 7 | /** |
|
| 194 | 7 | * Send POST request to API resource |
|
| 195 | * |
||
| 196 | 7 | * @param string $resource |
|
| 197 | 7 | * @param array $params |
|
| 198 | * @return array |
||
| 199 | 7 | */ |
|
| 200 | View Code Duplication | protected function sendPostRequest($resource, $params) |
|
| 216 | 7 | ||
| 217 | 7 | /** |
|
| 218 | 7 | * Send PUT request to API resource |
|
| 219 | * |
||
| 220 | 7 | * @param string $resource |
|
| 221 | 7 | * @param array $params |
|
| 222 | * @return array |
||
| 223 | 7 | */ |
|
| 224 | View Code Duplication | protected function sendPutRequest($resource, $params) |
|
| 240 | 7 | ||
| 241 | /** |
||
| 242 | 7 | * Send DELETE request to API resource |
|
| 243 | 7 | * |
|
| 244 | * @param string $resource |
||
| 245 | 7 | * @return array |
|
| 246 | */ |
||
| 247 | View Code Duplication | protected function sendDeleteRequest($resource) |
|
| 262 | } |
||
| 263 |
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.