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 |
||
| 32 | class MetricaClient extends AbstractServiceClient |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * API domain |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $serviceDomain = 'api-metrika.yandex.ru/management/v1'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string $token access token |
||
| 43 | */ |
||
| 44 | 55 | public function __construct($token = '') |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Get url to service resource with parameters |
||
| 51 | * |
||
| 52 | * @param string $resource |
||
| 53 | * @param array $params |
||
| 54 | * @see http://api.yandex.ru/metrika/doc/ref/concepts/method-call.xml |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | 27 | public function getServiceUrl($resource = '', $params = []) |
|
| 58 | { |
||
| 59 | 27 | $format = $resource === '' ? '' : '.json'; |
|
| 60 | 27 | $url = $this->serviceScheme . '://' . $this->serviceDomain . '/' |
|
| 61 | 27 | . $resource . $format . '?oauth_token=' . $this->getAccessToken(); |
|
| 62 | |||
| 63 | 27 | if ($params) { |
|
|
|
|||
| 64 | 1 | $url .= '&' . http_build_query($params); |
|
| 65 | 1 | } |
|
| 66 | |||
| 67 | 27 | return $url; |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Sends a request |
||
| 72 | * |
||
| 73 | * @param string $method HTTP method |
||
| 74 | * @param string|UriInterface $uri URI object or string. |
||
| 75 | * @param array $options Request options to apply. |
||
| 76 | * |
||
| 77 | * @return Response |
||
| 78 | * |
||
| 79 | * @throws ForbiddenException |
||
| 80 | * @throws UnauthorizedException |
||
| 81 | * @throws MetricaException |
||
| 82 | */ |
||
| 83 | 26 | View Code Duplication | protected function sendRequest($method, $uri, array $options = []) |
| 84 | { |
||
| 85 | try { |
||
| 86 | 26 | $response = $this->getClient()->request($method, $uri, $options); |
|
| 87 | 26 | } catch (ClientException $ex) { |
|
| 88 | 4 | $result = $ex->getResponse(); |
|
| 89 | 4 | $code = $result->getStatusCode(); |
|
| 90 | 4 | $message = $result->getReasonPhrase(); |
|
| 91 | |||
| 92 | 4 | if ($code === 403) { |
|
| 93 | 1 | throw new ForbiddenException($message); |
|
| 94 | } |
||
| 95 | |||
| 96 | 3 | if ($code === 401) { |
|
| 97 | 1 | throw new UnauthorizedException($message); |
|
| 98 | } |
||
| 99 | |||
| 100 | 2 | if ($code === 429) { |
|
| 101 | 1 | throw new TooManyRequestsException($message); |
|
| 102 | } |
||
| 103 | |||
| 104 | 1 | throw new MetricaException( |
|
| 105 | 1 | 'Service responded with error code: "' . $code . '" and message: "' . $message . '"', |
|
| 106 | $code |
||
| 107 | 1 | ); |
|
| 108 | } |
||
| 109 | |||
| 110 | 22 | return $response; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Send GET request to API resource |
||
| 115 | * |
||
| 116 | * @param string $resource |
||
| 117 | * @param array $params |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | 5 | protected function sendGetRequest($resource, $params = []) |
|
| 121 | { |
||
| 122 | 5 | $response = $this->sendRequest( |
|
| 123 | 5 | 'GET', |
|
| 124 | 5 | $this->getServiceUrl($resource, $params), |
|
| 125 | [ |
||
| 126 | 'headers' => [ |
||
| 127 | 5 | 'Accept' => 'application/x-yametrika+json', |
|
| 128 | 5 | 'Content-Type' => 'application/x-yametrika+json', |
|
| 129 | ] |
||
| 130 | 5 | ] |
|
| 131 | 5 | ); |
|
| 132 | |||
| 133 | 1 | $decodedResponseBody = $this->getDecodedBody($response->getBody()); |
|
| 134 | |||
| 135 | 1 | View Code Duplication | if (isset($decodedResponseBody['links']) && isset($decodedResponseBody['links']['next'])) { |
| 136 | $url = $decodedResponseBody['links']['next']; |
||
| 137 | unset($decodedResponseBody['rows']); |
||
| 138 | unset($decodedResponseBody['links']); |
||
| 139 | return $this->getNextPartOfList($url, $decodedResponseBody); |
||
| 140 | } |
||
| 141 | 1 | return $decodedResponseBody; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Send custom GET request to API resource |
||
| 146 | * |
||
| 147 | * @param string $url |
||
| 148 | * @param array $data |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | protected function getNextPartOfList($url, $data = []) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Send POST request to API resource |
||
| 180 | * |
||
| 181 | * @param string $resource |
||
| 182 | * @param array $params |
||
| 183 | * @return array |
||
| 184 | */ |
||
| 185 | 7 | View Code Duplication | protected function sendPostRequest($resource, $params) |
| 201 | |||
| 202 | /** |
||
| 203 | * Send PUT request to API resource |
||
| 204 | * |
||
| 205 | * @param string $resource |
||
| 206 | * @param array $params |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | 7 | View Code Duplication | protected function sendPutRequest($resource, $params) |
| 225 | |||
| 226 | /** |
||
| 227 | * Send DELETE request to API resource |
||
| 228 | * |
||
| 229 | * @param string $resource |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | 7 | View Code Duplication | protected function sendDeleteRequest($resource) |
| 247 | } |
||
| 248 |
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.