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 | public function __construct(HttpClient $httpClient = null, array $configuration = []) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function withApiKey(string $apikey) :RandomOrgAPIInterface |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function withEndPoint(string $endpoint) :RandomOrgAPIInterface |
||
| 76 | 16 | ||
| 77 | 16 | /** |
|
| 78 | * {@inheritdoc} |
||
| 79 | */ |
||
| 80 | public function withHttpClient(HttpClient $client) :RandomOrgAPIInterface |
||
| 87 | 16 | ||
| 88 | 16 | /** |
|
| 89 | * {@inheritdoc} |
||
| 90 | 16 | */ |
|
| 91 | public function getEndPoint() :string |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | 13 | public function getApiKey() :string |
|
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | public function call(ProviderInterface $methodPlugin) :ResponseInterface |
||
| 133 | |||
| 134 | /** |
||
| 135 | 2 | * {@inheritdoc} |
|
| 136 | 2 | */ |
|
| 137 | 2 | public function get(ProviderInterface $methodPlugin) :array |
|
| 147 | 17 | ||
| 148 | 17 | /** |
|
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function getData(ProviderInterface $methodPlugin) |
||
| 165 | 16 | ||
| 166 | 16 | /** |
|
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | 16 | public function getConfiguration() :array |
|
| 173 | |||
| 174 | 16 | /** |
|
| 175 | 16 | * {@inheritdoc} |
|
| 176 | 16 | */ |
|
| 177 | public function getHttpClient() :HttpClient |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Validate the response. |
||
| 184 | * |
||
| 185 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 186 | 16 | * |
|
| 187 | 16 | * @return \Exception|ResponseInterface |
|
| 188 | */ |
||
| 189 | private function validateResponse(ResponseInterface $response) :ResponseInterface |
||
| 229 | } |
||
| 230 |
This check looks for
@paramannotations 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.