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 Query 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 Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Query implements \IteratorAggregate |
||
| 40 | { |
||
| 41 | const TYPE_FIND = 1; |
||
| 42 | const TYPE_FIND_AND_UPDATE = 2; |
||
| 43 | const TYPE_FIND_AND_REMOVE = 3; |
||
| 44 | const TYPE_INSERT = 4; |
||
| 45 | const TYPE_UPDATE = 5; |
||
| 46 | const TYPE_REMOVE = 6; |
||
| 47 | const TYPE_GROUP = 7; |
||
| 48 | const TYPE_MAP_REDUCE = 8; |
||
| 49 | const TYPE_DISTINCT = 9; |
||
| 50 | const TYPE_COUNT = 11; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @deprecated 1.1 Will be removed for 2.0 |
||
| 54 | */ |
||
| 55 | const TYPE_GEO_LOCATION = 10; |
||
| 56 | |||
| 57 | const HINT_REFRESH = 1; |
||
| 58 | /** @deprecated */ |
||
| 59 | const HINT_SLAVE_OKAY = 2; |
||
| 60 | const HINT_READ_PREFERENCE = 3; |
||
| 61 | const HINT_READ_ONLY = 5; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The DocumentManager instance. |
||
| 65 | * |
||
| 66 | * @var DocumentManager |
||
| 67 | */ |
||
| 68 | private $dm; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The ClassMetadata instance. |
||
| 72 | * |
||
| 73 | * @var ClassMetadata |
||
| 74 | */ |
||
| 75 | private $class; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Whether to hydrate results as document class instances. |
||
| 79 | * |
||
| 80 | * @var boolean |
||
| 81 | */ |
||
| 82 | private $hydrate = true; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Array of primer Closure instances. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private $primers = array(); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Hints for UnitOfWork behavior. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private $unitOfWorkHints = array(); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The Collection instance. |
||
| 100 | * |
||
| 101 | * @var Collection |
||
| 102 | */ |
||
| 103 | protected $collection; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Query structure generated by the Builder class. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | private $query; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var Iterator |
||
| 114 | */ |
||
| 115 | private $iterator; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Query options |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private $options; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Constructor. |
||
| 126 | * |
||
| 127 | * Please note that $requireIndexes was deprecated in 1.2 and will be removed in 2.0 |
||
| 128 | * |
||
| 129 | * @param DocumentManager $dm |
||
| 130 | * @param ClassMetadata $class |
||
| 131 | * @param Collection $collection |
||
| 132 | * @param array $query |
||
| 133 | * @param array $options |
||
| 134 | * @param boolean $hydrate |
||
| 135 | * @param boolean $refresh |
||
| 136 | * @param array $primers |
||
| 137 | * @param boolean $readOnly |
||
| 138 | */ |
||
| 139 | 159 | public function __construct(DocumentManager $dm, ClassMetadata $class, Collection $collection, array $query = array(), array $options = array(), $hydrate = true, $refresh = false, array $primers = array(), $readOnly = false) |
|
| 187 | |||
| 188 | 64 | public function __clone() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Return an array of information about the query structure for debugging. |
||
| 195 | * |
||
| 196 | * The $name parameter may be used to return a specific key from the |
||
| 197 | * internal $query array property. If omitted, the entire array will be |
||
| 198 | * returned. |
||
| 199 | * |
||
| 200 | * @param string $name |
||
| 201 | * @return mixed |
||
| 202 | */ |
||
| 203 | 26 | public function debug($name = null) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Execute the query and returns the results. |
||
| 210 | * |
||
| 211 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 212 | * @return Iterator|int|string|array |
||
| 213 | */ |
||
| 214 | 119 | public function execute() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Gets the ClassMetadata instance. |
||
| 252 | * |
||
| 253 | * @return ClassMetadata $class |
||
| 254 | */ |
||
| 255 | public function getClass() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Gets the DocumentManager instance. |
||
| 262 | * |
||
| 263 | * @return DocumentManager $dm |
||
| 264 | */ |
||
| 265 | public function getDocumentManager() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Execute the query and return its result, which must be an Iterator. |
||
| 272 | * |
||
| 273 | * If the query type is not expected to return an Iterator, |
||
| 274 | * BadMethodCallException will be thrown before executing the query. |
||
| 275 | * Otherwise, the query will be executed and UnexpectedValueException will |
||
| 276 | * be thrown if {@link Query::execute()} does not return an Iterator. |
||
| 277 | * |
||
| 278 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 279 | * @return Iterator |
||
| 280 | * @throws \BadMethodCallException if the query type would not return an Iterator |
||
| 281 | * @throws \UnexpectedValueException if the query did not return an Iterator |
||
| 282 | */ |
||
| 283 | 84 | public function getIterator() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Return the query structure. |
||
| 305 | * |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | 14 | public function getQuery() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Execute the query and return the first result. |
||
| 315 | * |
||
| 316 | * @return array|object|null |
||
| 317 | */ |
||
| 318 | 64 | public function getSingleResult() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Return the query type. |
||
| 327 | * |
||
| 328 | * @return integer |
||
| 329 | */ |
||
| 330 | public function getType() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Sets whether or not to hydrate the documents to objects. |
||
| 337 | * |
||
| 338 | * @param boolean $hydrate |
||
| 339 | */ |
||
| 340 | public function setHydrate($hydrate) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Set whether documents should be registered in UnitOfWork. If document would |
||
| 347 | * already be managed it will be left intact and new instance returned. |
||
| 348 | * |
||
| 349 | * This option has no effect if hydration is disabled. |
||
| 350 | * |
||
| 351 | * @param boolean $readOnly |
||
| 352 | */ |
||
| 353 | 158 | public function setReadOnly($readOnly) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Set whether to refresh hydrated documents that are already in the |
||
| 360 | * identity map. |
||
| 361 | * |
||
| 362 | * This option has no effect if hydration is disabled. |
||
| 363 | * |
||
| 364 | * @param boolean $refresh |
||
| 365 | */ |
||
| 366 | 158 | public function setRefresh($refresh) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Execute the query and return its results as an array. |
||
| 373 | * |
||
| 374 | * @see IteratorAggregate::toArray() |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | 11 | public function toArray() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Returns an array containing the specified keys and their values from the |
||
| 384 | * query array, provided they exist and are not null. |
||
| 385 | * |
||
| 386 | * @param string $key,... One or more option keys to be read |
||
| 387 | * @return array |
||
| 388 | */ |
||
| 389 | 118 | private function getQueryOptions(/* $key, ... */) |
|
| 396 | |||
| 397 | 105 | private function makeIterator(Cursor $cursor): Iterator |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Returns an array with its keys renamed based on the translation map. |
||
| 415 | * |
||
| 416 | * @param array $options Query options |
||
| 417 | * @return array $rename Translation map (from => to) for renaming keys |
||
| 418 | */ |
||
| 419 | 110 | private function renameQueryOptions(array $options, array $rename) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Execute the query and return its result. |
||
| 436 | * |
||
| 437 | * The return value will vary based on the query type. Commands with results |
||
| 438 | * (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other |
||
| 439 | * commands and operations may return a status array or a boolean, depending |
||
| 440 | * on the driver's write concern. Queries and some mapReduce commands will |
||
| 441 | * return an Iterator. |
||
| 442 | * |
||
| 443 | * @return Iterator|string|int|array |
||
| 444 | */ |
||
| 445 | 119 | public function runQuery() |
|
| 522 | } |
||
| 523 |
This class constant has been deprecated.