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 |
||
17 | class RandomOrgAPI implements RandomOrgAPIInterface |
||
18 | { |
||
19 | /** |
||
20 | * The default Random.org endpoint. |
||
21 | * |
||
22 | * @var string; |
||
23 | */ |
||
24 | private $endpoint = 'https://api.random.org/json-rpc/1/invoke'; |
||
25 | |||
26 | /** |
||
27 | * The configuration. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $configuration; |
||
32 | |||
33 | /** |
||
34 | * The HTTP client. |
||
35 | * |
||
36 | * @var \Http\Client\HttpClient |
||
37 | */ |
||
38 | private $httpClient; |
||
39 | |||
40 | /** |
||
41 | * RandomOrgAPI constructor. |
||
42 | * |
||
43 | * @param \Http\Client\HttpClient $httpClient |
||
|
|||
44 | * The HTTP client. |
||
45 | * @param array $configuration |
||
46 | * The configuration array. |
||
47 | * |
||
48 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
49 | */ |
||
50 | 10 | public function __construct(HttpClient $httpClient = null, array $configuration = []) |
|
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 3 | public function withApiKey(string $apikey) :RandomOrgAPIInterface |
|
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 2 | public function withEndPoint(string $endpoint) :RandomOrgAPIInterface |
|
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 1 | public function withHttpClient(HttpClient $client) :RandomOrgAPIInterface |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 6 | public function getEndPoint() :string |
|
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 6 | public function getApiKey() :string |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 5 | public function call(ProviderInterface $methodPlugin) :ResponseInterface |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 3 | public function get(ProviderInterface $methodPlugin) :array |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 2 | public function getData(ProviderInterface $methodPlugin) |
|
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 7 | public function getConfiguration() :array |
|
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 6 | public function getHttpClient() :HttpClient |
|
204 | |||
205 | /** |
||
206 | * Validate the response. |
||
207 | * |
||
208 | * @param \Psr\Http\Message\ResponseInterface $response |
||
209 | * |
||
210 | * @return \Exception|ResponseInterface |
||
211 | */ |
||
212 | 5 | private function validateResponse(ResponseInterface $response) :ResponseInterface |
|
252 | } |
||
253 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.