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 |
||
| 40 | class Query implements IteratorAggregate |
||
| 41 | { |
||
| 42 | public const TYPE_FIND = 1; |
||
| 43 | public const TYPE_FIND_AND_UPDATE = 2; |
||
| 44 | public const TYPE_FIND_AND_REMOVE = 3; |
||
| 45 | public const TYPE_INSERT = 4; |
||
| 46 | public const TYPE_UPDATE = 5; |
||
| 47 | public const TYPE_REMOVE = 6; |
||
| 48 | public const TYPE_GROUP = 7; |
||
| 49 | public const TYPE_MAP_REDUCE = 8; |
||
| 50 | public const TYPE_DISTINCT = 9; |
||
| 51 | public const TYPE_COUNT = 11; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @deprecated 1.1 Will be removed for 2.0 |
||
| 55 | */ |
||
| 56 | public const TYPE_GEO_LOCATION = 10; |
||
| 57 | |||
| 58 | public const HINT_REFRESH = 1; |
||
| 59 | // 2 was used for HINT_SLAVE_OKAY, which was removed in 2.0 |
||
| 60 | public const HINT_READ_PREFERENCE = 3; |
||
| 61 | public const HINT_READ_ONLY = 5; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The DocumentManager instance. |
||
| 65 | * |
||
| 66 | * @var DocumentManager |
||
| 67 | */ |
||
| 68 | private $dm; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The ClassMetadata instance. |
||
| 72 | * |
||
| 73 | * @var ClassMetadata |
||
| 74 | */ |
||
| 75 | private $class; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Whether to hydrate results as document class instances. |
||
| 79 | * |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $hydrate = true; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Array of primer Closure instances. |
||
| 86 | * |
||
| 87 | * @var array |
||
| 88 | */ |
||
| 89 | private $primers = []; |
||
| 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 | 162 | public function __construct(DocumentManager $dm, ClassMetadata $class, Collection $collection, array $query = [], array $options = [], bool $hydrate = true, bool $refresh = false, array $primers = [], bool $readOnly = false) |
|
| 123 | { |
||
| 124 | 162 | $primers = array_filter($primers); |
|
| 125 | |||
| 126 | 162 | switch ($query['type']) { |
|
| 127 | 162 | case self::TYPE_FIND: |
|
| 128 | 38 | case self::TYPE_FIND_AND_UPDATE: |
|
| 129 | 26 | case self::TYPE_FIND_AND_REMOVE: |
|
| 130 | 23 | case self::TYPE_INSERT: |
|
| 131 | 22 | case self::TYPE_UPDATE: |
|
| 132 | 8 | case self::TYPE_REMOVE: |
|
| 133 | 6 | case self::TYPE_GROUP: |
|
| 134 | 6 | case self::TYPE_MAP_REDUCE: |
|
| 135 | 6 | case self::TYPE_DISTINCT: |
|
| 136 | 4 | case self::TYPE_COUNT: |
|
| 137 | 161 | break; |
|
| 138 | |||
| 139 | default: |
||
| 140 | 1 | throw new InvalidArgumentException('Invalid query type: ' . $query['type']); |
|
| 141 | } |
||
| 142 | |||
| 143 | 161 | $this->collection = $collection; |
|
| 144 | 161 | $this->query = $query; |
|
| 145 | 161 | $this->options = $options; |
|
| 146 | 161 | $this->dm = $dm; |
|
| 147 | 161 | $this->class = $class; |
|
| 148 | 161 | $this->hydrate = $hydrate; |
|
| 149 | 161 | $this->primers = $primers; |
|
| 150 | |||
| 151 | 161 | $this->setReadOnly($readOnly); |
|
| 152 | 161 | $this->setRefresh($refresh); |
|
| 153 | |||
| 154 | 161 | if (! isset($query['readPreference'])) { |
|
| 155 | 155 | return; |
|
| 156 | } |
||
| 157 | |||
| 158 | 6 | $this->unitOfWorkHints[self::HINT_READ_PREFERENCE] = $query['readPreference']; |
|
| 159 | 6 | } |
|
| 160 | |||
| 161 | 64 | public function __clone() |
|
| 162 | { |
||
| 163 | 64 | $this->iterator = null; |
|
| 164 | 64 | } |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Return an array of information about the query structure for debugging. |
||
| 168 | * |
||
| 169 | * The $name parameter may be used to return a specific key from the |
||
| 170 | * internal $query array property. If omitted, the entire array will be |
||
| 171 | * returned. |
||
| 172 | */ |
||
| 173 | 27 | public function debug(?string $name = null) |
|
| 174 | { |
||
| 175 | 27 | return $name !== null ? $this->query[$name] : $this->query; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Execute the query and returns the results. |
||
| 180 | * |
||
| 181 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
| 182 | * |
||
| 183 | * @throws MongoDBException |
||
| 184 | */ |
||
| 185 | 122 | public function execute() |
|
| 186 | { |
||
| 187 | 122 | $results = $this->runQuery(); |
|
| 188 | |||
| 189 | 122 | if (! $this->hydrate) { |
|
| 190 | 9 | return $results; |
|
| 191 | } |
||
| 192 | |||
| 193 | 116 | $uow = $this->dm->getUnitOfWork(); |
|
| 194 | |||
| 195 | /* If a single document is returned from a findAndModify command and it |
||
| 196 | * includes the identifier field, attempt hydration. |
||
| 197 | */ |
||
| 198 | 116 | if (($this->query['type'] === self::TYPE_FIND_AND_UPDATE || |
|
| 199 | 116 | $this->query['type'] === self::TYPE_FIND_AND_REMOVE) && |
|
| 200 | 116 | is_array($results) && isset($results['_id'])) { |
|
| 201 | 5 | $results = $uow->getOrCreateDocument($this->class->name, $results, $this->unitOfWorkHints); |
|
| 202 | |||
| 203 | 5 | if (! empty($this->primers)) { |
|
| 204 | 1 | $referencePrimer = new ReferencePrimer($this->dm, $uow); |
|
| 205 | |||
| 206 | 1 | foreach ($this->primers as $fieldName => $primer) { |
|
| 207 | 1 | $primer = is_callable($primer) ? $primer : null; |
|
| 208 | 1 | $referencePrimer->primeReferences($this->class, [$results], $fieldName, $this->unitOfWorkHints, $primer); |
|
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | 116 | return $results; |
|
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Gets the ClassMetadata instance. |
||
| 218 | */ |
||
| 219 | public function getClass() : ClassMetadata |
||
| 223 | |||
| 224 | public function getDocumentManager() : DocumentManager |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Execute the query and return its result, which must be an Iterator. |
||
| 231 | * |
||
| 232 | * If the query type is not expected to return an Iterator, |
||
| 233 | * BadMethodCallException will be thrown before executing the query. |
||
| 234 | * Otherwise, the query will be executed and UnexpectedValueException will |
||
| 235 | * be thrown if {@link Query::execute()} does not return an Iterator. |
||
| 236 | * |
||
| 237 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 238 | * |
||
| 239 | * @throws BadMethodCallException If the query type would not return an Iterator. |
||
| 240 | * @throws UnexpectedValueException If the query did not return an Iterator. |
||
| 241 | * @throws MongoDBException |
||
| 242 | */ |
||
| 243 | 83 | public function getIterator() : Iterator |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Return the query structure. |
||
| 269 | */ |
||
| 270 | 13 | public function getQuery() : array |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Execute the query and return the first result. |
||
| 277 | * |
||
| 278 | * @return array|object|null |
||
| 279 | */ |
||
| 280 | 64 | public function getSingleResult() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Return the query type. |
||
| 289 | */ |
||
| 290 | public function getType() : int |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Sets whether or not to hydrate the documents to objects. |
||
| 297 | */ |
||
| 298 | public function setHydrate(bool $hydrate) : void |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Set whether documents should be registered in UnitOfWork. If document would |
||
| 305 | * already be managed it will be left intact and new instance returned. |
||
| 306 | * |
||
| 307 | * This option has no effect if hydration is disabled. |
||
| 308 | */ |
||
| 309 | 161 | public function setReadOnly(bool $readOnly) : void |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Set whether to refresh hydrated documents that are already in the |
||
| 316 | * identity map. |
||
| 317 | * |
||
| 318 | * This option has no effect if hydration is disabled. |
||
| 319 | */ |
||
| 320 | 161 | public function setRefresh(bool $refresh) : void |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Execute the query and return its results as an array. |
||
| 327 | * |
||
| 328 | * @see IteratorAggregate::toArray() |
||
| 329 | */ |
||
| 330 | 11 | public function toArray() : array |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Returns an array containing the specified keys and their values from the |
||
| 337 | * query array, provided they exist and are not null. |
||
| 338 | */ |
||
| 339 | 121 | private function getQueryOptions(string ...$keys) : array |
|
| 348 | |||
| 349 | 106 | private function makeIterator(Cursor $cursor) : Iterator |
|
| 350 | { |
||
| 351 | 106 | if ($this->hydrate) { |
|
| 352 | 98 | $cursor = new HydratingIterator($cursor, $this->dm->getUnitOfWork(), $this->class, $this->unitOfWorkHints); |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Returns an array with its keys renamed based on the translation map. |
||
| 367 | * |
||
| 368 | * @return array $rename Translation map (from => to) for renaming keys |
||
| 369 | */ |
||
| 370 | 111 | private function renameQueryOptions(array $options, array $rename) : array |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Execute the query and return its result. |
||
| 394 | * |
||
| 395 | * The return value will vary based on the query type. Commands with results |
||
| 396 | * (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other |
||
| 397 | * commands and operations may return a status array or a boolean, depending |
||
| 398 | * on the driver's write concern. Queries and some mapReduce commands will |
||
| 399 | * return an Iterator. |
||
| 400 | * |
||
| 401 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
| 402 | */ |
||
| 403 | 122 | public function runQuery() |
|
| 483 | } |
||
| 484 |
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: