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 |
||
| 27 | class MongoCursor extends AbstractCursor implements Iterator |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var bool |
||
| 31 | */ |
||
| 32 | public static $slaveOkay = false; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | public static $timeout = 30000; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $optionNames = [ |
||
| 43 | 'allowPartialResults', |
||
| 44 | 'batchSize', |
||
| 45 | 'cursorType', |
||
| 46 | 'limit', |
||
| 47 | 'maxTimeMS', |
||
| 48 | 'modifiers', |
||
| 49 | 'noCursorTimeout', |
||
| 50 | 'projection', |
||
| 51 | 'readPreference', |
||
| 52 | 'skip', |
||
| 53 | 'sort', |
||
| 54 | ]; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $projection; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $query; |
||
| 65 | |||
| 66 | protected $allowPartialResults; |
||
| 67 | protected $awaitData; |
||
| 68 | protected $flags = 0; |
||
| 69 | protected $hint; |
||
| 70 | protected $limit; |
||
| 71 | protected $maxTimeMS; |
||
| 72 | protected $noCursorTimeout; |
||
| 73 | protected $options = []; |
||
| 74 | protected $skip; |
||
| 75 | protected $snapshot; |
||
| 76 | protected $sort; |
||
| 77 | protected $tailable; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Create a new cursor |
||
| 81 | * @link http://www.php.net/manual/en/mongocursor.construct.php |
||
| 82 | * @param MongoClient $connection Database connection. |
||
| 83 | * @param string $ns Full name of database and collection. |
||
| 84 | * @param array $query Database query. |
||
| 85 | * @param array $fields Fields to return. |
||
| 86 | */ |
||
| 87 | public function __construct(MongoClient $connection, $ns, array $query = array(), array $fields = array()) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Adds a top-level key/value pair to a query |
||
| 97 | * @link http://www.php.net/manual/en/mongocursor.addoption.php |
||
| 98 | * @param string $key Fieldname to add. |
||
| 99 | * @param mixed $value Value to add. |
||
| 100 | * @throws MongoCursorException |
||
| 101 | * @return MongoCursor Returns this cursor |
||
| 102 | */ |
||
| 103 | public function addOption($key, $value) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * (PECL mongo >= 1.2.11)<br/> |
||
| 113 | * Sets whether this cursor will wait for a while for a tailable cursor to return more data |
||
| 114 | * @param bool $wait [optional] <p>If the cursor should wait for more data to become available.</p> |
||
| 115 | * @return MongoCursor Returns this cursor. |
||
| 116 | */ |
||
| 117 | public function awaitData($wait = true) |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * Counts the number of results for this query |
||
| 128 | * @link http://www.php.net/manual/en/mongocursor.count.php |
||
| 129 | * @param bool $foundOnly Send cursor limit and skip information to the count function, if applicable. |
||
| 130 | * @return int The number of documents returned by this cursor's query. |
||
| 131 | */ |
||
| 132 | public function count($foundOnly = false) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Execute the query |
||
| 157 | * @link http://www.php.net/manual/en/mongocursor.doquery.php |
||
| 158 | * @throws MongoConnectionException if it cannot reach the database. |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | protected function doQuery() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Return an explanation of the query, often useful for optimization and debugging |
||
| 176 | * @link http://www.php.net/manual/en/mongocursor.explain.php |
||
| 177 | * @return array Returns an explanation of the query. |
||
| 178 | */ |
||
| 179 | public function explain() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Sets the fields for a query |
||
| 186 | * @link http://www.php.net/manual/en/mongocursor.fields.php |
||
| 187 | * @param array $f Fields to return (or not return). |
||
| 188 | * @throws MongoCursorException |
||
| 189 | * @return MongoCursor |
||
| 190 | */ |
||
| 191 | public function fields(array $f) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Advances the cursor to the next result, and returns that result |
||
| 201 | * @link http://www.php.net/manual/en/mongocursor.getnext.php |
||
| 202 | * @throws MongoConnectionException |
||
| 203 | * @throws MongoCursorTimeoutException |
||
| 204 | * @return array Returns the next object |
||
| 205 | */ |
||
| 206 | public function getNext() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Checks if there are any more elements in this cursor |
||
| 213 | * @link http://www.php.net/manual/en/mongocursor.hasnext.php |
||
| 214 | * @throws MongoConnectionException |
||
| 215 | * @throws MongoCursorTimeoutException |
||
| 216 | * @return bool Returns true if there is another element |
||
| 217 | */ |
||
| 218 | public function hasNext() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Gives the database a hint about the query |
||
| 226 | * @link http://www.php.net/manual/en/mongocursor.hint.php |
||
| 227 | * @param array|string $keyPattern Indexes to use for the query. |
||
| 228 | * @throws MongoCursorException |
||
| 229 | * @return MongoCursor Returns this cursor |
||
| 230 | */ |
||
| 231 | public function hint($keyPattern) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Sets whether this cursor will timeout |
||
| 241 | * @link http://www.php.net/manual/en/mongocursor.immortal.php |
||
| 242 | * @param bool $liveForever If the cursor should be immortal. |
||
| 243 | * @throws MongoCursorException |
||
| 244 | * @return MongoCursor Returns this cursor |
||
| 245 | */ |
||
| 246 | public function immortal($liveForever = true) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Limits the number of results returned |
||
| 256 | * @link http://www.php.net/manual/en/mongocursor.limit.php |
||
| 257 | * @param int $num The number of results to return. |
||
| 258 | * @throws MongoCursorException |
||
| 259 | * @return MongoCursor Returns this cursor |
||
| 260 | */ |
||
| 261 | public function limit($num) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param int $ms |
||
| 271 | * @return $this |
||
| 272 | * @throws MongoCursorException |
||
| 273 | */ |
||
| 274 | public function maxTimeMS($ms) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @link http://www.php.net/manual/en/mongocursor.partial.php |
||
| 284 | * @param bool $okay [optional] <p>If receiving partial results is okay.</p> |
||
| 285 | * @return MongoCursor Returns this cursor. |
||
| 286 | */ |
||
| 287 | public function partial($okay = true) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Clears the cursor |
||
| 296 | * @link http://www.php.net/manual/en/mongocursor.reset.php |
||
| 297 | * @return void |
||
| 298 | */ |
||
| 299 | public function reset() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @link http://www.php.net/manual/en/mongocursor.setflag.php |
||
| 306 | * @param int $flag |
||
| 307 | * @param bool $set |
||
| 308 | * @return MongoCursor |
||
| 309 | */ |
||
| 310 | public function setFlag($flag, $set = true) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Skips a number of results |
||
| 317 | * @link http://www.php.net/manual/en/mongocursor.skip.php |
||
| 318 | * @param int $num The number of results to skip. |
||
| 319 | * @throws MongoCursorException |
||
| 320 | * @return MongoCursor Returns this cursor |
||
| 321 | */ |
||
| 322 | public function skip($num) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Sets whether this query can be done on a slave |
||
| 332 | * This method will override the static class variable slaveOkay. |
||
| 333 | * @link http://www.php.net/manual/en/mongocursor.slaveOkay.php |
||
| 334 | * @param boolean $okay If it is okay to query the slave. |
||
| 335 | * @throws MongoCursorException |
||
| 336 | * @return MongoCursor Returns this cursor |
||
| 337 | */ |
||
| 338 | public function slaveOkay($okay = true) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Use snapshot mode for the query |
||
| 349 | * @link http://www.php.net/manual/en/mongocursor.snapshot.php |
||
| 350 | * @throws MongoCursorException |
||
| 351 | * @return MongoCursor Returns this cursor |
||
| 352 | */ |
||
| 353 | public function snapshot() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Sorts the results by given fields |
||
| 363 | * @link http://www.php.net/manual/en/mongocursor.sort.php |
||
| 364 | * @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 |
||
| 365 | * @throws MongoCursorException |
||
| 366 | * @return MongoCursor Returns the same cursor that this method was called on |
||
| 367 | */ |
||
| 368 | public function sort(array $fields) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Sets whether this cursor will be left open after fetching the last results |
||
| 378 | * @link http://www.php.net/manual/en/mongocursor.tailable.php |
||
| 379 | * @param bool $tail If the cursor should be tailable. |
||
| 380 | * @return MongoCursor Returns this cursor |
||
| 381 | */ |
||
| 382 | public function tailable($tail = true) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @return int|null |
||
| 392 | */ |
||
| 393 | protected function convertCursorType() |
||
| 401 | |||
| 402 | protected function convertModifiers() |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @return Cursor |
||
| 419 | */ |
||
| 420 | protected function ensureCursor() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param \Traversable $traversable |
||
| 431 | * @return \Generator |
||
| 432 | */ |
||
| 433 | protected function wrapTraversable(\Traversable $traversable) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return array |
||
| 445 | */ |
||
| 446 | protected function getCursorInfo() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return array |
||
| 461 | */ |
||
| 462 | public function __sleep() |
||
| 482 | } |
||
| 483 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..