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 |
||
| 26 | class Client extends HttpMethodsClient { |
||
|
|
|||
| 27 | |||
| 28 | /** |
||
| 29 | * The default Random.org endpoint. |
||
| 30 | * |
||
| 31 | * @var UriInterface |
||
| 32 | */ |
||
| 33 | protected $endpoint; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The URI Factory. |
||
| 37 | * |
||
| 38 | * @var UriFactory |
||
| 39 | */ |
||
| 40 | protected $uriFactory; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The logger. |
||
| 44 | * |
||
| 45 | * @var \Psr\Log\LoggerInterface |
||
| 46 | */ |
||
| 47 | protected $logger; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Client constructor. |
||
| 51 | * |
||
| 52 | * @param \Http\Client\HttpClient|NULL $httpClient |
||
| 53 | * @param \Http\Message\UriFactory|NULL $uriFactory |
||
| 54 | * @param \Psr\Log\LoggerInterface|NULL $logger |
||
| 55 | */ |
||
| 56 | 15 | public function __construct(HttpClient $httpClient = NULL, UriFactory $uriFactory = NULL, LoggerInterface $logger = NULL) { |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Set the Random.org endpoint. |
||
| 71 | * |
||
| 72 | * @param string $uri |
||
| 73 | */ |
||
| 74 | 15 | public function setEndpoint($uri) { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Get the Random.org endpoint. |
||
| 80 | * |
||
| 81 | * @return UriInterface |
||
| 82 | */ |
||
| 83 | 12 | public function getEndpoint() { |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Request. |
||
| 89 | * |
||
| 90 | * @param MethodPluginInterface $methodPlugin |
||
| 91 | * |
||
| 92 | * @return null|ResponseInterface |
||
| 93 | */ |
||
| 94 | 12 | public function request(MethodPluginInterface $methodPlugin) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Validate the response. |
||
| 106 | * |
||
| 107 | * @param \Psr\Http\Message\ResponseInterface $response |
||
| 108 | * |
||
| 109 | * @return \Exception|ResponseInterface |
||
| 110 | */ |
||
| 111 | 12 | public function validateResponse(ResponseInterface $response) { |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get the logger. |
||
| 138 | * |
||
| 139 | * @return \Psr\Log\LoggerInterface |
||
| 140 | */ |
||
| 141 | public function getLogger() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set the logger. |
||
| 147 | * |
||
| 148 | * @param \Psr\Log\LoggerInterface $logger |
||
| 149 | */ |
||
| 150 | public function setLogger(LoggerInterface $logger) { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Returns the UriFactory. |
||
| 156 | * |
||
| 157 | * @return \Http\Message\UriFactory |
||
| 158 | */ |
||
| 159 | 15 | public function getUriFactory() { |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @param \Http\Message\UriFactory $uriFactory |
||
| 165 | */ |
||
| 166 | 15 | public function setUriFactory(UriFactory $uriFactory) { |
|
| 169 | |||
| 170 | } |
||
| 171 |