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 |
||
| 22 | class Client |
||
| 23 | { |
||
| 24 | const BASE_URL = 'https://api.coinpaprika.com/%ver%/'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $apiVersion; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var \GuzzleHttp\Client |
||
| 33 | */ |
||
| 34 | private $httpClient; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Serializer |
||
| 38 | */ |
||
| 39 | private $serializer; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Client constructor. |
||
| 43 | * |
||
| 44 | * @param string|null $cacheDir |
||
| 45 | * @param \GuzzleHttp\Client $httpClient |
||
| 46 | */ |
||
| 47 | 7 | public function __construct( |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Get global stats |
||
| 70 | * |
||
| 71 | * @throws GuzzleException |
||
| 72 | * @throws ResponseErrorException |
||
| 73 | * @throws RateLimitExceededException |
||
| 74 | * |
||
| 75 | * @return GlobalStats |
||
| 76 | */ |
||
| 77 | 1 | public function getGlobalStats(): GlobalStats |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Get tickers |
||
| 86 | * |
||
| 87 | * @throws GuzzleException |
||
| 88 | * @throws ResponseErrorException |
||
| 89 | * @throws RateLimitExceededException |
||
| 90 | * |
||
| 91 | * @return array|Ticker[] |
||
| 92 | */ |
||
| 93 | 1 | public function getTickers(): array |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Get coin`s ticker data |
||
| 102 | * |
||
| 103 | * @param string $id |
||
| 104 | * |
||
| 105 | * @throws GuzzleException |
||
| 106 | * @throws ResponseErrorException |
||
| 107 | * @throws RateLimitExceededException |
||
| 108 | * |
||
| 109 | * @return Ticker |
||
| 110 | */ |
||
| 111 | 3 | View Code Duplication | public function getTickerByCoinId(string $id): Ticker |
| 120 | |||
| 121 | /** |
||
| 122 | * Get coins list |
||
| 123 | * |
||
| 124 | * @throws GuzzleException |
||
| 125 | * @throws ResponseErrorException |
||
| 126 | * @throws RateLimitExceededException |
||
| 127 | * |
||
| 128 | * @return array|Coin[] |
||
| 129 | */ |
||
| 130 | 1 | View Code Duplication | public function getCoins(): array |
| 137 | |||
| 138 | /** |
||
| 139 | * Get the endpoint URL. |
||
| 140 | * |
||
| 141 | * @param string $endpoint |
||
| 142 | * |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | 6 | protected function getEndpointUrl(string $endpoint): string |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Get the API version |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 7 | public function getApiVersion(): string |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Unmarshal JSON |
||
| 162 | * |
||
| 163 | * @param ResponseInterface $response |
||
| 164 | * @param string $type |
||
| 165 | * |
||
| 166 | * @throws ResponseErrorException |
||
| 167 | * @throws RateLimitExceededException |
||
| 168 | * |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | 6 | private function deserializeResponse(ResponseInterface $response, string $type) |
|
| 186 | } |
||
| 187 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: