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 |
||
| 12 | class RandomOrgAPI implements RandomOrgAPIInterface |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The default Random.org endpoint template. |
||
| 17 | * |
||
| 18 | * @var string; |
||
| 19 | */ |
||
| 20 | private $endpoint = 'https://api.random.org/json-rpc/1/invoke'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The configuration. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $configuration = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The HTTP client. |
||
| 31 | * |
||
| 32 | * @var \Http\Client\HttpClient |
||
| 33 | */ |
||
| 34 | private $httpClient; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * RandomOrgAPI constructor. |
||
| 38 | * |
||
| 39 | * @param array $configuration |
||
| 40 | */ |
||
| 41 | public function __construct(array $configuration) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | public function withApiKey(string $apikey) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | public function withEndPoint(string $endpoint) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function withHttpClient(HttpClient $client) |
||
| 75 | 16 | ||
| 76 | 16 | /** |
|
| 77 | 16 | * {@inheritdoc} |
|
| 78 | */ |
||
| 79 | public function getEndPoint() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * {@inheritdoc} |
||
| 86 | */ |
||
| 87 | 16 | public function getApiKey() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | 13 | public function call(MethodPluginInterface $methodPlugin) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function get(MethodPluginInterface $methodPlugin) |
||
| 115 | 16 | ||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function getData(MethodPluginInterface $methodPlugin) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | 2 | public function getConfiguration() |
|
| 139 | 2 | ||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function getHttpClient() |
||
| 147 | 17 | ||
| 148 | 17 | /** |
|
| 149 | * @param string $apikey |
||
| 150 | * |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | private function setApiKey(string $apikey) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $endpoint |
||
| 163 | 16 | * |
|
| 164 | * @return $this |
||
| 165 | 16 | */ |
|
| 166 | 16 | private function setEndPoint(string $endpoint) |
|
| 172 | |||
| 173 | /** |
||
| 174 | 16 | * Validate the response. |
|
| 175 | 16 | * |
|
| 176 | 16 | * @param \Psr\Http\Message\ResponseInterface $response |
|
| 177 | * |
||
| 178 | 16 | * @return \Exception|ResponseInterface |
|
| 179 | */ |
||
| 180 | private function validateResponse(ResponseInterface $response) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param $configuration |
||
| 208 | * |
||
| 209 | 12 | * @return $this |
|
| 210 | 12 | */ |
|
| 211 | private function setConfiguration($configuration) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Set the HTTP client. |
||
| 220 | * |
||
| 221 | 16 | * @param \Http\Client\HttpClient $client |
|
| 222 | 16 | * |
|
| 223 | * @return \drupol\Yaroc\RandomOrgAPI |
||
| 224 | 16 | */ |
|
| 225 | private function setHttpClient(HttpClient $client) |
||
| 231 | } |
||
| 232 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: