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 | 164 | * @var array |
|
| 119 | */ |
||
| 120 | 164 | private $options; |
|
| 121 | |||
| 122 | 164 | 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 | public function __clone() |
||
| 161 | { |
||
| 162 | $this->iterator = null; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Return an array of information about the query structure for debugging. |
||
| 167 | 27 | * |
|
| 168 | * The $name parameter may be used to return a specific key from the |
||
| 169 | 27 | * internal $query array property. If omitted, the entire array will be |
|
| 170 | * returned. |
||
| 171 | */ |
||
| 172 | public function debug(?string $name = null) |
||
| 173 | { |
||
| 174 | return $name !== null ? $this->query[$name] : $this->query; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Execute the query and returns the results. |
||
| 179 | 123 | * |
|
| 180 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
| 181 | 123 | * |
|
| 182 | * @throws MongoDBException |
||
| 183 | 122 | */ |
|
| 184 | 9 | public function execute() |
|
| 185 | { |
||
| 186 | $results = $this->runQuery(); |
||
| 187 | 116 | ||
| 188 | if (! $this->hydrate) { |
||
| 189 | return $results; |
||
| 190 | } |
||
| 191 | |||
| 192 | 116 | $uow = $this->dm->getUnitOfWork(); |
|
| 193 | 116 | ||
| 194 | 116 | /* If a single document is returned from a findAndModify command and it |
|
| 195 | 5 | * includes the identifier field, attempt hydration. |
|
| 196 | */ |
||
| 197 | 5 | if (($this->query['type'] === self::TYPE_FIND_AND_UPDATE || |
|
| 198 | 1 | $this->query['type'] === self::TYPE_FIND_AND_REMOVE) && |
|
| 199 | is_array($results) && isset($results['_id'])) { |
||
| 200 | 1 | $results = $uow->getOrCreateDocument($this->class->name, $results, $this->unitOfWorkHints); |
|
| 201 | 1 | ||
| 202 | 1 | if (! empty($this->primers)) { |
|
| 203 | $referencePrimer = new ReferencePrimer($this->dm, $uow); |
||
| 204 | |||
| 205 | foreach ($this->primers as $fieldName => $primer) { |
||
| 206 | $primer = is_callable($primer) ? $primer : null; |
||
| 207 | 116 | $referencePrimer->primeReferences($this->class, [$results], $fieldName, $this->unitOfWorkHints, $primer); |
|
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | return $results; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Gets the ClassMetadata instance. |
||
| 217 | */ |
||
| 218 | public function getClass() : ClassMetadata |
||
| 222 | |||
| 223 | public function getDocumentManager() : DocumentManager |
||
| 224 | { |
||
| 225 | return $this->dm; |
||
| 226 | } |
||
| 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 | 83 | * |
|
| 238 | * @throws BadMethodCallException If the query type would not return an Iterator. |
||
| 239 | 83 | * @throws UnexpectedValueException If the query did not return an Iterator. |
|
| 240 | 83 | * @throws MongoDBException |
|
| 241 | 6 | */ |
|
| 242 | 77 | public function getIterator() : Iterator |
|
| 243 | { |
||
| 244 | switch ($this->query['type']) { |
||
| 245 | 6 | case self::TYPE_FIND: |
|
| 246 | case self::TYPE_DISTINCT: |
||
| 247 | break; |
||
| 248 | 77 | ||
| 249 | 77 | default: |
|
| 250 | 77 | throw new BadMethodCallException('Iterator would not be returned for query type: ' . $this->query['type']); |
|
| 251 | } |
||
| 252 | |||
| 253 | 77 | if ($this->iterator === null) { |
|
| 254 | $result = $this->execute(); |
||
| 255 | if (! $result instanceof Iterator) { |
||
| 256 | 77 | throw new UnexpectedValueException('Iterator was not returned for query type: ' . $this->query['type']); |
|
| 257 | } |
||
| 258 | $this->iterator = $result; |
||
| 259 | } |
||
| 260 | |||
| 261 | return $this->iterator; |
||
| 262 | 14 | } |
|
| 263 | |||
| 264 | 14 | /** |
|
| 265 | * Return the query structure. |
||
| 266 | */ |
||
| 267 | public function getQuery() : array |
||
| 268 | { |
||
| 269 | return $this->query; |
||
| 270 | } |
||
| 271 | |||
| 272 | 64 | /** |
|
| 273 | * Execute the query and return the first result. |
||
| 274 | 64 | * |
|
| 275 | 64 | * @return array|object|null |
|
| 276 | */ |
||
| 277 | 64 | public function getSingleResult() |
|
| 278 | { |
||
| 279 | $clonedQuery = clone $this; |
||
| 280 | $clonedQuery->query['limit'] = 1; |
||
| 281 | |||
| 282 | return $clonedQuery->getIterator()->current() ?: null; |
||
|
|
|||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return the query type. |
||
| 287 | */ |
||
| 288 | public function getType() : int |
||
| 289 | { |
||
| 290 | return $this->query['type']; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Sets whether or not to hydrate the documents to objects. |
||
| 295 | */ |
||
| 296 | public function setHydrate(bool $hydrate) : void |
||
| 297 | { |
||
| 298 | $this->hydrate = $hydrate; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | 163 | * 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 | 163 | * |
|
| 305 | 163 | * This option has no effect if hydration is disabled. |
|
| 306 | */ |
||
| 307 | public function setReadOnly(bool $readOnly) : void |
||
| 308 | { |
||
| 309 | $this->unitOfWorkHints[self::HINT_READ_ONLY] = $readOnly; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | 163 | * Set whether to refresh hydrated documents that are already in the |
|
| 314 | * identity map. |
||
| 315 | 163 | * |
|
| 316 | 163 | * This option has no effect if hydration is disabled. |
|
| 317 | */ |
||
| 318 | public function setRefresh(bool $refresh) : void |
||
| 319 | { |
||
| 320 | $this->unitOfWorkHints[self::HINT_REFRESH] = $refresh; |
||
| 321 | } |
||
| 322 | |||
| 323 | 11 | /** |
|
| 324 | * Set to enable wrapping of resulting Iterator with CachingIterator |
||
| 325 | 11 | */ |
|
| 326 | public function setRewindable(bool $rewindable = true) : void |
||
| 327 | { |
||
| 328 | $this->rewindable = $rewindable; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | 121 | * Execute the query and return its results as an array. |
|
| 333 | * |
||
| 334 | 121 | * @see IteratorAggregate::toArray() |
|
| 335 | 121 | */ |
|
| 336 | 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 | private function getQueryOptions(string ...$keys) : array |
||
| 346 | { |
||
| 347 | return array_filter( |
||
| 348 | array_intersect_key($this->query, array_flip($keys)), |
||
| 349 | static function ($value) { |
||
| 350 | 107 | return $value !== null; |
|
| 351 | } |
||
| 352 | 107 | ); |
|
| 354 | |||
| 355 | /** |
||
| 356 | 107 | * Decorate the cursor with caching, hydration, and priming behavior. |
|
| 357 | * |
||
| 358 | 107 | * Note: while this method could strictly take a MongoDB\Driver\Cursor, we |
|
| 359 | 20 | * accept Traversable for testing purposes since Cursor cannot be mocked. |
|
| 360 | 20 | * HydratingIterator, CachingIterator, and BaseIterator expect a Traversable |
|
| 361 | * so this should not have any adverse effects. |
||
| 362 | */ |
||
| 363 | 107 | private function makeIterator(Traversable $cursor) : Iterator |
|
| 378 | 83 | ||
| 379 | /** |
||
| 380 | 83 | * Returns an array with its keys renamed based on the translation map. |
|
| 381 | 83 | * |
|
| 382 | 83 | * @return array $rename Translation map (from => to) for renaming keys |
|
| 383 | */ |
||
| 384 | 83 | private function renameQueryOptions(array $options, array $rename) : array |
|
| 405 | |||
| 406 | 123 | /** |
|
| 407 | * Execute the query and return its result. |
||
| 408 | 123 | * |
|
| 409 | 123 | * The return value will vary based on the query type. Commands with results |
|
| 410 | 107 | * (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other |
|
| 411 | 107 | * 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 | 107 | * return an Iterator. |
|
| 414 | 107 | * |
|
| 415 | 107 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
|
| 416 | */ |
||
| 417 | private function runQuery() |
||
| 503 | |||
| 504 | 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: