Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | final class Query implements IteratorAggregate |
||
| 45 | { |
||
| 46 | public const TYPE_FIND = 1; |
||
| 47 | public const TYPE_FIND_AND_UPDATE = 2; |
||
| 48 | public const TYPE_FIND_AND_REMOVE = 3; |
||
| 49 | public const TYPE_INSERT = 4; |
||
| 50 | public const TYPE_UPDATE = 5; |
||
| 51 | public const TYPE_REMOVE = 6; |
||
| 52 | public const TYPE_DISTINCT = 9; |
||
| 53 | public const TYPE_COUNT = 11; |
||
| 54 | |||
| 55 | public const HINT_REFRESH = 1; |
||
| 56 | // 2 was used for HINT_SLAVE_OKAY, which was removed in 2.0 |
||
| 57 | public const HINT_READ_PREFERENCE = 3; |
||
| 58 | public const HINT_READ_ONLY = 5; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The DocumentManager instance. |
||
| 62 | * |
||
| 63 | * @var DocumentManager |
||
| 64 | */ |
||
| 65 | private $dm; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The ClassMetadata instance. |
||
| 69 | * |
||
| 70 | * @var ClassMetadata |
||
| 71 | */ |
||
| 72 | private $class; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Whether to hydrate results as document class instances. |
||
| 76 | * |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | private $hydrate = true; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Array of primer Closure instances. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private $primers = []; |
||
| 87 | |||
| 88 | /** @var bool */ |
||
| 89 | private $rewindable = true; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Hints for UnitOfWork behavior. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private $unitOfWorkHints = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The Collection instance. |
||
| 100 | * |
||
| 101 | * @var Collection |
||
| 102 | */ |
||
| 103 | protected $collection; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Query structure generated by the Builder class. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | private $query; |
||
| 111 | |||
| 112 | /** @var Iterator|null */ |
||
| 113 | private $iterator; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Query options |
||
| 117 | * |
||
| 118 | * @var array |
||
| 119 | */ |
||
| 120 | private $options; |
||
| 121 | |||
| 122 | 168 | public function __construct(DocumentManager $dm, ClassMetadata $class, Collection $collection, array $query = [], array $options = [], bool $hydrate = true, bool $refresh = false, array $primers = [], bool $readOnly = false, bool $rewindable = true) |
|
| 159 | |||
| 160 | 64 | public function __clone() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Return an array of information about the query structure for debugging. |
||
| 167 | * |
||
| 168 | * The $name parameter may be used to return a specific key from the |
||
| 169 | * internal $query array property. If omitted, the entire array will be |
||
| 170 | * returned. |
||
| 171 | */ |
||
| 172 | 27 | public function debug(?string $name = null) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Execute the query and returns the results. |
||
| 179 | * |
||
| 180 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
| 181 | * |
||
| 182 | * @throws MongoDBException |
||
| 183 | */ |
||
| 184 | 127 | public function execute() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Gets the ClassMetadata instance. |
||
| 217 | */ |
||
| 218 | public function getClass() : ClassMetadata |
||
| 222 | |||
| 223 | public function getDocumentManager() : DocumentManager |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Execute the query and return its result, which must be an Iterator. |
||
| 230 | * |
||
| 231 | * If the query type is not expected to return an Iterator, |
||
| 232 | * BadMethodCallException will be thrown before executing the query. |
||
| 233 | * Otherwise, the query will be executed and UnexpectedValueException will |
||
| 234 | * be thrown if {@link Query::execute()} does not return an Iterator. |
||
| 235 | * |
||
| 236 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 237 | * |
||
| 238 | * @throws BadMethodCallException If the query type would not return an Iterator. |
||
| 239 | * @throws UnexpectedValueException If the query did not return an Iterator. |
||
| 240 | * @throws MongoDBException |
||
| 241 | */ |
||
| 242 | 84 | public function getIterator() : Iterator |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Return the query structure. |
||
| 266 | */ |
||
| 267 | 14 | public function getQuery() : array |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Execute the query and return the first result. |
||
| 274 | * |
||
| 275 | * @return array|object|null |
||
| 276 | */ |
||
| 277 | 64 | public function getSingleResult() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Return the query type. |
||
| 287 | */ |
||
| 288 | public function getType() : int |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Sets whether or not to hydrate the documents to objects. |
||
| 295 | */ |
||
| 296 | 1 | public function setHydrate(bool $hydrate) : void |
|
| 297 | { |
||
| 298 | 1 | $this->hydrate = $hydrate; |
|
| 299 | 1 | } |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Set whether documents should be registered in UnitOfWork. If document would |
||
| 303 | * already be managed it will be left intact and new instance returned. |
||
| 304 | * |
||
| 305 | * This option has no effect if hydration is disabled. |
||
| 306 | */ |
||
| 307 | 167 | public function setReadOnly(bool $readOnly) : void |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Set whether to refresh hydrated documents that are already in the |
||
| 314 | * identity map. |
||
| 315 | * |
||
| 316 | * This option has no effect if hydration is disabled. |
||
| 317 | */ |
||
| 318 | 167 | public function setRefresh(bool $refresh) : void |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Set to enable wrapping of resulting Iterator with CachingIterator |
||
| 325 | */ |
||
| 326 | 167 | public function setRewindable(bool $rewindable = true) : void |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Execute the query and return its results as an array. |
||
| 333 | * |
||
| 334 | * @see IteratorAggregate::toArray() |
||
| 335 | */ |
||
| 336 | 11 | public function toArray() : array |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Returns an array containing the specified keys and their values from the |
||
| 343 | * query array, provided they exist and are not null. |
||
| 344 | */ |
||
| 345 | 125 | private function getQueryOptions(string ...$keys) : array |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Decorate the cursor with caching, hydration, and priming behavior. |
||
| 357 | * |
||
| 358 | * Note: while this method could strictly take a MongoDB\Driver\Cursor, we |
||
| 359 | * accept Traversable for testing purposes since Cursor cannot be mocked. |
||
| 360 | * HydratingIterator, CachingIterator, and BaseIterator expect a Traversable |
||
| 361 | * so this should not have any adverse effects. |
||
| 362 | */ |
||
| 363 | 111 | private function makeIterator(Traversable $cursor) : Iterator |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Returns an array with its keys renamed based on the translation map. |
||
| 381 | * |
||
| 382 | * @return array $rename Translation map (from => to) for renaming keys |
||
| 383 | */ |
||
| 384 | 116 | private function renameQueryOptions(array $options, array $rename) : array |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Execute the query and return its result. |
||
| 408 | * |
||
| 409 | * The return value will vary based on the query type. Commands with results |
||
| 410 | * (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other |
||
| 411 | * commands and operations may return a status array or a boolean, depending |
||
| 412 | * on the driver's write concern. Queries and some mapReduce commands will |
||
| 413 | * return an Iterator. |
||
| 414 | * |
||
| 415 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
| 416 | */ |
||
| 417 | 127 | private function runQuery() |
|
| 503 | |||
| 504 | 19 | private function isFirstKeyUpdateOperator() : bool |
|
| 511 | } |
||
| 512 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: