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 | 5 | public function __construct(HttpClient $httpClient = null, array $configuration = []) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * {@inheritdoc} |
||
| 71 | */ |
||
| 72 | 2 | 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 | 1 | public function getEndPoint() :string |
|
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | 1 | public function getApiKey() :string |
|
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function call(ProviderInterface $methodPlugin) :ResponseInterface |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function get(ProviderInterface $methodPlugin) :array |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function getData(ProviderInterface $methodPlugin) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | 2 | public function getConfiguration() :array |
|
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | 1 | 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 | private function validateResponse(ResponseInterface $response) :ResponseInterface |
||
| 235 | } |
||
| 236 |
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.