Complex classes like MongoCursor 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 MongoCursor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class MongoCursor implements Iterator |
||
1 ignored issue
–
show
|
|||
26 | { |
||
27 | /** |
||
28 | * @link http://php.net/manual/en/class.mongocursor.php#mongocursor.props.slaveokay |
||
29 | * @static |
||
30 | * @var bool $slaveOkay |
||
31 | */ |
||
32 | public static $slaveOkay = FALSE; |
||
33 | |||
34 | /** |
||
35 | * @var int <p> |
||
36 | * Set timeout in milliseconds for all database responses. Use |
||
37 | * <em>-1</em> to wait forever. Can be overridden with |
||
38 | * {link http://php.net/manual/en/mongocursor.timeout.php MongoCursor::timeout()}. This does not cause the |
||
39 | * MongoDB server to cancel the operation; it only instructs the driver to |
||
40 | * stop waiting for a response and throw a |
||
41 | * {@link http://php.net/manual/en/class.mongocursortimeoutexception.php MongoCursorTimeoutException} after a set time. |
||
42 | * </p> |
||
43 | */ |
||
44 | static $timeout = 30000; |
||
45 | |||
46 | /** |
||
47 | * @var MongoClient |
||
48 | */ |
||
49 | private $connection; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $ns; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $query; |
||
60 | |||
61 | /** |
||
62 | * @var |
||
63 | */ |
||
64 | private $filter; |
||
65 | |||
66 | /** |
||
67 | * @var Collection |
||
68 | */ |
||
69 | private $collection; |
||
70 | |||
71 | /** |
||
72 | * @var Cursor |
||
73 | */ |
||
74 | private $cursor; |
||
75 | |||
76 | /** |
||
77 | * @var IteratorIterator |
||
78 | */ |
||
79 | private $iterator; |
||
80 | |||
81 | private $awaitData; |
||
82 | private $batchSize; |
||
83 | private $limit; |
||
84 | private $maxTimeMS; |
||
85 | private $noCursorTimeout; |
||
86 | private $options = []; |
||
87 | private $projection; |
||
88 | private $skip; |
||
89 | private $sort; |
||
90 | private $tailable; |
||
91 | |||
92 | /** |
||
93 | * Create a new cursor |
||
94 | * @link http://www.php.net/manual/en/mongocursor.construct.php |
||
95 | * @param MongoClient $connection Database connection. |
||
96 | * @param string $ns Full name of database and collection. |
||
97 | * @param array $query Database query. |
||
98 | * @param array $fields Fields to return. |
||
99 | * @return MongoCursor Returns the new cursor |
||
100 | */ |
||
101 | public function __construct(MongoClient $connection, $ns, array $query = array(), array $fields = array()) |
||
113 | |||
114 | /** |
||
115 | * Adds a top-level key/value pair to a query |
||
116 | * @link http://www.php.net/manual/en/mongocursor.addoption.php |
||
117 | * @param string $key Fieldname to add. |
||
118 | * @param mixed $value Value to add. |
||
119 | * @throws MongoCursorException |
||
120 | * @return MongoCursor Returns this cursor |
||
121 | */ |
||
122 | public function addOption($key, $value) |
||
129 | |||
130 | /** |
||
131 | * (PECL mongo >= 1.2.11)<br/> |
||
132 | * Sets whether this cursor will wait for a while for a tailable cursor to return more data |
||
133 | * @param bool $wait [optional] <p>If the cursor should wait for more data to become available.</p> |
||
134 | * @return MongoCursor Returns this cursor. |
||
135 | */ |
||
136 | public function awaitData($wait = true) |
||
143 | |||
144 | /** |
||
145 | * Limits the number of elements returned in one batch. |
||
146 | * |
||
147 | * @link http://docs.php.net/manual/en/mongocursor.batchsize.php |
||
148 | * @param int $batchSize The number of results to return per batch |
||
149 | * @return MongoCursor Returns this cursor. |
||
150 | */ |
||
151 | public function batchSize($batchSize) |
||
158 | |||
159 | /** |
||
160 | * Counts the number of results for this query |
||
161 | * @link http://www.php.net/manual/en/mongocursor.count.php |
||
162 | * @param bool $foundOnly Send cursor limit and skip information to the count function, if applicable. |
||
163 | * @return int The number of documents returned by this cursor's query. |
||
164 | */ |
||
165 | public function count($foundOnly = false) |
||
175 | |||
176 | /** |
||
177 | * Returns the current element |
||
178 | * @link http://www.php.net/manual/en/mongocursor.current.php |
||
179 | * @return array |
||
180 | */ |
||
181 | public function current() |
||
190 | |||
191 | /** |
||
192 | * Checks if there are documents that have not been sent yet from the database for this cursor |
||
193 | * @link http://www.php.net/manual/en/mongocursor.dead.php |
||
194 | * @return boolean Returns if there are more results that have not been sent to the client, yet. |
||
195 | */ |
||
196 | public function dead() |
||
200 | |||
201 | /** |
||
202 | * Execute the query |
||
203 | * @link http://www.php.net/manual/en/mongocursor.doquery.php |
||
204 | * @throws MongoConnectionException if it cannot reach the database. |
||
205 | * @return void |
||
206 | */ |
||
207 | protected function doQuery() |
||
211 | |||
212 | /** |
||
213 | * Return an explanation of the query, often useful for optimization and debugging |
||
214 | * @link http://www.php.net/manual/en/mongocursor.explain.php |
||
215 | * @return array Returns an explanation of the query. |
||
216 | */ |
||
217 | public function explain() |
||
221 | |||
222 | /** |
||
223 | * Sets the fields for a query |
||
224 | * @link http://www.php.net/manual/en/mongocursor.fields.php |
||
225 | * @param array $f Fields to return (or not return). |
||
226 | * @throws MongoCursorException |
||
227 | * @return MongoCursor |
||
228 | */ |
||
229 | public function fields(array $f) |
||
236 | |||
237 | /** |
||
238 | * Return the next object to which this cursor points, and advance the cursor |
||
239 | * @link http://www.php.net/manual/en/mongocursor.getnext.php |
||
240 | * @throws MongoConnectionException |
||
241 | * @throws MongoCursorTimeoutException |
||
242 | * @return array Returns the next object |
||
243 | */ |
||
244 | public function getNext() |
||
250 | |||
251 | /** |
||
252 | * (PECL mongo >= 1.3.3)<br/> |
||
253 | * @link http://www.php.net/manual/en/mongocursor.getreadpreference.php |
||
254 | * @return array This function returns an array describing the read preference. The array contains the values <em>type</em> for the string |
||
255 | * read preference mode (corresponding to the {@link http://www.php.net/manual/en/class.mongoclient.php MongoClient} constants), and <em>tagsets</em> containing a list of all tag set criteria. If no tag sets were specified, <em>tagsets</em> will not be present in the array. |
||
256 | */ |
||
257 | public function getReadPreference() |
||
261 | |||
262 | /** |
||
263 | * Checks if there are any more elements in this cursor |
||
264 | * @link http://www.php.net/manual/en/mongocursor.hasnext.php |
||
265 | * @throws MongoConnectionException |
||
266 | * @throws MongoCursorTimeoutException |
||
267 | * @return bool Returns true if there is another element |
||
268 | */ |
||
269 | public function hasNext() |
||
273 | |||
274 | /** |
||
275 | * Gives the database a hint about the query |
||
276 | * @link http://www.php.net/manual/en/mongocursor.hint.php |
||
277 | * @param array $key_pattern Indexes to use for the query. |
||
278 | * @throws MongoCursorException |
||
279 | * @return MongoCursor Returns this cursor |
||
280 | */ |
||
281 | public function hint(array $key_pattern) |
||
285 | |||
286 | /** |
||
287 | * Sets whether this cursor will timeout |
||
288 | * @link http://www.php.net/manual/en/mongocursor.immortal.php |
||
289 | * @param bool $liveForever If the cursor should be immortal. |
||
290 | * @throws MongoCursorException |
||
291 | * @return MongoCursor Returns this cursor |
||
292 | */ |
||
293 | public function immortal($liveForever = true) |
||
300 | |||
301 | /** |
||
302 | * Gets the query, fields, limit, and skip for this cursor |
||
303 | * @link http://www.php.net/manual/en/mongocursor.info.php |
||
304 | * @return array The query, fields, limit, and skip for this cursor as an associative array. |
||
305 | */ |
||
306 | public function info() |
||
310 | |||
311 | /** |
||
312 | * Returns the current result's _id |
||
313 | * @link http://www.php.net/manual/en/mongocursor.key.php |
||
314 | * @return string The current result's _id as a string. |
||
315 | */ |
||
316 | public function key() |
||
320 | |||
321 | /** |
||
322 | * Limits the number of results returned |
||
323 | * @link http://www.php.net/manual/en/mongocursor.limit.php |
||
324 | * @param int $num The number of results to return. |
||
325 | * @throws MongoCursorException |
||
326 | * @return MongoCursor Returns this cursor |
||
327 | */ |
||
328 | public function limit($num) |
||
335 | |||
336 | /** |
||
337 | * @param int $ms |
||
338 | * @return $this |
||
339 | * @throws MongoCursorException |
||
340 | */ |
||
341 | public function maxTimeMS($ms) |
||
348 | |||
349 | /** |
||
350 | * Advances the cursor to the next result |
||
351 | * @link http://www.php.net/manual/en/mongocursor.next.php |
||
352 | * @throws MongoConnectionException |
||
353 | * @throws MongoCursorTimeoutException |
||
354 | * @return void |
||
355 | */ |
||
356 | public function next() |
||
360 | |||
361 | /** |
||
362 | * (PECL mongo >= 1.2.0)<br/> |
||
363 | * @link http://www.php.net/manual/en/mongocursor.partial.php |
||
364 | * @param bool $okay [optional] <p>If receiving partial results is okay.</p> |
||
365 | * @return MongoCursor Returns this cursor. |
||
366 | */ |
||
367 | public function partial($okay = true) |
||
371 | |||
372 | /** |
||
373 | * Clears the cursor |
||
374 | * @link http://www.php.net/manual/en/mongocursor.reset.php |
||
375 | * @return void |
||
376 | */ |
||
377 | public function reset() |
||
382 | |||
383 | /** |
||
384 | * Returns the cursor to the beginning of the result set |
||
385 | * @throws MongoConnectionException |
||
386 | * @throws MongoCursorTimeoutException |
||
387 | * @return void |
||
388 | */ |
||
389 | public function rewind() |
||
395 | |||
396 | /** |
||
397 | * (PECL mongo >= 1.2.1)<br/> |
||
398 | * @link http://www.php.net/manual/en/mongocursor.setflag.php |
||
399 | * @param int $flag <p> |
||
400 | * Which flag to set. You can not set flag 6 (EXHAUST) as the driver does |
||
401 | * not know how to handle them. You will get a warning if you try to use |
||
402 | * this flag. For available flags, please refer to the wire protocol |
||
403 | * {@link http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol#MongoWireProtocol-OPQUERY documentation}. |
||
404 | * </p> |
||
405 | * @param bool $set [optional] <p>Whether the flag should be set (<b>TRUE</b>) or unset (<b>FALSE</b>).</p> |
||
406 | * @return MongoCursor |
||
407 | */ |
||
408 | public function setFlag($flag, $set = true ) |
||
412 | |||
413 | /** |
||
414 | * (PECL mongo >= 1.3.3)<br/> |
||
415 | * @link http://www.php.net/manual/en/mongocursor.setreadpreference.php |
||
416 | * @param string $read_preference <p>The read preference mode: MongoClient::RP_PRIMARY, MongoClient::RP_PRIMARY_PREFERRED, MongoClient::RP_SECONDARY, MongoClient::RP_SECONDARY_PREFERRED, or MongoClient::RP_NEAREST.</p> |
||
417 | * @param array $tags [optional] <p>The read preference mode: MongoClient::RP_PRIMARY, MongoClient::RP_PRIMARY_PREFERRED, MongoClient::RP_SECONDARY, MongoClient::RP_SECONDARY_PREFERRED, or MongoClient::RP_NEAREST.</p> |
||
418 | * @return MongoCursor Returns this cursor. |
||
419 | */ |
||
420 | public function setReadPreference($read_preference, array $tags) |
||
424 | |||
425 | /** |
||
426 | * Skips a number of results |
||
427 | * @link http://www.php.net/manual/en/mongocursor.skip.php |
||
428 | * @param int $num The number of results to skip. |
||
429 | * @throws MongoCursorException |
||
430 | * @return MongoCursor Returns this cursor |
||
431 | */ |
||
432 | public function skip($num) |
||
439 | |||
440 | /** |
||
441 | * Sets whether this query can be done on a slave |
||
442 | * This method will override the static class variable slaveOkay. |
||
443 | * @link http://www.php.net/manual/en/mongocursor.slaveOkay.php |
||
444 | * @param boolean $okay If it is okay to query the slave. |
||
445 | * @throws MongoCursorException |
||
446 | * @return MongoCursor Returns this cursor |
||
447 | */ |
||
448 | public function slaveOkay($okay = true) |
||
452 | |||
453 | /** |
||
454 | * Use snapshot mode for the query |
||
455 | * @link http://www.php.net/manual/en/mongocursor.snapshot.php |
||
456 | * @throws MongoCursorException |
||
457 | * @return MongoCursor Returns this cursor |
||
458 | */ |
||
459 | public function snapshot() |
||
463 | |||
464 | /** |
||
465 | * Sorts the results by given fields |
||
466 | * @link http://www.php.net/manual/en/mongocursor.sort.php |
||
467 | * @param array $fields An array of fields by which to sort. Each element in the array has as key the field name, and as value either 1 for ascending sort, or -1 for descending sort |
||
468 | * @throws MongoCursorException |
||
469 | * @return MongoCursor Returns the same cursor that this method was called on |
||
470 | */ |
||
471 | public function sort(array $fields) |
||
478 | |||
479 | /** |
||
480 | * Sets whether this cursor will be left open after fetching the last results |
||
481 | * @link http://www.php.net/manual/en/mongocursor.tailable.php |
||
482 | * @param bool $tail If the cursor should be tailable. |
||
483 | * @return MongoCursor Returns this cursor |
||
484 | */ |
||
485 | public function tailable($tail = true) |
||
492 | |||
493 | /** |
||
494 | * Sets a client-side timeout for this query |
||
495 | * @link http://www.php.net/manual/en/mongocursor.timeout.php |
||
496 | * @param int $ms The number of milliseconds for the cursor to wait for a response. By default, the cursor will wait forever. |
||
497 | * @throws MongoCursorTimeoutException |
||
498 | * @return MongoCursor Returns this cursor |
||
499 | */ |
||
500 | public function timeout($ms) |
||
504 | |||
505 | /** |
||
506 | * Checks if the cursor is reading a valid result. |
||
507 | * @link http://www.php.net/manual/en/mongocursor.valid.php |
||
508 | * @return boolean If the current result is not null. |
||
509 | */ |
||
510 | public function valid() |
||
514 | |||
515 | private function applyOptions($options, $optionNames) |
||
527 | |||
528 | /** |
||
529 | * @return Cursor |
||
530 | */ |
||
531 | private function ensureCursor() |
||
545 | |||
546 | private function errorIfOpened() |
||
554 | |||
555 | /** |
||
556 | * @return IteratorIterator |
||
557 | */ |
||
558 | private function ensureIterator() |
||
566 | |||
567 | protected function notImplemented() |
||
571 | } |
||
572 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.