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 |
||
| 17 | class RandomOrgAPI implements RandomOrgAPIInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The default Random.org endpoint. |
||
| 21 | * |
||
| 22 | * @var string; |
||
| 23 | */ |
||
| 24 | private $endpoint = 'https://api.random.org/json-rpc/1/invoke'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The configuration. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $configuration; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The HTTP client. |
||
| 35 | * |
||
| 36 | * @var \Http\Client\HttpClient |
||
| 37 | */ |
||
| 38 | private $httpClient; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * RandomOrgAPI constructor. |
||
| 42 | * |
||
| 43 | * @param \Http\Client\HttpClient $httpClient |
||
|
|
|||
| 44 | * The HTTP client. |
||
| 45 | * @param array $configuration |
||
| 46 | * The configuration array. |
||
| 47 | * |
||
| 48 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
| 49 | */ |
||
| 50 | 10 | public function __construct(HttpClient $httpClient = null, array $configuration = []) |
|
| 51 | { |
||
| 52 | 10 | if (null === $httpClient) { |
|
| 53 | 10 | $httpClient = new PluginClient( |
|
| 54 | 10 | HttpClientDiscovery::find(), |
|
| 55 | [ |
||
| 56 | 10 | new HeaderDefaultsPlugin([ |
|
| 57 | 10 | 'Content-Type' => 'application/json', |
|
| 58 | 'User-Agent' => 'YAROC (http://github.com/drupol/yaroc)', |
||
| 59 | ]), |
||
| 60 | 10 | new RetryPlugin([ |
|
| 61 | 10 | 'retries' => 5, |
|
| 62 | ]), |
||
| 63 | ] |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | 10 | $this->httpClient = $httpClient; |
|
| 68 | |||
| 69 | 10 | $dotenv = new Dotenv(); |
|
| 70 | 10 | $files = array_filter( |
|
| 71 | [ |
||
| 72 | 10 | __DIR__ . '/../.env.dist', |
|
| 73 | __DIR__ . '/../.env' |
||
| 74 | ], |
||
| 75 | 10 | 'file_exists' |
|
| 76 | ); |
||
| 77 | 10 | $dotenv->load(...$files); |
|
| 78 | |||
| 79 | 10 | if ($apikey = getenv('RANDOM_ORG_APIKEY')) { |
|
| 80 | 10 | $configuration += ['apiKey' => $apikey]; |
|
| 81 | } |
||
| 82 | |||
| 83 | 10 | $this->configuration = $configuration; |
|
| 84 | 10 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | 3 | public function withApiKey(string $apikey) :RandomOrgAPIInterface |
|
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | 2 | public function withEndPoint(string $endpoint) :RandomOrgAPIInterface |
|
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 1 | public function withHttpClient(HttpClient $client) :RandomOrgAPIInterface |
|
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | 6 | public function getEndPoint() :string |
|
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | 6 | public function getApiKey() :string |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | 5 | public function call(ProviderInterface $methodPlugin) :ResponseInterface |
|
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | 3 | public function get(ProviderInterface $methodPlugin) :array |
|
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | 2 | public function getData(ProviderInterface $methodPlugin) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * {@inheritdoc} |
||
| 191 | */ |
||
| 192 | 7 | public function getConfiguration() :array |
|
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | */ |
||
| 200 | 6 | public function getHttpClient() :HttpClient |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Validate the response. |
||
| 207 | * |
||
| 208 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 209 | * |
||
| 210 | * @return \Exception|ResponseInterface |
||
| 211 | */ |
||
| 212 | 5 | private function validateResponse(ResponseInterface $response) :ResponseInterface |
|
| 252 | } |
||
| 253 |
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.