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 | */ |
||
| 45 | 57 | 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 | */ |
||
| 58 | 29 | 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 | * @throws TooManyRequestsException |
||
| 84 | * @throws UnauthorizedException |
||
| 85 | */ |
||
| 86 | 27 | protected function sendRequest($method, $uri, array $options = []) |
|
| 87 | { |
||
| 88 | try { |
||
| 89 | 27 | $response = $this->getClient()->request($method, $uri, $options); |
|
| 90 | 27 | } catch (ClientException $ex) { |
|
| 91 | 5 | $result = $ex->getResponse(); |
|
| 92 | 5 | $code = $result->getStatusCode(); |
|
| 93 | 5 | $message = $result->getReasonPhrase(); |
|
| 94 | |||
| 95 | 5 | $body = $result->getBody(); |
|
| 96 | 5 | if ($body) { |
|
| 97 | 5 | $jsonBody = json_decode($body); |
|
| 98 | 5 | if ($jsonBody && isset($jsonBody->message)) { |
|
| 99 | 1 | $message = $jsonBody->message; |
|
| 100 | 1 | } |
|
| 101 | 5 | } |
|
| 102 | |||
| 103 | 5 | if ($code === 400) { |
|
| 104 | 1 | throw new BadRequestException($message); |
|
| 105 | } |
||
| 106 | |||
| 107 | 4 | if ($code === 403) { |
|
| 108 | 1 | throw new ForbiddenException($message); |
|
| 109 | } |
||
| 110 | |||
| 111 | 3 | if ($code === 401) { |
|
| 112 | 1 | throw new UnauthorizedException($message); |
|
| 113 | } |
||
| 114 | |||
| 115 | 2 | if ($code === 429) { |
|
| 116 | 1 | throw new TooManyRequestsException($message); |
|
| 117 | } |
||
| 118 | |||
| 119 | 1 | throw new MetricaException( |
|
| 120 | 1 | 'Service responded with error code: "' . $code . '" and message: "' . $message . '"', |
|
| 121 | $code |
||
| 122 | 1 | ); |
|
| 123 | } |
||
| 124 | |||
| 125 | 22 | return $response; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Send GET request to API resource |
||
| 130 | * |
||
| 131 | * @param string $resource |
||
| 132 | * @param array $params |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 6 | 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 | |||
| 193 | /** |
||
| 194 | * Send POST request to API resource |
||
| 195 | * |
||
| 196 | * @param string $resource |
||
| 197 | * @param array $params |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | 7 | View Code Duplication | protected function sendPostRequest($resource, $params) |
| 216 | |||
| 217 | /** |
||
| 218 | * Send PUT request to API resource |
||
| 219 | * |
||
| 220 | * @param string $resource |
||
| 221 | * @param array $params |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | 7 | View Code Duplication | protected function sendPutRequest($resource, $params) |
| 240 | |||
| 241 | /** |
||
| 242 | * Send DELETE request to API resource |
||
| 243 | * |
||
| 244 | * @param string $resource |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | 7 | 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.