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 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 |
||
32 | class MongoCursor extends AbstractCursor implements Iterator, Countable, MongoCursorInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | public static $slaveOkay = false; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | public static $timeout = 30000; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $optionNames = [ |
||
48 | 'allowPartialResults', |
||
49 | 'batchSize', |
||
50 | 'cursorType', |
||
51 | 'limit', |
||
52 | 'maxTimeMS', |
||
53 | 'modifiers', |
||
54 | 'noCursorTimeout', |
||
55 | 'projection', |
||
56 | 'readPreference', |
||
57 | 'skip', |
||
58 | 'sort', |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $projection; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $query; |
||
70 | |||
71 | protected $allowPartialResults; |
||
72 | protected $awaitData; |
||
73 | protected $flags = 0; |
||
74 | protected $hint; |
||
75 | protected $limit; |
||
76 | protected $maxTimeMS; |
||
77 | protected $noCursorTimeout; |
||
78 | protected $options = []; |
||
79 | protected $skip; |
||
80 | protected $snapshot; |
||
81 | protected $sort; |
||
82 | protected $tailable; |
||
83 | |||
84 | /** |
||
85 | * Create a new cursor |
||
86 | * @link http://www.php.net/manual/en/mongocursor.construct.php |
||
87 | * @param MongoClient $connection Database connection. |
||
88 | * @param string $ns Full name of database and collection. |
||
89 | * @param array $query Database query. |
||
90 | * @param array $fields Fields to return. |
||
91 | */ |
||
92 | public function __construct(MongoClient $connection, $ns, array $query = array(), array $fields = array()) |
||
99 | |||
100 | /** |
||
101 | * Adds a top-level key/value pair to a query |
||
102 | * @link http://www.php.net/manual/en/mongocursor.addoption.php |
||
103 | * @param string $key Fieldname to add. |
||
104 | * @param mixed $value Value to add. |
||
105 | * @throws MongoCursorException |
||
106 | * @return MongoCursor Returns this cursor |
||
107 | */ |
||
108 | public function addOption($key, $value) |
||
115 | |||
116 | /** |
||
117 | * (PECL mongo >= 1.2.11)<br/> |
||
118 | * Sets whether this cursor will wait for a while for a tailable cursor to return more data |
||
119 | * @param bool $wait [optional] <p>If the cursor should wait for more data to become available.</p> |
||
120 | * @return MongoCursor Returns this cursor. |
||
121 | */ |
||
122 | public function awaitData($wait = true) |
||
129 | |||
130 | |||
131 | /** |
||
132 | * Counts the number of results for this query |
||
133 | * @link http://www.php.net/manual/en/mongocursor.count.php |
||
134 | * @param bool $foundOnly Send cursor limit and skip information to the count function, if applicable. |
||
135 | * @return int The number of documents returned by this cursor's query. |
||
136 | */ |
||
137 | public function count($foundOnly = false) |
||
155 | |||
156 | /** |
||
157 | * Execute the query |
||
158 | * @link http://www.php.net/manual/en/mongocursor.doquery.php |
||
159 | * @throws MongoConnectionException if it cannot reach the database. |
||
160 | * @return void |
||
161 | */ |
||
162 | protected function doQuery() |
||
174 | |||
175 | /** |
||
176 | * Return an explanation of the query, often useful for optimization and debugging |
||
177 | * @link http://www.php.net/manual/en/mongocursor.explain.php |
||
178 | * @return array Returns an explanation of the query. |
||
179 | */ |
||
180 | public function explain() |
||
208 | |||
209 | /** |
||
210 | * Sets the fields for a query |
||
211 | * @link http://www.php.net/manual/en/mongocursor.fields.php |
||
212 | * @param array $f Fields to return (or not return). |
||
213 | * @throws MongoCursorException |
||
214 | * @return MongoCursor |
||
215 | */ |
||
216 | public function fields(array $f) |
||
223 | |||
224 | /** |
||
225 | * Advances the cursor to the next result, and returns that result |
||
226 | * @link http://www.php.net/manual/en/mongocursor.getnext.php |
||
227 | * @throws MongoConnectionException |
||
228 | * @throws MongoCursorTimeoutException |
||
229 | * @return array Returns the next object |
||
230 | */ |
||
231 | public function getNext() |
||
235 | |||
236 | /** |
||
237 | * Checks if there are any more elements in this cursor |
||
238 | * @link http://www.php.net/manual/en/mongocursor.hasnext.php |
||
239 | * @throws MongoConnectionException |
||
240 | * @throws MongoCursorTimeoutException |
||
241 | * @return bool Returns true if there is another element |
||
242 | */ |
||
243 | public function hasNext() |
||
257 | |||
258 | /** |
||
259 | * Gives the database a hint about the query |
||
260 | * @link http://www.php.net/manual/en/mongocursor.hint.php |
||
261 | * @param array|string $keyPattern Indexes to use for the query. |
||
262 | * @throws MongoCursorException |
||
263 | * @return MongoCursor Returns this cursor |
||
264 | */ |
||
265 | public function hint($keyPattern) |
||
272 | |||
273 | /** |
||
274 | * Sets whether this cursor will timeout |
||
275 | * @link http://www.php.net/manual/en/mongocursor.immortal.php |
||
276 | * @param bool $liveForever If the cursor should be immortal. |
||
277 | * @throws MongoCursorException |
||
278 | * @return MongoCursor Returns this cursor |
||
279 | */ |
||
280 | public function immortal($liveForever = true) |
||
287 | |||
288 | /** |
||
289 | * Limits the number of results returned |
||
290 | * @link http://www.php.net/manual/en/mongocursor.limit.php |
||
291 | * @param int $num The number of results to return. |
||
292 | * @throws MongoCursorException |
||
293 | * @return MongoCursor Returns this cursor |
||
294 | */ |
||
295 | public function limit($num) |
||
302 | |||
303 | /** |
||
304 | * @param int $ms |
||
305 | * @return $this |
||
306 | * @throws MongoCursorException |
||
307 | */ |
||
308 | public function maxTimeMS($ms) |
||
315 | |||
316 | /** |
||
317 | * @link http://www.php.net/manual/en/mongocursor.partial.php |
||
318 | * @param bool $okay [optional] <p>If receiving partial results is okay.</p> |
||
319 | * @return MongoCursor Returns this cursor. |
||
320 | */ |
||
321 | public function partial($okay = true) |
||
327 | |||
328 | /** |
||
329 | * Clears the cursor |
||
330 | * @link http://www.php.net/manual/en/mongocursor.reset.php |
||
331 | * @return void |
||
332 | */ |
||
333 | public function reset() |
||
337 | |||
338 | /** |
||
339 | * @link http://www.php.net/manual/en/mongocursor.setflag.php |
||
340 | * @param int $flag |
||
341 | * @param bool $set |
||
342 | * @return MongoCursor |
||
343 | */ |
||
344 | public function setFlag($flag, $set = true) |
||
348 | |||
349 | /** |
||
350 | * Skips a number of results |
||
351 | * @link http://www.php.net/manual/en/mongocursor.skip.php |
||
352 | * @param int $num The number of results to skip. |
||
353 | * @throws MongoCursorException |
||
354 | * @return MongoCursor Returns this cursor |
||
355 | */ |
||
356 | public function skip($num) |
||
363 | |||
364 | /** |
||
365 | * Sets whether this query can be done on a slave |
||
366 | * This method will override the static class variable slaveOkay. |
||
367 | * @link http://www.php.net/manual/en/mongocursor.slaveOkay.php |
||
368 | * @param boolean $okay If it is okay to query the slave. |
||
369 | * @throws MongoCursorException |
||
370 | * @return MongoCursor Returns this cursor |
||
371 | */ |
||
372 | public function slaveOkay($okay = true) |
||
380 | |||
381 | /** |
||
382 | * Use snapshot mode for the query |
||
383 | * @link http://www.php.net/manual/en/mongocursor.snapshot.php |
||
384 | * @throws MongoCursorException |
||
385 | * @return MongoCursor Returns this cursor |
||
386 | */ |
||
387 | public function snapshot() |
||
394 | |||
395 | /** |
||
396 | * Sorts the results by given fields |
||
397 | * @link http://www.php.net/manual/en/mongocursor.sort.php |
||
398 | * @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 |
||
399 | * @throws MongoCursorException |
||
400 | * @return MongoCursor Returns the same cursor that this method was called on |
||
401 | */ |
||
402 | public function sort(array $fields) |
||
409 | |||
410 | /** |
||
411 | * Sets whether this cursor will be left open after fetching the last results |
||
412 | * @link http://www.php.net/manual/en/mongocursor.tailable.php |
||
413 | * @param bool $tail If the cursor should be tailable. |
||
414 | * @return MongoCursor Returns this cursor |
||
415 | */ |
||
416 | public function tailable($tail = true) |
||
423 | |||
424 | /** |
||
425 | * @return int|null |
||
426 | */ |
||
427 | protected function convertCursorType() |
||
435 | |||
436 | /** |
||
437 | * @return array |
||
438 | */ |
||
439 | protected function convertModifiers() |
||
453 | |||
454 | /** |
||
455 | * @return array |
||
456 | */ |
||
457 | protected function convertProjection() |
||
461 | |||
462 | /** |
||
463 | * @return Cursor |
||
464 | */ |
||
465 | protected function ensureCursor() |
||
473 | |||
474 | /** |
||
475 | * @param \Traversable $traversable |
||
476 | * @return CursorIterator |
||
477 | */ |
||
478 | protected function wrapTraversable(\Traversable $traversable) |
||
482 | |||
483 | /** |
||
484 | * @return array |
||
485 | */ |
||
486 | protected function getCursorInfo() |
||
498 | |||
499 | /** |
||
500 | * @return array |
||
501 | */ |
||
502 | public function __sleep() |
||
522 | } |
||
523 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.