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 |
||
14 | class RandomOrgAPI implements RandomOrgAPIInterface |
||
15 | { |
||
16 | /** |
||
17 | * The default Random.org endpoint. |
||
18 | * |
||
19 | * @var string; |
||
20 | */ |
||
21 | private $endpoint = 'https://api.random.org/json-rpc/1/invoke'; |
||
22 | |||
23 | /** |
||
24 | * The configuration. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | private $configuration; |
||
29 | |||
30 | /** |
||
31 | * The HTTP client. |
||
32 | * |
||
33 | * @var \Http\Client\HttpClient |
||
34 | */ |
||
35 | private $httpClient; |
||
36 | |||
37 | /** |
||
38 | * RandomOrgAPI constructor. |
||
39 | * |
||
40 | * @param \Http\Client\HttpClient $httpClient |
||
|
|||
41 | * The HTTP client. |
||
42 | * @param array $configuration |
||
43 | * The configuration array. |
||
44 | * |
||
45 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
46 | */ |
||
47 | 9 | public function __construct(HttpClient $httpClient = null, array $configuration = []) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 3 | public function withApiKey(string $apikey) :RandomOrgAPIInterface |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 1 | public function withEndPoint(string $endpoint) :RandomOrgAPIInterface |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 1 | public function withHttpClient(HttpClient $client) :RandomOrgAPIInterface |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 5 | public function getEndPoint() :string |
|
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | 5 | public function getApiKey() :string |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 4 | public function call(ProviderInterface $methodPlugin) :ResponseInterface |
|
127 | { |
||
128 | 4 | $parameters = $methodPlugin->getParameters() + |
|
129 | 4 | ['apiKey' => $this->getApiKey()]; |
|
130 | |||
131 | 4 | return $this->validateResponse( |
|
132 | $methodPlugin |
||
133 | 4 | ->withEndPoint($this->getEndPoint()) |
|
134 | 4 | ->withHttpClient($this->getHttpClient()) |
|
135 | 4 | ->withParameters($parameters) |
|
136 | 4 | ->request() |
|
137 | ); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 2 | public function get(ProviderInterface $methodPlugin) :array |
|
144 | { |
||
145 | 2 | return json_decode( |
|
146 | (string) $this |
||
147 | 2 | ->call($methodPlugin) |
|
148 | 2 | ->getBody() |
|
149 | 2 | ->getContents(), |
|
150 | 2 | true |
|
151 | ); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | 1 | public function getData(ProviderInterface $methodPlugin) |
|
158 | { |
||
159 | 1 | $data = $this->get($methodPlugin); |
|
160 | |||
161 | 1 | if (!isset($data['result'])) { |
|
162 | return false; |
||
163 | } |
||
164 | |||
165 | 1 | if (isset($data['result']['random']['data'])) { |
|
166 | 1 | return $data['result']['random']['data']; |
|
167 | } |
||
168 | |||
169 | return false; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | 6 | public function getConfiguration() :array |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | 5 | public function getHttpClient() :HttpClient |
|
187 | |||
188 | /** |
||
189 | * Validate the response. |
||
190 | * |
||
191 | * @param \Psr\Http\Message\ResponseInterface $response |
||
192 | * |
||
193 | * @return \Exception|ResponseInterface |
||
194 | */ |
||
195 | 4 | private function validateResponse(ResponseInterface $response) :ResponseInterface |
|
235 | } |
||
236 |
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.