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 | * The default Random.org endpoint template. |
||
| 16 | * |
||
| 17 | * @var string; |
||
| 18 | */ |
||
| 19 | private $endpoint = 'https://api.random.org/json-rpc/1/invoke'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The configuration. |
||
| 23 | * |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | private $configuration; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The HTTP client. |
||
| 30 | * |
||
| 31 | * @var \Http\Client\HttpClient |
||
| 32 | */ |
||
| 33 | private $httpClient; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * RandomOrgAPI constructor. |
||
| 37 | * |
||
| 38 | * @param array $configuration |
||
| 39 | */ |
||
| 40 | public function __construct(array $configuration = []) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * {@inheritdoc} |
||
| 47 | */ |
||
| 48 | public function withApiKey(string $apikey) |
||
| 49 | { |
||
| 50 | $clone = clone $this; |
||
| 51 | |||
| 52 | $configuration = $clone->getConfiguration(); |
||
| 53 | $configuration['apiKey'] = $apikey; |
||
| 54 | |||
| 55 | $clone->configuration = $configuration; |
||
| 56 | |||
| 57 | return $clone; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * {@inheritdoc} |
||
| 62 | */ |
||
| 63 | public function withEndPoint(string $endpoint) |
||
| 64 | { |
||
| 65 | $clone = clone $this; |
||
| 66 | $clone->endpoint = $endpoint; |
||
| 67 | |||
| 68 | return $clone; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | 16 | */ |
|
| 74 | 16 | public function withHttpClient(HttpClient $client) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function getEndPoint() |
||
| 89 | |||
| 90 | 16 | /** |
|
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function getApiKey() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function call(MethodPluginInterface $methodPlugin) |
||
| 114 | |||
| 115 | 16 | /** |
|
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | public function get(MethodPluginInterface $methodPlugin) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | public function getData(MethodPluginInterface $methodPlugin) |
||
| 146 | |||
| 147 | 17 | /** |
|
| 148 | 17 | * {@inheritdoc} |
|
| 149 | */ |
||
| 150 | public function getConfiguration() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function getHttpClient() |
||
| 162 | |||
| 163 | 16 | /** |
|
| 164 | * Validate the response. |
||
| 165 | 16 | * |
|
| 166 | 16 | * @param \Psr\Http\Message\ResponseInterface $response |
|
| 167 | * |
||
| 168 | * @return \Exception|ResponseInterface |
||
| 169 | 16 | */ |
|
| 170 | 16 | private function validateResponse(ResponseInterface $response) |
|
| 210 | } |
||
| 211 |
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: