Complex classes like AbstractCursor 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 AbstractCursor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | abstract class AbstractCursor |
||
25 | { |
||
26 | /** |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $batchSize; |
||
30 | |||
31 | /** |
||
32 | * @var Collection |
||
33 | */ |
||
34 | protected $collection; |
||
35 | |||
36 | /** |
||
37 | * @var \MongoClient |
||
38 | */ |
||
39 | protected $connection; |
||
40 | |||
41 | /** |
||
42 | * @var Cursor |
||
43 | */ |
||
44 | protected $cursor; |
||
45 | |||
46 | /** |
||
47 | * @var \MongoDB\Database |
||
48 | */ |
||
49 | protected $db; |
||
50 | |||
51 | /** |
||
52 | * @var \IteratorIterator |
||
53 | */ |
||
54 | protected $iterator; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $ns; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $optionNames = [ |
||
65 | 'batchSize', |
||
66 | 'readPreference', |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $readPreference = []; |
||
73 | |||
74 | /** |
||
75 | * @return Cursor |
||
76 | */ |
||
77 | abstract protected function ensureCursor(); |
||
78 | |||
79 | /** |
||
80 | * @return array |
||
81 | */ |
||
82 | abstract protected function getCursorInfo(); |
||
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 | */ |
||
90 | 25 | public function __construct(\MongoClient $connection, $ns) |
|
105 | |||
106 | /** |
||
107 | * Returns the current element |
||
108 | * @link http://www.php.net/manual/en/mongocursor.current.php |
||
109 | * @return array |
||
110 | */ |
||
111 | 6 | public function current() |
|
120 | |||
121 | /** |
||
122 | * Returns the current result's _id |
||
123 | * @link http://www.php.net/manual/en/mongocursor.key.php |
||
124 | * @return string The current result's _id as a string. |
||
125 | */ |
||
126 | 6 | public function key() |
|
130 | |||
131 | /** |
||
132 | * Advances the cursor to the next result |
||
133 | * @link http://www.php.net/manual/en/mongocursor.next.php |
||
134 | * @throws \MongoConnectionException |
||
135 | * @throws \MongoCursorTimeoutException |
||
136 | * @return void |
||
137 | */ |
||
138 | 6 | public function next() |
|
142 | |||
143 | /** |
||
144 | * Returns the cursor to the beginning of the result set |
||
145 | * @throws \MongoConnectionException |
||
146 | * @throws \MongoCursorTimeoutException |
||
147 | * @return void |
||
148 | */ |
||
149 | 23 | public function rewind() |
|
155 | |||
156 | /** |
||
157 | * Checks if the cursor is reading a valid result. |
||
158 | * @link http://www.php.net/manual/en/mongocursor.valid.php |
||
159 | * @return boolean If the current result is not null. |
||
160 | */ |
||
161 | 22 | public function valid() |
|
165 | |||
166 | /** |
||
167 | * Limits the number of elements returned in one batch. |
||
168 | * |
||
169 | * @link http://docs.php.net/manual/en/mongocursor.batchsize.php |
||
170 | * @param int $batchSize The number of results to return per batch |
||
171 | * @return $this Returns this cursor. |
||
172 | */ |
||
173 | 1 | public function batchSize($batchSize) |
|
179 | |||
180 | /** |
||
181 | * Checks if there are documents that have not been sent yet from the database for this cursor |
||
182 | * @link http://www.php.net/manual/en/mongocursor.dead.php |
||
183 | * @return boolean Returns if there are more results that have not been sent to the client, yet. |
||
184 | */ |
||
185 | public function dead() |
||
189 | |||
190 | /** |
||
191 | * Get the read preference for this query |
||
192 | * @link http://www.php.net/manual/en/mongocursor.getreadpreference.php |
||
193 | * @return array |
||
194 | */ |
||
195 | public function getReadPreference() |
||
199 | |||
200 | /** |
||
201 | * @return array |
||
202 | */ |
||
203 | 2 | public function info() |
|
204 | { |
||
205 | 2 | return $this->getCursorInfo() + $this->getIterationInfo(); |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * @link http://www.php.net/manual/en/mongocursor.setreadpreference.php |
||
210 | * @param string $readPreference |
||
211 | * @param array $tags |
||
212 | * @return $this Returns this cursor. |
||
213 | */ |
||
214 | 2 | public function setReadPreference($readPreference, $tags = null) |
|
240 | |||
241 | /** |
||
242 | * Sets a client-side timeout for this query |
||
243 | * @link http://www.php.net/manual/en/mongocursor.timeout.php |
||
244 | * @param int $ms The number of milliseconds for the cursor to wait for a response. By default, the cursor will wait forever. |
||
245 | * @return $this Returns this cursor |
||
246 | */ |
||
247 | public function timeout($ms) |
||
251 | |||
252 | /** |
||
253 | * Applies all options set on the cursor, overwriting any options that have already been set |
||
254 | * |
||
255 | * @param array $optionNames Array of option names to be applied (will be read from properties) |
||
256 | * @return array |
||
257 | */ |
||
258 | 24 | protected function getOptions($optionNames = null) |
|
279 | |||
280 | /** |
||
281 | * @return ReadPreference|null |
||
282 | */ |
||
283 | 23 | protected function convertReadPreference() |
|
311 | |||
312 | /** |
||
313 | * @return \IteratorIterator |
||
314 | */ |
||
315 | 23 | protected function ensureIterator() |
|
323 | |||
324 | /** |
||
325 | * @throws \MongoCursorException |
||
326 | */ |
||
327 | 14 | protected function errorIfOpened() |
|
335 | |||
336 | /** |
||
337 | * @return array |
||
338 | */ |
||
339 | 2 | protected function getIterationInfo() |
|
340 | { |
||
341 | $iterationInfo = [ |
||
342 | 2 | 'started_iterating' => $this->cursor !== null, |
|
343 | 2 | ]; |
|
344 | |||
345 | 2 | if ($this->cursor !== null) { |
|
346 | 2 | switch ($this->cursor->getServer()->getType()) { |
|
347 | 2 | case \MongoDB\Driver\Server::TYPE_RS_ARBITER: |
|
348 | $typeString = 'ARBITER'; |
||
349 | break; |
||
350 | 2 | case \MongoDB\Driver\Server::TYPE_MONGOS: |
|
351 | $typeString = 'MONGOS'; |
||
352 | break; |
||
353 | 2 | case \MongoDB\Driver\Server::TYPE_RS_PRIMARY: |
|
354 | $typeString = 'PRIMARY'; |
||
355 | break; |
||
356 | 2 | case \MongoDB\Driver\Server::TYPE_RS_SECONDARY: |
|
357 | $typeString = 'SECONDARY'; |
||
358 | break; |
||
359 | 2 | default: |
|
360 | 2 | $typeString = 'STANDALONE'; |
|
361 | 2 | } |
|
362 | |||
363 | $iterationInfo += [ |
||
364 | 2 | 'id' => (string) $this->cursor->getId(), |
|
365 | 2 | 'at' => null, // @todo Complete info for cursor that is iterating |
|
366 | 2 | 'numReturned' => null, // @todo Complete info for cursor that is iterating |
|
367 | 2 | 'server' => null, // @todo Complete info for cursor that is iterating |
|
368 | 2 | 'host' => $this->cursor->getServer()->getHost(), |
|
369 | 2 | 'port' => $this->cursor->getServer()->getPort(), |
|
370 | 2 | 'connection_type_desc' => $typeString, |
|
371 | ]; |
||
372 | 2 | } |
|
373 | |||
374 | 2 | return $iterationInfo; |
|
375 | } |
||
376 | |||
377 | /** |
||
378 | * @throws \Exception |
||
379 | */ |
||
380 | protected function notImplemented() |
||
384 | |||
385 | /** |
||
386 | * Clears the cursor |
||
387 | * |
||
388 | * This is generic but implemented as protected since it's only exposed in MongoCursor |
||
389 | */ |
||
390 | 23 | protected function reset() |
|
395 | } |
||
396 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.