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 |
||
| 13 | class RandomOrgAPI implements RandomOrgAPIInterface |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The default Random.org endpoint template. |
||
| 17 | * |
||
| 18 | * @var string; |
||
| 19 | */ |
||
| 20 | private $endpoint = 'https://api.random.org/json-rpc/1/invoke'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The configuration. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $configuration; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The HTTP client. |
||
| 31 | * |
||
| 32 | * @var \Http\Client\HttpClient |
||
| 33 | */ |
||
| 34 | private $httpClient; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * RandomOrgAPI constructor. |
||
| 38 | * |
||
| 39 | * @param \Http\Client\HttpClient $httpClient |
||
|
|
|||
| 40 | * @param array $configuration |
||
| 41 | */ |
||
| 42 | 5 | public function __construct(HttpClient $httpClient = null, array $configuration = []) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | 2 | public function withApiKey(string $apikey) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 1 | public function withEndPoint(string $endpoint) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | 1 | public function withHttpClient(HttpClient $client) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | 1 | public function getEndPoint() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * {@inheritdoc} |
||
| 95 | */ |
||
| 96 | 1 | public function getApiKey() |
|
| 103 | |||
| 104 | /** |
||
| 105 | * {@inheritdoc} |
||
| 106 | */ |
||
| 107 | public function call(MethodPluginInterface $methodPlugin) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | public function get(MethodPluginInterface $methodPlugin) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | public function getData(MethodPluginInterface $methodPlugin) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * {@inheritdoc} |
||
| 155 | */ |
||
| 156 | 2 | public function getConfiguration() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * {@inheritdoc} |
||
| 163 | */ |
||
| 164 | 1 | public function getHttpClient() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Validate the response. |
||
| 171 | * |
||
| 172 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 173 | * |
||
| 174 | * @return \Exception|ResponseInterface |
||
| 175 | */ |
||
| 176 | private function validateResponse(ResponseInterface $response) |
||
| 216 | } |
||
| 217 |
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.