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 | public function __construct(HttpClient $httpClient = null, array $configuration = []) |
||
| 45 | { |
||
| 46 | $this->httpClient = $httpClient ?? HttpClientDiscovery::find(); |
||
| 47 | $this->configuration = $configuration; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | public function withApiKey(string $apikey) :RandomOrgAPIInterface |
||
| 54 | { |
||
| 55 | $clone = clone $this; |
||
| 56 | |||
| 57 | $configuration = $clone->getConfiguration(); |
||
| 58 | $configuration['apiKey'] = $apikey; |
||
| 59 | |||
| 60 | $clone->configuration = $configuration; |
||
| 61 | |||
| 62 | return $clone; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public function withEndPoint(string $endpoint) :RandomOrgAPIInterface |
||
| 69 | { |
||
| 70 | $clone = clone $this; |
||
| 71 | $clone->endpoint = $endpoint; |
||
| 72 | |||
| 73 | 16 | return $clone; |
|
| 74 | 16 | } |
|
| 75 | 16 | ||
| 76 | 16 | /** |
|
| 77 | 16 | * {@inheritdoc} |
|
| 78 | */ |
||
| 79 | public function withHttpClient(HttpClient $client) :RandomOrgAPIInterface |
||
| 80 | { |
||
| 81 | $clone = clone $this; |
||
| 82 | $clone->httpClient = $client; |
||
| 83 | |||
| 84 | return $clone; |
||
| 85 | } |
||
| 86 | |||
| 87 | 16 | /** |
|
| 88 | 16 | * {@inheritdoc} |
|
| 89 | */ |
||
| 90 | 16 | public function getEndPoint() :string |
|
| 91 | { |
||
| 92 | return $this->endpoint; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function getApiKey() :string |
||
| 99 | 13 | { |
|
| 100 | 13 | $configuration = $this->getConfiguration() |
|
| 101 | + ['apiKey' => '']; |
||
| 102 | |||
| 103 | return $configuration['apiKey']; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function call(ProviderInterface $methodPlugin) :ResponseInterface |
||
| 110 | { |
||
| 111 | 16 | $parameters = $methodPlugin->getParameters() + |
|
| 112 | 16 | ['apiKey' => $this->getApiKey()]; |
|
| 113 | 16 | ||
| 114 | return $this->validateResponse( |
||
| 115 | 16 | $methodPlugin |
|
| 116 | ->withHttpClient($this->getHttpClient()) |
||
| 117 | ->withEndPoint($this->getEndPoint()) |
||
| 118 | ->withParameters($parameters) |
||
| 119 | ->request() |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 123 | 16 | /** |
|
| 124 | 16 | * {@inheritdoc} |
|
| 125 | */ |
||
| 126 | public function get(ProviderInterface $methodPlugin) :array |
||
| 127 | { |
||
| 128 | return json_decode( |
||
| 129 | (string) $this |
||
| 130 | ->call($methodPlugin) |
||
| 131 | ->getBody() |
||
| 132 | ->getContents(), |
||
| 133 | true |
||
| 134 | ); |
||
| 135 | 2 | } |
|
| 136 | 2 | ||
| 137 | 2 | /** |
|
| 138 | * {@inheritdoc} |
||
| 139 | 2 | */ |
|
| 140 | public function getData(ProviderInterface $methodPlugin) |
||
| 141 | { |
||
| 142 | $data = $this->get($methodPlugin); |
||
| 143 | |||
| 144 | if (!isset($data['result'])) { |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | 17 | ||
| 148 | 17 | if (isset($data['result']['random']['data'])) { |
|
| 149 | return $data['result']['random']['data']; |
||
| 150 | } |
||
| 151 | |||
| 152 | return false; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function getConfiguration() :array |
||
| 159 | { |
||
| 160 | return $this->configuration; |
||
| 161 | } |
||
| 162 | |||
| 163 | 16 | /** |
|
| 164 | * {@inheritdoc} |
||
| 165 | 16 | */ |
|
| 166 | 16 | public function getHttpClient() :HttpClient |
|
| 170 | 16 | ||
| 171 | /** |
||
| 172 | * Validate the response. |
||
| 173 | * |
||
| 174 | 16 | * @param \Psr\Http\Message\ResponseInterface $response |
|
| 175 | 16 | * |
|
| 176 | 16 | * @return \Exception|ResponseInterface |
|
| 177 | */ |
||
| 178 | 16 | private function validateResponse(ResponseInterface $response) :ResponseInterface |
|
| 179 | { |
||
| 180 | if (200 === $response->getStatusCode()) { |
||
| 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.