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