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) |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function withEndPoint(string $endpoint) |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | public function withHttpClient(HttpClient $client) |
||
74 | 16 | ||
75 | 16 | /** |
|
76 | 16 | * {@inheritdoc} |
|
77 | 16 | */ |
|
78 | public function getEndPoint() |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function getApiKey() |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function call(MethodPluginInterface $methodPlugin) |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function get(MethodPluginInterface $methodPlugin) |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function getData(MethodPluginInterface $methodPlugin) |
||
136 | 2 | ||
137 | 2 | /** |
|
138 | * {@inheritdoc} |
||
139 | 2 | */ |
|
140 | public function getConfiguration() |
||
144 | |||
145 | /** |
||
146 | * {@inheritdoc} |
||
147 | 17 | */ |
|
148 | 17 | public function getHttpClient() |
|
152 | |||
153 | /** |
||
154 | * @param string $apikey |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | private function setApiKey(string $apikey) |
||
165 | 16 | ||
166 | 16 | /** |
|
167 | * @param string $endpoint |
||
168 | * |
||
169 | 16 | * @return $this |
|
170 | 16 | */ |
|
171 | private function setEndPoint(string $endpoint) |
||
177 | |||
178 | 16 | /** |
|
179 | * Validate the response. |
||
180 | * |
||
181 | * @param \Psr\Http\Message\ResponseInterface $response |
||
182 | * |
||
183 | * @return \Exception|ResponseInterface |
||
184 | */ |
||
185 | private function validateResponse(ResponseInterface $response) |
||
225 | |||
226 | /** |
||
227 | * @param $configuration |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | private function setConfiguration($configuration) |
||
237 | |||
238 | /** |
||
239 | * Set the HTTP client. |
||
240 | * |
||
241 | * @param \Http\Client\HttpClient $client |
||
242 | * |
||
243 | 13 | * @return \drupol\Yaroc\RandomOrgAPI |
|
244 | 13 | */ |
|
245 | private function setHttpClient(HttpClient $client) |
||
251 | } |
||
252 |
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: