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 | 5 | public function __construct(HttpClient $httpClient = null, array $configuration = []) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | 2 | public function withApiKey(string $apikey) :RandomOrgAPIInterface |
|
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | */ |
||
| 84 | 1 | public function withEndPoint(string $endpoint) :RandomOrgAPIInterface |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | 1 | public function withHttpClient(HttpClient $client) :RandomOrgAPIInterface |
|
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | 1 | public function getEndPoint() :string |
|
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 1 | public function getApiKey() :string |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | public function call(ProviderInterface $methodPlugin) :ResponseInterface |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | public function get(ProviderInterface $methodPlugin) :array |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function getData(ProviderInterface $methodPlugin) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * {@inheritdoc} |
||
| 172 | */ |
||
| 173 | 2 | public function getConfiguration() :array |
|
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | 1 | public function getHttpClient() :HttpClient |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Validate the response. |
||
| 188 | * |
||
| 189 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 190 | * |
||
| 191 | * @return \Exception|ResponseInterface |
||
| 192 | */ |
||
| 193 | private function validateResponse(ResponseInterface $response) :ResponseInterface |
||
| 233 | } |
||
| 234 |
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.