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 Repository 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 Repository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Repository |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var string Fully qualified class namespace |
||
| 39 | */ |
||
| 40 | private $classNamespace; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string Elasticsearch type name |
||
| 44 | */ |
||
| 45 | private $type; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array Manager configuration |
||
| 49 | */ |
||
| 50 | private $config = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var Client |
||
| 54 | */ |
||
| 55 | private $client; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Converter |
||
| 59 | */ |
||
| 60 | private $converter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array Container for bulk queries |
||
| 64 | */ |
||
| 65 | private $bulkQueries = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array Holder for consistency, refresh and replication parameters |
||
| 69 | */ |
||
| 70 | private $bulkParams = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $indexSettings; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var MetadataCollector |
||
| 79 | */ |
||
| 80 | private $metadataCollector; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * After commit to make data available the refresh or flush operation is needed |
||
| 84 | * so one of those methods has to be defined, the default is refresh. |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | private $commitMode = 'refresh'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The size that defines after how much document inserts call commit function. |
||
| 92 | * |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | private $bulkCommitSize = 100; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Container to count how many documents was passed to the bulk query. |
||
| 99 | * |
||
| 100 | * @var int |
||
| 101 | */ |
||
| 102 | private $bulkCount = 0; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var Repository[] Repository local cache |
||
| 106 | */ |
||
| 107 | private $repositories; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var EventDispatcherInterface |
||
| 111 | */ |
||
| 112 | private $eventDispatcher; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var Stopwatch |
||
| 116 | */ |
||
| 117 | private $stopwatch; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $classNamespace |
||
| 121 | * @param array $config |
||
| 122 | * @param Client $client |
||
| 123 | * @param array $indexSettings |
||
| 124 | * @param MetadataCollector $metadataCollector |
||
| 125 | * @param Converter $converter |
||
| 126 | */ |
||
| 127 | public function __construct( |
||
| 128 | $classNamespace, |
||
| 129 | array $config, |
||
| 130 | $client, |
||
| 131 | array $indexSettings, |
||
| 132 | $metadataCollector, |
||
| 133 | $converter |
||
| 134 | ) { |
||
| 135 | if (!class_exists($classNamespace)) { |
||
| 136 | throw new \InvalidArgumentException( |
||
| 137 | sprintf('Cannot create repository for non-existing class "%s".', $classNamespace) |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | $this->classNamespace = $classNamespace; |
||
| 142 | $this->config = $config; |
||
| 143 | $this->client = $client; |
||
| 144 | $this->indexSettings = $indexSettings; |
||
| 145 | $this->metadataCollector = $metadataCollector; |
||
| 146 | $this->converter = $converter; |
||
| 147 | $this->type = $this->resolveType($classNamespace); |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function getType() |
||
| 154 | { |
||
| 155 | return $this->type; |
||
| 156 | } |
||
| 157 | |||
| 158 | public function createSearch() :Search |
||
| 159 | { |
||
| 160 | return new Search(); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Returns a single document by ID. Returns NULL if document was not found. |
||
| 165 | * |
||
| 166 | * @param string $id Document ID to find |
||
| 167 | * @param string $routing Custom routing for the document |
||
| 168 | * |
||
| 169 | * @return object |
||
| 170 | */ |
||
| 171 | public function find($id, $routing = null) |
||
| 172 | { |
||
| 173 | $params = [ |
||
| 174 | 'index' => $this->getIndexName(), |
||
| 175 | 'type' => $this->getType(), |
||
| 176 | 'id' => $id, |
||
| 177 | ]; |
||
| 178 | |||
| 179 | if ($routing) { |
||
|
|
|||
| 180 | $params['routing'] = $routing; |
||
| 181 | } |
||
| 182 | |||
| 183 | $result = $this->getClient()->get($params); |
||
| 184 | |||
| 185 | return $this->getConverter()->convertToDocument($result, $this); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Finds documents by a set of criteria. |
||
| 190 | * |
||
| 191 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
| 192 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
| 193 | * @param int|null $limit Example: 5. |
||
| 194 | * @param int|null $offset Example: 30. |
||
| 195 | */ |
||
| 196 | public function findBy( |
||
| 197 | array $criteria, |
||
| 198 | array $orderBy = [], |
||
| 199 | $limit = null, |
||
| 200 | $offset = null |
||
| 201 | ):DocumentIterator { |
||
| 202 | $search = $this->createSearch(); |
||
| 203 | |||
| 204 | if ($limit !== null) { |
||
| 205 | $search->setSize($limit); |
||
| 206 | } |
||
| 207 | if ($offset !== null) { |
||
| 208 | $search->setFrom($offset); |
||
| 209 | } |
||
| 210 | |||
| 211 | foreach ($criteria as $field => $value) { |
||
| 212 | if (preg_match('/^!(.+)$/', $field)) { |
||
| 213 | $boolType = BoolQuery::MUST_NOT; |
||
| 214 | $field = preg_replace('/^!/', '', $field); |
||
| 215 | } else { |
||
| 216 | $boolType = BoolQuery::MUST; |
||
| 217 | } |
||
| 218 | |||
| 219 | $search->addQuery( |
||
| 220 | new QueryStringQuery(is_array($value) ? implode(' OR ', $value) : $value, ['default_field' => $field]), |
||
| 221 | $boolType |
||
| 222 | ); |
||
| 223 | } |
||
| 224 | |||
| 225 | foreach ($orderBy as $field => $direction) { |
||
| 226 | $search->addSort(new FieldSort($field, $direction)); |
||
| 227 | } |
||
| 228 | |||
| 229 | return $this->findDocuments($search); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Finds a single document by a set of criteria. |
||
| 234 | * |
||
| 235 | * @param array $criteria Example: ['group' => ['best', 'worst'], 'job' => 'medic']. |
||
| 236 | * @param array|null $orderBy Example: ['name' => 'ASC', 'surname' => 'DESC']. |
||
| 237 | */ |
||
| 238 | public function findOneBy(array $criteria, array $orderBy = []) |
||
| 239 | { |
||
| 240 | return $this->findBy($criteria, $orderBy, 1, null)->current(); |
||
| 241 | } |
||
| 242 | |||
| 243 | View Code Duplication | public function findDocuments(Search $search) :DocumentIterator |
|
| 244 | { |
||
| 245 | $results = $this->executeSearch($search); |
||
| 246 | |||
| 247 | return new DocumentIterator( |
||
| 248 | $results, |
||
| 249 | $this->getManager(), |
||
| 250 | $this->getScrollConfiguration($results, $search->getScroll()) |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | |||
| 254 | View Code Duplication | public function findArray(Search $search):ArrayIterator |
|
| 255 | { |
||
| 256 | $results = $this->executeSearch($search); |
||
| 257 | |||
| 258 | return new ArrayIterator( |
||
| 259 | $results, |
||
| 260 | $this->getManager(), |
||
| 261 | $this->getScrollConfiguration($results, $search->getScroll()) |
||
| 262 | ); |
||
| 263 | } |
||
| 264 | |||
| 265 | View Code Duplication | public function findRaw(Search $search):RawIterator |
|
| 266 | { |
||
| 267 | $results = $this->executeSearch($search); |
||
| 268 | |||
| 269 | return new RawIterator( |
||
| 270 | $results, |
||
| 271 | $this->getManager(), |
||
| 272 | $this->getScrollConfiguration($results, $search->getScroll()) |
||
| 273 | ); |
||
| 274 | } |
||
| 275 | |||
| 276 | private function executeSearch(Search $search):array |
||
| 280 | |||
| 281 | public function getScrollConfiguration(array $raw, string $scrollDuration):array |
||
| 282 | { |
||
| 283 | $scrollConfig = []; |
||
| 284 | if (isset($raw['_scroll_id'])) { |
||
| 285 | $scrollConfig['_scroll_id'] = $raw['_scroll_id']; |
||
| 286 | $scrollConfig['duration'] = $scrollDuration; |
||
| 287 | } |
||
| 288 | |||
| 289 | return $scrollConfig; |
||
| 291 | |||
| 292 | public function count(Search $search, array $params = []):int |
||
| 308 | |||
| 309 | public function remove($id, $routing = null):array |
||
| 325 | |||
| 326 | public function update(string $id, array $fields = [], string $script = null, array $params = []):array |
||
| 347 | |||
| 348 | private function resolveType($className):string |
||
| 352 | |||
| 353 | public function getClassNamespace():string |
||
| 357 | |||
| 358 | public function getClient():Client |
||
| 362 | |||
| 363 | public function getConfig():array |
||
| 367 | |||
| 368 | public function getMetadataCollector():MetadataCollector |
||
| 372 | |||
| 373 | public function getConverter():Converter |
||
| 377 | |||
| 378 | public function getCommitMode():string |
||
| 382 | |||
| 383 | public function setCommitMode(string $commitMode) |
||
| 391 | |||
| 392 | public function getBulkCommitSize():int |
||
| 396 | |||
| 397 | public function setBulkCommitSize(int $bulkCommitSize) |
||
| 401 | |||
| 402 | public function search(array $types, array $query, array $queryStringParams = []):array |
||
| 428 | |||
| 429 | public function msearch(array $body):array |
||
| 439 | |||
| 440 | public function persist($document):array |
||
| 447 | |||
| 448 | public function flush(array $params = []):array |
||
| 452 | |||
| 453 | public function refresh(array $params = []):array |
||
| 457 | |||
| 458 | public function commit(array $params = []):array |
||
| 507 | |||
| 508 | public function bulk(string $operation, string $type, array $query):array |
||
| 555 | |||
| 556 | public function setBulkParams(array $params) |
||
| 560 | |||
| 561 | public function createIndex(bool $noMapping = false):array |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Drops elasticsearch index. |
||
| 572 | */ |
||
| 573 | public function dropIndex():array |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Tries to drop and create fresh elasticsearch index. |
||
| 580 | */ |
||
| 581 | public function dropAndCreateIndex(bool $noMapping = false):array |
||
| 593 | |||
| 594 | public function indexExists() |
||
| 598 | |||
| 599 | public function getIndexName():string |
||
| 603 | |||
| 604 | public function setIndexName(string $name) |
||
| 608 | |||
| 609 | public function getIndexMappings():array |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Returns Elasticsearch version number. |
||
| 616 | */ |
||
| 617 | public function getVersionNumber():string |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Clears elasticsearch index cache. |
||
| 624 | */ |
||
| 625 | public function clearIndexCache():array |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Fetches next scroll batch of results. |
||
| 632 | */ |
||
| 633 | public function scroll(string $scrollId, string $scrollDuration = '5m'):array |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Clears scroll. |
||
| 642 | */ |
||
| 643 | public function clearScroll(string $scrollId):array |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Calls "Get Settings API" in Elasticsearch and will return you the currently configured settings. |
||
| 650 | * |
||
| 651 | * return array |
||
| 652 | */ |
||
| 653 | public function getSettings():array |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Gets Elasticsearch aliases information. |
||
| 660 | * @param $params |
||
| 661 | * |
||
| 662 | * @return array |
||
| 663 | */ |
||
| 664 | public function getAliases($params = []):array |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Resolves type name by class name. |
||
| 671 | */ |
||
| 672 | private function resolveTypeName($classNamespace):string |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Starts and stops an event in the stopwatch |
||
| 679 | * |
||
| 680 | * @param string $action only 'start' and 'stop' |
||
| 681 | * @param string $name name of the event |
||
| 682 | */ |
||
| 683 | private function stopwatch($action, $name) |
||
| 689 | } |
||
| 690 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: