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 Cursor 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 Cursor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Cursor implements \Iterator, \Countable |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * |
||
| 21 | * @var \Sokil\Mongo\Client |
||
| 22 | */ |
||
| 23 | private $client; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * |
||
| 27 | * @var \Sokil\Mongo\Collection |
||
| 28 | */ |
||
| 29 | private $collection; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $fields = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * |
||
| 39 | * @var \MongoCursor |
||
| 40 | */ |
||
| 41 | private $cursor; |
||
| 42 | /** |
||
| 43 | * |
||
| 44 | * @var \Sokil\Mongo\Expression |
||
| 45 | */ |
||
| 46 | private $expression; |
||
| 47 | |||
| 48 | private $skip = 0; |
||
| 49 | |||
| 50 | private $limit = 0; |
||
| 51 | |||
| 52 | |||
| 53 | private $sort = array(); |
||
| 54 | |||
| 55 | private $readPreference = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Return result as array or as Document instance |
||
| 59 | * @var boolean |
||
| 60 | */ |
||
| 61 | private $resultAsArray = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Cursor options |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $options = array( |
||
| 68 | 'expressionClass' => '\Sokil\Mongo\Expression', |
||
| 69 | /** |
||
| 70 | * @link http://docs.mongodb.org/manual/reference/method/cursor.batchSize/ |
||
| 71 | * @var int number of documents to return in each batch of the response from the MongoDB instance |
||
| 72 | */ |
||
| 73 | 'batchSize' => null, |
||
| 74 | // client timeout |
||
| 75 | 'clientTimeout' => null, |
||
| 76 | // Specifies a cumulative time limit in milliseconds to be allowed by the server for processing operations on the cursor. |
||
| 77 | 'serverTimeout' => null, |
||
| 78 | ); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Use document pool to create Document object from array |
||
| 82 | * @var bool |
||
| 83 | */ |
||
| 84 | private $isDocumentPoolUsed = true; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Index hinting |
||
| 88 | * @param \Sokil\Mongo\Collection $collection |
||
| 89 | * @param array $options |
||
| 90 | */ |
||
| 91 | private $hint; |
||
| 92 | |||
| 93 | public function __construct(Collection $collection, array $options = null) |
||
| 106 | |||
| 107 | public function __call($name, $arguments) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get option |
||
| 115 | * |
||
| 116 | * @param string|int $name |
||
| 117 | * @return mixed |
||
| 118 | */ |
||
| 119 | public function getOption($name, $default = null) |
||
| 123 | |||
| 124 | public function asArray() |
||
| 129 | |||
| 130 | public function asObject() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Check if result returned as array |
||
| 138 | * |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public function isResultAsArray() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Return only specified fields |
||
| 148 | * |
||
| 149 | * @param array $fields |
||
| 150 | * @return \Sokil\Mongo\Cursor |
||
| 151 | */ |
||
| 152 | public function fields(array $fields) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Return all fields except specified |
||
| 163 | * |
||
| 164 | * @param array $fields |
||
| 165 | * @return \Sokil\Mongo\Cursor |
||
| 166 | */ |
||
| 167 | public function skipFields(array $fields) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Append field to accept list |
||
| 178 | * |
||
| 179 | * @param string $field field name |
||
| 180 | * @return \Sokil\Mongo\Cursor |
||
| 181 | */ |
||
| 182 | public function field($field) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Append field to skip list |
||
| 193 | * |
||
| 194 | * @param string $field field name |
||
| 195 | * @return Cursor |
||
| 196 | */ |
||
| 197 | public function skipField($field) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Paginate list of sub-documents |
||
| 208 | * |
||
| 209 | * @param string $field |
||
| 210 | * @param integer $limit |
||
| 211 | * @param integer $skip |
||
| 212 | * @return \Sokil\Mongo\Cursor |
||
| 213 | * @throws Exception |
||
| 214 | */ |
||
| 215 | public function slice($field, $limit, $skip = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Merge expression |
||
| 234 | * @param \Sokil\Mongo\Expression $expression |
||
| 235 | * @return \Sokil\Mongo\Cursor |
||
| 236 | */ |
||
| 237 | public function query(Expression $expression) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Helper to create new expression |
||
| 245 | * |
||
| 246 | * @return \Sokil\Mongo\Expression |
||
| 247 | */ |
||
| 248 | public function expression() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Filter by list of \MongoId |
||
| 255 | * |
||
| 256 | * @param array $idList list of ids |
||
| 257 | * @return \Sokil\Mongo\Cursor |
||
| 258 | */ |
||
| 259 | public function byIdList(array $idList) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Filter by id |
||
| 267 | * |
||
| 268 | * @param string|\MongoId $id id of document |
||
| 269 | * @return \Sokil\Mongo\Cursor |
||
| 270 | */ |
||
| 271 | public function byId($id) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Skip defined number of documents |
||
| 288 | * |
||
| 289 | * @param int $skip number of documents to skip |
||
| 290 | * @return \Sokil\Mongo\Cursor |
||
| 291 | */ |
||
| 292 | public function skip($skip) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Limit result set to specified number of elements |
||
| 301 | * |
||
| 302 | * @param int $limit number of elements in result set |
||
| 303 | * @param int|null $offset number of elements to skip |
||
| 304 | * @return \Sokil\Mongo\Cursor |
||
| 305 | */ |
||
| 306 | public function limit($limit, $offset = null) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Specifies the number of documents to return in each batch of the response from the MongoDB instance. |
||
| 319 | * |
||
| 320 | * @param int $size number of documents |
||
| 321 | * @link http://docs.mongodb.org/manual/reference/method/cursor.batchSize/ |
||
| 322 | * @return \Sokil\Mongo\Cursor |
||
| 323 | */ |
||
| 324 | public function setBatchSize($size) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Instructs the driver to stop waiting for a response and throw a |
||
| 333 | * MongoCursorTimeoutException after a set time, |
||
| 334 | * A timeout can be set at any time and will affect subsequent queries on |
||
| 335 | * the cursor, including fetching more results from the database. |
||
| 336 | * @param type $ms |
||
| 337 | * @return \Sokil\Mongo\Cursor |
||
| 338 | */ |
||
| 339 | public function setClientTimeout($ms) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Server-side timeout for a query, |
||
| 348 | * Specifies a cumulative time limit in milliseconds to be allowed |
||
| 349 | * by the server for processing operations on the cursor. |
||
| 350 | * @param type $ms |
||
| 351 | * @return \Sokil\Mongo\Cursor |
||
| 352 | */ |
||
| 353 | public function setServerTimeout($ms) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Sort result by specified keys and directions |
||
| 362 | * |
||
| 363 | * An array of fields by which to sort. Each element in the array has as key the field name, and as value either |
||
| 364 | * 1 for ascending sort, or -1 for descending sort. Each result is first sorted on the first field in the array, |
||
| 365 | * then (if it exists) on the second field in the array, etc. This means that the order of the fields in the |
||
| 366 | * fields array is important. See also the examples section. |
||
| 367 | * |
||
| 368 | * @param array $sort |
||
| 369 | * @return \Sokil\Mongo\Cursor |
||
| 370 | */ |
||
| 371 | public function sort(array $sort) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * |
||
| 380 | * @return \MongoCursor |
||
| 381 | */ |
||
| 382 | private function getCursor() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Count documents in result without applying limit and offset |
||
| 445 | * @return int count |
||
| 446 | */ |
||
| 447 | public function count() |
||
| 453 | |||
| 454 | public function explain() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Count documents in result with applying limit and offset |
||
| 465 | * @return int count |
||
| 466 | */ |
||
| 467 | public function limitedCount() |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * Gte list of \MongoId of current search query |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | public function getIdList() |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Find one document which correspond to expression |
||
| 486 | * |
||
| 487 | * @return \Sokil\Mongo\Document|array|null |
||
| 488 | */ |
||
| 489 | public function findOne() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * |
||
| 519 | * @return array result of searching |
||
| 520 | */ |
||
| 521 | public function findAll() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Get random document |
||
| 528 | * @return |
||
| 529 | */ |
||
| 530 | public function findRandom() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get query builder's expression |
||
| 550 | * |
||
| 551 | * @return Expression |
||
| 552 | */ |
||
| 553 | public function getExpression() |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Get MongoDB query array |
||
| 560 | * |
||
| 561 | * @return array |
||
| 562 | */ |
||
| 563 | public function getMongoQuery() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Return the values from a single field in the result set of documents |
||
| 570 | * |
||
| 571 | * @param string $fieldName |
||
| 572 | * @return array |
||
| 573 | */ |
||
| 574 | public function pluck($fieldName) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Pluck by dot-notated field name |
||
| 595 | * |
||
| 596 | * @param string $fieldName field name |
||
| 597 | * @return array |
||
| 598 | */ |
||
| 599 | private function pluckDotNoteted($fieldName) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Get document instance and remove it from collection |
||
| 619 | * |
||
| 620 | * @return \Sokil\Mongo\Document |
||
| 621 | */ |
||
| 622 | public function findAndRemove() |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Find first document and update it |
||
| 643 | * |
||
| 644 | * @param Operator $operator operations with document to update |
||
| 645 | * @param bool $upsert if document not found - create |
||
| 646 | * @param bool $returnUpdated if true - return updated document |
||
| 647 | * |
||
| 648 | * @return null|Document |
||
| 649 | */ |
||
| 650 | public function findAndUpdate(Operator $operator, $upsert = false, $returnUpdated = true) |
||
| 671 | |||
| 672 | View Code Duplication | public function map($handler) |
|
| 682 | |||
| 683 | View Code Duplication | public function filter($handler) |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Get result set of documents. |
||
| 700 | * |
||
| 701 | * @return \Sokil\Mongo\ResultSet |
||
| 702 | */ |
||
| 703 | public function getResultSet() |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Get paginator |
||
| 710 | * |
||
| 711 | * @param int $page page number |
||
| 712 | * @param int $itemsOnPage number of items on page |
||
| 713 | * @return \Sokil\Mongo\Paginator |
||
| 714 | */ |
||
| 715 | public function paginate($page, $itemsOnPage = 30) |
||
| 724 | |||
| 725 | public function current() |
||
| 738 | |||
| 739 | public function key() |
||
| 743 | |||
| 744 | public function next() |
||
| 749 | |||
| 750 | public function rewind() |
||
| 755 | |||
| 756 | public function valid() |
||
| 760 | |||
| 761 | public function readPrimaryOnly() |
||
| 770 | |||
| 771 | public function readPrimaryPreferred(array $tags = null) |
||
| 780 | |||
| 781 | public function readSecondaryOnly(array $tags = null) |
||
| 790 | |||
| 791 | public function readSecondaryPreferred(array $tags = null) |
||
| 800 | |||
| 801 | public function readNearest(array $tags = null) |
||
| 810 | |||
| 811 | public function getReadPreference() |
||
| 819 | |||
| 820 | public function isDocumentPoolUsed() |
||
| 824 | |||
| 825 | public function useDocumentPool() |
||
| 830 | |||
| 831 | public function skipDocumentPool() |
||
| 836 | |||
| 837 | /** |
||
| 838 | * Specify index to use |
||
| 839 | * |
||
| 840 | * @link http://docs.mongodb.org/manual/reference/operator/meta/hint/ |
||
| 841 | * @param array|string $specification Specify the index either by the index name or by document |
||
| 842 | * @return \Sokil\Mongo\Cursor |
||
| 843 | */ |
||
| 844 | public function hint($specification) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Copy selected documents to another collection |
||
| 852 | * |
||
| 853 | * @param type $targetCollectionName |
||
| 854 | * @param type $targetDatabaseName Target database name. If not specified - use current |
||
| 855 | */ |
||
| 856 | public function copyToCollection($targetCollectionName, $targetDatabaseName = null) |
||
| 912 | |||
| 913 | /** |
||
| 914 | * Move selected documents to another collection. |
||
| 915 | * Dociuments will be removed from source collection only after |
||
| 916 | * copying them to target collection. |
||
| 917 | * |
||
| 918 | * @param type $targetCollectionName |
||
| 919 | * @param type $targetDatabaseName Target database name. If not specified - use current |
||
| 920 | */ |
||
| 921 | public function moveToCollection($targetCollectionName, $targetDatabaseName = null) |
||
| 929 | |||
| 930 | /** |
||
| 931 | * Used to get hash that uniquely identifies current query |
||
| 932 | */ |
||
| 933 | public function getHash() |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Get list of MongoId objects from array of strings, MongoId's and Document's |
||
| 964 | * |
||
| 965 | * @param array $list |
||
| 966 | * @return array list of \MongoId |
||
| 967 | */ |
||
| 968 | public static function mixedToMongoIdList(array $list) |
||
| 1007 | } |
||
| 1008 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.