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 |
||
16 | class HttpAdapter extends AbstractTransport |
||
17 | { |
||
18 | /** |
||
19 | * @var HttpAdapterInterface |
||
20 | */ |
||
21 | private $httpAdapter; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $_scheme = 'http'; |
||
27 | |||
28 | /** |
||
29 | * Construct transport. |
||
30 | * |
||
31 | * @param Connection $connection |
||
32 | * @param HttpAdapterInterface $httpAdapter |
||
33 | */ |
||
34 | public function __construct(Connection $connection = null, HttpAdapterInterface $httpAdapter) |
||
39 | |||
40 | /** |
||
41 | * Makes calls to the elasticsearch server. |
||
42 | * |
||
43 | * All calls that are made to the server are done through this function |
||
44 | * |
||
45 | * @param \Elastica\Request $elasticaRequest |
||
46 | * @param array $params Host, Port, ... |
||
47 | * |
||
48 | * @throws \Elastica\Exception\ConnectionException |
||
49 | * @throws \Elastica\Exception\ResponseException |
||
50 | * @throws \Elastica\Exception\Connection\HttpException |
||
51 | * |
||
52 | * @return \Elastica\Response Response object |
||
53 | */ |
||
54 | public function exec(ElasticaRequest $elasticaRequest, array $params) |
||
55 | { |
||
56 | $connection = $this->getConnection(); |
||
57 | |||
58 | if ($timeout = $connection->getTimeout()) { |
||
59 | $this->httpAdapter->getConfiguration()->setTimeout($timeout); |
||
60 | } |
||
61 | |||
62 | $httpAdapterRequest = $this->_createHttpAdapterRequest($elasticaRequest, $connection); |
||
63 | |||
64 | $start = microtime(true); |
||
65 | $httpAdapterResponse = $this->httpAdapter->sendRequest($httpAdapterRequest); |
||
66 | $end = microtime(true); |
||
67 | |||
68 | $elasticaResponse = $this->_createElasticaResponse($httpAdapterResponse); |
||
69 | $elasticaResponse->setQueryTime($end - $start); |
||
70 | |||
71 | $elasticaResponse->setTransferInfo( |
||
72 | [ |
||
73 | 'request_header' => $httpAdapterRequest->getMethod(), |
||
74 | 'http_code' => $httpAdapterResponse->getStatusCode(), |
||
75 | ] |
||
76 | ); |
||
77 | |||
78 | if ($elasticaResponse->hasError()) { |
||
79 | throw new ResponseException($elasticaRequest, $elasticaResponse); |
||
80 | } |
||
81 | |||
82 | if ($elasticaResponse->hasFailedShards()) { |
||
83 | throw new PartialShardFailureException($elasticaRequest, $elasticaResponse); |
||
84 | } |
||
85 | |||
86 | return $elasticaResponse; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param HttpAdapterResponse $httpAdapterResponse |
||
91 | * |
||
92 | * @return ElasticaResponse |
||
93 | */ |
||
94 | protected function _createElasticaResponse(HttpAdapterResponse $httpAdapterResponse) |
||
98 | |||
99 | /** |
||
100 | * @param ElasticaRequest $elasticaRequest |
||
101 | * @param Connection $connection |
||
102 | * |
||
103 | * @return HttpAdapterRequest |
||
104 | */ |
||
105 | protected function _createHttpAdapterRequest(ElasticaRequest $elasticaRequest, Connection $connection) |
||
133 | |||
134 | /** |
||
135 | * @param ElasticaRequest $request |
||
136 | * @param \Elastica\Connection $connection |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | protected function _getUri(ElasticaRequest $request, Connection $connection) |
||
165 | } |
||
166 |
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.