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 | 19 | public function __construct(\MongoClient $connection, $ns) |
|
101 | |||
102 | /** |
||
103 | * Returns the current element |
||
104 | * @link http://www.php.net/manual/en/mongocursor.current.php |
||
105 | * @return array |
||
106 | */ |
||
107 | 1 | public function current() |
|
116 | |||
117 | /** |
||
118 | * Returns the current result's _id |
||
119 | * @link http://www.php.net/manual/en/mongocursor.key.php |
||
120 | * @return string The current result's _id as a string. |
||
121 | */ |
||
122 | 1 | public function key() |
|
126 | |||
127 | /** |
||
128 | * Advances the cursor to the next result |
||
129 | * @link http://www.php.net/manual/en/mongocursor.next.php |
||
130 | * @throws \MongoConnectionException |
||
131 | * @throws \MongoCursorTimeoutException |
||
132 | * @return void |
||
133 | */ |
||
134 | 1 | public function next() |
|
138 | |||
139 | /** |
||
140 | * Returns the cursor to the beginning of the result set |
||
141 | * @throws \MongoConnectionException |
||
142 | * @throws \MongoCursorTimeoutException |
||
143 | * @return void |
||
144 | */ |
||
145 | 17 | public function rewind() |
|
151 | |||
152 | /** |
||
153 | * Checks if the cursor is reading a valid result. |
||
154 | * @link http://www.php.net/manual/en/mongocursor.valid.php |
||
155 | * @return boolean If the current result is not null. |
||
156 | */ |
||
157 | 17 | public function valid() |
|
161 | |||
162 | /** |
||
163 | * Limits the number of elements returned in one batch. |
||
164 | * |
||
165 | * @link http://docs.php.net/manual/en/mongocursor.batchsize.php |
||
166 | * @param int $batchSize The number of results to return per batch |
||
167 | * @return $this Returns this cursor. |
||
168 | */ |
||
169 | 1 | public function batchSize($batchSize) |
|
175 | |||
176 | /** |
||
177 | * Checks if there are documents that have not been sent yet from the database for this cursor |
||
178 | * @link http://www.php.net/manual/en/mongocursor.dead.php |
||
179 | * @return boolean Returns if there are more results that have not been sent to the client, yet. |
||
180 | */ |
||
181 | public function dead() |
||
185 | |||
186 | /** |
||
187 | * Get the read preference for this query |
||
188 | * @link http://www.php.net/manual/en/mongocursor.getreadpreference.php |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getReadPreference() |
||
195 | |||
196 | /** |
||
197 | * @return array |
||
198 | */ |
||
199 | public function info() |
||
203 | |||
204 | /** |
||
205 | * @link http://www.php.net/manual/en/mongocursor.setreadpreference.php |
||
206 | * @param string $readPreference |
||
207 | * @param array $tags |
||
208 | * @return $this Returns this cursor. |
||
209 | */ |
||
210 | 2 | public function setReadPreference($readPreference, array $tags = []) |
|
236 | |||
237 | /** |
||
238 | * Sets a client-side timeout for this query |
||
239 | * @link http://www.php.net/manual/en/mongocursor.timeout.php |
||
240 | * @param int $ms The number of milliseconds for the cursor to wait for a response. By default, the cursor will wait forever. |
||
241 | * @return $this Returns this cursor |
||
242 | */ |
||
243 | public function timeout($ms) |
||
247 | |||
248 | /** |
||
249 | * Applies all options set on the cursor, overwriting any options that have already been set |
||
250 | * |
||
251 | * @param array $optionNames Array of option names to be applied (will be read from properties) |
||
252 | * @return array |
||
253 | */ |
||
254 | 18 | protected function getOptions($optionNames = null) |
|
275 | |||
276 | /** |
||
277 | * @return ReadPreference|null |
||
278 | */ |
||
279 | 17 | protected function convertReadPreference() |
|
307 | |||
308 | /** |
||
309 | * @return \IteratorIterator |
||
310 | */ |
||
311 | 17 | protected function ensureIterator() |
|
319 | |||
320 | /** |
||
321 | * @throws \MongoCursorException |
||
322 | */ |
||
323 | 13 | protected function errorIfOpened() |
|
331 | |||
332 | /** |
||
333 | * @return array |
||
334 | */ |
||
335 | protected function getIterationInfo() |
||
372 | |||
373 | /** |
||
374 | * @throws \Exception |
||
375 | */ |
||
376 | protected function notImplemented() |
||
380 | |||
381 | /** |
||
382 | * Clears the cursor |
||
383 | * |
||
384 | * This is generic but implemented as protected since it's only exposed in MongoCursor |
||
385 | */ |
||
386 | 17 | protected function reset() |
|
391 | } |
||
392 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.