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. |
||
| 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 | * The HTTP client. |
||
| 41 | * @param array $configuration |
||
| 42 | * The configuration array. |
||
| 43 | */ |
||
| 44 | 5 | public function __construct(HttpClient $httpClient = null, array $configuration = []) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | 2 | public function withApiKey(string $apikey) :RandomOrgAPIInterface |
|
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | 1 | public function withEndPoint(string $endpoint) :RandomOrgAPIInterface |
|
| 75 | |||
| 76 | /** |
||
| 77 | * {@inheritdoc} |
||
| 78 | */ |
||
| 79 | 1 | public function withHttpClient(HttpClient $client) :RandomOrgAPIInterface |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 1 | public function getEndPoint() :string |
|
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | 1 | public function getApiKey() :string |
|
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function call(ProviderInterface $methodPlugin) :ResponseInterface |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function get(ProviderInterface $methodPlugin) :array |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function getData(ProviderInterface $methodPlugin) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | 2 | public function getConfiguration() :array |
|
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | 1 | public function getHttpClient() :HttpClient |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Validate the response. |
||
| 173 | * |
||
| 174 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 175 | * |
||
| 176 | * @return \Exception|ResponseInterface |
||
| 177 | */ |
||
| 178 | private function validateResponse(ResponseInterface $response) :ResponseInterface |
||
| 218 | } |
||
| 219 |
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.