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 GuzzleHttpAdapter implements HttpAdapterInterface |
||
| 27 | { |
||
| 28 | /** @var ServerRequest */ |
||
| 29 | private $request; |
||
| 30 | /** @var ResponseInterface */ |
||
| 31 | private $response; |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | private $server; |
||
| 35 | /** @var array */ |
||
| 36 | private $get; |
||
| 37 | /** @var array */ |
||
| 38 | private $post; |
||
| 39 | /** @var array */ |
||
| 40 | private $cookie; |
||
| 41 | /** @var array */ |
||
| 42 | private $files; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * GuzzleHTTPAdapter constructor. |
||
| 46 | * |
||
| 47 | * @param array $server |
||
| 48 | * @param array $get |
||
| 49 | * @param array $post |
||
| 50 | * @param array $cookie |
||
| 51 | * @param array $files |
||
| 52 | */ |
||
| 53 | View Code Duplication | public function __construct(array $server, array $get, array $post, array $cookie, array $files) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Initialize adapter: create the ServerRequest and Response instances. |
||
| 66 | */ |
||
| 67 | private function initialize() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Gets the specific server data, or a default value if not present. |
||
| 94 | * |
||
| 95 | * @param string $keyName |
||
| 96 | * @param mixed $defaultValue |
||
| 97 | * |
||
| 98 | * @return mixed|null |
||
| 99 | */ |
||
| 100 | private function getServerData($keyName, $defaultValue = '') |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Gets server scheme. |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | private function getScheme() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Gets the server host name. |
||
| 128 | * |
||
| 129 | * @return string |
||
| 130 | */ |
||
| 131 | private function getHost() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Gets the server request uri. |
||
| 146 | * |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | private function getRequestUri() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Gets the server protocol. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | private function getProtocol() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns the HTTP request. |
||
| 175 | * |
||
| 176 | * @return ServerRequestInterface |
||
| 177 | */ |
||
| 178 | public function getRequest() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Returns the response being sent. |
||
| 185 | * |
||
| 186 | * @return ResponseInterface |
||
| 187 | */ |
||
| 188 | public function getResponse() |
||
| 192 | } |
||
| 193 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.