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:
Complex classes like Bulk often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Bulk, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Bulk |
||
| 15 | { |
||
| 16 | const DELIMITER = "\n"; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Client |
||
| 20 | */ |
||
| 21 | protected $_client; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var Action[] |
||
| 25 | */ |
||
| 26 | protected $_actions = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string|null |
||
| 30 | */ |
||
| 31 | protected $_index; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string|null |
||
| 35 | */ |
||
| 36 | protected $_type; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array request parameters to the bulk api |
||
| 40 | */ |
||
| 41 | protected $_requestParams = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param Client $client |
||
| 45 | */ |
||
| 46 | public function __construct(Client $client) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string|Index $index |
||
| 53 | * |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | View Code Duplication | public function setIndex($index): self |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @return string|null |
||
| 69 | */ |
||
| 70 | public function getIndex() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function hasIndex(): bool |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function getPath(): string |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param Action $action |
||
| 99 | * |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | public function addAction(Action $action): self |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param Action[] $actions |
||
| 111 | * |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | public function addActions(array $actions): self |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return Action[] |
||
| 125 | */ |
||
| 126 | public function getActions(): array |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param Document $document |
||
| 133 | * @param string $opType |
||
| 134 | * |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function addDocument(Document $document, string $opType = null): self |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param Document[] $documents |
||
| 146 | * @param string $opType |
||
| 147 | * |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | public function addDocuments(array $documents, string $opType = null): self |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param AbstractScript $script |
||
| 161 | * @param string $opType |
||
| 162 | * |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | public function addScript(AbstractScript $script, string $opType = null): self |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param Document[] $scripts |
||
| 174 | * @param string $opType |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function addScripts(array $scripts, $opType = null): self |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param \Elastica\Script\AbstractScript|\Elastica\Document|array $data |
||
| 189 | * @param string $opType |
||
| 190 | * |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | public function addData($data, string $opType = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param array $data |
||
| 214 | * |
||
| 215 | * @throws InvalidException |
||
| 216 | * |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function addRawData(array $data): self |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set a url parameter on the request bulk request. |
||
| 253 | * |
||
| 254 | * @param string $name name of the parameter |
||
| 255 | * @param mixed $value value of the parameter |
||
| 256 | * |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function setRequestParam(string $name, $value): self |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Set the amount of time that the request will wait the shards to come on line. |
||
| 268 | * Requires Elasticsearch version >= 0.90.8. |
||
| 269 | * |
||
| 270 | * @param string $time timeout in Elasticsearch time format |
||
| 271 | * |
||
| 272 | * @return $this |
||
| 273 | */ |
||
| 274 | public function setShardTimeout(string $time): self |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function __toString(): string |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function toString(): string |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function toArray(): array |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @return ResponseSet |
||
| 317 | */ |
||
| 318 | public function send(): ResponseSet |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param Response $response |
||
| 330 | * |
||
| 331 | * @throws ResponseException |
||
| 332 | * @throws InvalidException |
||
| 333 | * |
||
| 334 | * @return ResponseSet |
||
| 335 | */ |
||
| 336 | protected function _processResponse(Response $response): ResponseSet |
||
| 381 | } |
||
| 382 |
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.