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 |
||
| 43 | final class Query implements IteratorAggregate |
||
| 44 | { |
||
| 45 | public const TYPE_FIND = 1; |
||
| 46 | public const TYPE_FIND_AND_UPDATE = 2; |
||
| 47 | public const TYPE_FIND_AND_REMOVE = 3; |
||
| 48 | public const TYPE_INSERT = 4; |
||
| 49 | public const TYPE_UPDATE = 5; |
||
| 50 | public const TYPE_REMOVE = 6; |
||
| 51 | public const TYPE_DISTINCT = 9; |
||
| 52 | public const TYPE_COUNT = 11; |
||
| 53 | |||
| 54 | public const HINT_REFRESH = 1; |
||
| 55 | // 2 was used for HINT_SLAVE_OKAY, which was removed in 2.0 |
||
| 56 | public const HINT_READ_PREFERENCE = 3; |
||
| 57 | public const HINT_READ_ONLY = 5; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The DocumentManager instance. |
||
| 61 | * |
||
| 62 | * @var DocumentManager |
||
| 63 | */ |
||
| 64 | private $dm; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The ClassMetadata instance. |
||
| 68 | * |
||
| 69 | * @var ClassMetadata |
||
| 70 | */ |
||
| 71 | private $class; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Whether to hydrate results as document class instances. |
||
| 75 | * |
||
| 76 | * @var bool |
||
| 77 | */ |
||
| 78 | private $hydrate = true; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Array of primer Closure instances. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $primers = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Hints for UnitOfWork behavior. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private $unitOfWorkHints = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The Collection instance. |
||
| 96 | * |
||
| 97 | * @var Collection |
||
| 98 | */ |
||
| 99 | protected $collection; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Query structure generated by the Builder class. |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private $query; |
||
| 107 | |||
| 108 | /** @var Iterator|null */ |
||
| 109 | private $iterator; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Query options |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | private $options; |
||
| 117 | |||
| 118 | public function __construct(DocumentManager $dm, ClassMetadata $class, Collection $collection, array $query = [], array $options = [], bool $hydrate = true, bool $refresh = false, array $primers = [], bool $readOnly = false) |
||
| 119 | { |
||
| 120 | $primers = array_filter($primers); |
||
| 121 | |||
| 122 | switch ($query['type']) { |
||
| 123 | 164 | case self::TYPE_FIND: |
|
| 124 | case self::TYPE_FIND_AND_UPDATE: |
||
| 125 | 164 | case self::TYPE_FIND_AND_REMOVE: |
|
| 126 | case self::TYPE_INSERT: |
||
| 127 | case self::TYPE_UPDATE: |
||
| 128 | case self::TYPE_REMOVE: |
||
| 129 | 164 | case self::TYPE_DISTINCT: |
|
| 130 | case self::TYPE_COUNT: |
||
| 131 | 164 | break; |
|
| 132 | 164 | ||
| 133 | 39 | default: |
|
| 134 | 27 | throw new InvalidArgumentException('Invalid query type: ' . $query['type']); |
|
| 135 | 24 | } |
|
| 136 | 23 | ||
| 137 | 8 | $this->collection = $collection; |
|
| 138 | 6 | $this->query = $query; |
|
| 139 | 4 | $this->options = $options; |
|
| 140 | 163 | $this->dm = $dm; |
|
| 141 | $this->class = $class; |
||
| 142 | $this->hydrate = $hydrate; |
||
| 143 | 1 | $this->primers = $primers; |
|
| 144 | |||
| 145 | $this->setReadOnly($readOnly); |
||
| 146 | 163 | $this->setRefresh($refresh); |
|
| 147 | 163 | ||
| 148 | 163 | if (! isset($query['readPreference'])) { |
|
| 149 | 163 | return; |
|
| 150 | 163 | } |
|
| 151 | 163 | ||
| 152 | 163 | $this->unitOfWorkHints[self::HINT_READ_PREFERENCE] = $query['readPreference']; |
|
| 153 | } |
||
| 154 | 163 | ||
| 155 | 163 | public function __clone() |
|
| 159 | |||
| 160 | /** |
||
| 161 | 6 | * Return an array of information about the query structure for debugging. |
|
| 162 | 6 | * |
|
| 163 | * The $name parameter may be used to return a specific key from the |
||
| 164 | 64 | * internal $query array property. If omitted, the entire array will be |
|
| 165 | * returned. |
||
| 166 | 64 | */ |
|
| 167 | 64 | public function debug(?string $name = null) |
|
| 168 | { |
||
| 169 | return $name !== null ? $this->query[$name] : $this->query; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Execute the query and returns the results. |
||
| 174 | * |
||
| 175 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
| 176 | 27 | * |
|
| 177 | * @throws MongoDBException |
||
| 178 | 27 | */ |
|
| 179 | public function execute() |
||
| 180 | { |
||
| 181 | $results = $this->runQuery(); |
||
| 182 | |||
| 183 | if (! $this->hydrate) { |
||
| 184 | return $results; |
||
| 185 | } |
||
| 186 | |||
| 187 | $uow = $this->dm->getUnitOfWork(); |
||
| 188 | 123 | ||
| 189 | /* If a single document is returned from a findAndModify command and it |
||
| 190 | 123 | * includes the identifier field, attempt hydration. |
|
| 191 | */ |
||
| 192 | 122 | if (($this->query['type'] === self::TYPE_FIND_AND_UPDATE || |
|
| 193 | 9 | $this->query['type'] === self::TYPE_FIND_AND_REMOVE) && |
|
| 194 | is_array($results) && isset($results['_id'])) { |
||
| 195 | $results = $uow->getOrCreateDocument($this->class->name, $results, $this->unitOfWorkHints); |
||
| 196 | 116 | ||
| 197 | if (! empty($this->primers)) { |
||
| 198 | $referencePrimer = new ReferencePrimer($this->dm, $uow); |
||
| 199 | |||
| 200 | foreach ($this->primers as $fieldName => $primer) { |
||
| 201 | 116 | $primer = is_callable($primer) ? $primer : null; |
|
| 202 | 116 | $referencePrimer->primeReferences($this->class, [$results], $fieldName, $this->unitOfWorkHints, $primer); |
|
| 203 | 116 | } |
|
| 204 | 5 | } |
|
| 205 | } |
||
| 206 | 5 | ||
| 207 | 1 | return $results; |
|
| 208 | } |
||
| 209 | 1 | ||
| 210 | 1 | /** |
|
| 211 | 1 | * Gets the ClassMetadata instance. |
|
| 212 | */ |
||
| 213 | public function getClass() : ClassMetadata |
||
| 214 | { |
||
| 215 | return $this->class; |
||
| 216 | 116 | } |
|
| 217 | |||
| 218 | public function getDocumentManager() : DocumentManager |
||
| 219 | { |
||
| 220 | return $this->dm; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Execute the query and return its result, which must be an Iterator. |
||
| 225 | * |
||
| 226 | * If the query type is not expected to return an Iterator, |
||
| 227 | * BadMethodCallException will be thrown before executing the query. |
||
| 228 | * Otherwise, the query will be executed and UnexpectedValueException will |
||
| 229 | * be thrown if {@link Query::execute()} does not return an Iterator. |
||
| 230 | * |
||
| 231 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
| 232 | * |
||
| 233 | * @throws BadMethodCallException If the query type would not return an Iterator. |
||
| 234 | * @throws UnexpectedValueException If the query did not return an Iterator. |
||
| 235 | * @throws MongoDBException |
||
| 236 | */ |
||
| 237 | public function getIterator() : Iterator |
||
| 238 | { |
||
| 239 | switch ($this->query['type']) { |
||
| 240 | case self::TYPE_FIND: |
||
| 241 | case self::TYPE_DISTINCT: |
||
| 242 | break; |
||
| 243 | |||
| 244 | default: |
||
| 245 | throw new BadMethodCallException('Iterator would not be returned for query type: ' . $this->query['type']); |
||
| 246 | 83 | } |
|
| 247 | |||
| 248 | 83 | if ($this->iterator === null) { |
|
| 249 | 83 | $result = $this->execute(); |
|
| 250 | 6 | if (! $result instanceof Iterator) { |
|
| 251 | 77 | throw new UnexpectedValueException('Iterator was not returned for query type: ' . $this->query['type']); |
|
| 252 | } |
||
| 253 | $this->iterator = $result; |
||
| 254 | 6 | } |
|
| 255 | |||
| 256 | return $this->iterator; |
||
| 257 | 77 | } |
|
| 258 | 77 | ||
| 259 | 77 | /** |
|
| 260 | * Return the query structure. |
||
| 261 | */ |
||
| 262 | 77 | public function getQuery() : array |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Execute the query and return the first result. |
||
| 269 | * |
||
| 270 | * @return array|object|null |
||
| 271 | 14 | */ |
|
| 272 | public function getSingleResult() |
||
| 273 | 14 | { |
|
| 274 | $clonedQuery = clone $this; |
||
| 275 | $clonedQuery->query['limit'] = 1; |
||
| 276 | return $clonedQuery->getIterator()->current() ?: null; |
||
|
|
|||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return the query type. |
||
| 281 | 64 | */ |
|
| 282 | public function getType() : int |
||
| 283 | 64 | { |
|
| 284 | 64 | return $this->query['type']; |
|
| 285 | 64 | } |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Sets whether or not to hydrate the documents to objects. |
||
| 289 | */ |
||
| 290 | public function setHydrate(bool $hydrate) : void |
||
| 291 | { |
||
| 292 | $this->hydrate = $hydrate; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set whether documents should be registered in UnitOfWork. If document would |
||
| 297 | * already be managed it will be left intact and new instance returned. |
||
| 298 | * |
||
| 299 | * This option has no effect if hydration is disabled. |
||
| 300 | */ |
||
| 301 | public function setReadOnly(bool $readOnly) : void |
||
| 302 | { |
||
| 303 | $this->unitOfWorkHints[self::HINT_READ_ONLY] = $readOnly; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Set whether to refresh hydrated documents that are already in the |
||
| 308 | * identity map. |
||
| 309 | * |
||
| 310 | 163 | * This option has no effect if hydration is disabled. |
|
| 311 | */ |
||
| 312 | 163 | public function setRefresh(bool $refresh) : void |
|
| 313 | 163 | { |
|
| 314 | $this->unitOfWorkHints[self::HINT_REFRESH] = $refresh; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Execute the query and return its results as an array. |
||
| 319 | * |
||
| 320 | * @see IteratorAggregate::toArray() |
||
| 321 | 163 | */ |
|
| 322 | public function toArray() : array |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns an array containing the specified keys and their values from the |
||
| 329 | * query array, provided they exist and are not null. |
||
| 330 | */ |
||
| 331 | 11 | private function getQueryOptions(string ...$keys) : array |
|
| 332 | { |
||
| 333 | 11 | return array_filter( |
|
| 334 | array_intersect_key($this->query, array_flip($keys)), |
||
| 335 | static function ($value) { |
||
| 336 | return $value !== null; |
||
| 337 | } |
||
| 340 | 121 | ||
| 341 | private function makeIterator(Cursor $cursor) : Iterator |
||
| 356 | 106 | ||
| 357 | /** |
||
| 358 | 106 | * Returns an array with its keys renamed based on the translation map. |
|
| 359 | 20 | * |
|
| 360 | 20 | * @return array $rename Translation map (from => to) for renaming keys |
|
| 361 | */ |
||
| 362 | private function renameQueryOptions(array $options, array $rename) : array |
||
| 383 | |||
| 384 | 82 | /** |
|
| 385 | * Execute the query and return its result. |
||
| 386 | * |
||
| 387 | * The return value will vary based on the query type. Commands with results |
||
| 388 | 82 | * (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other |
|
| 389 | * commands and operations may return a status array or a boolean, depending |
||
| 390 | 82 | * on the driver's write concern. Queries and some mapReduce commands will |
|
| 391 | * return an Iterator. |
||
| 392 | * |
||
| 393 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
| 394 | */ |
||
| 395 | public function runQuery() |
||
| 489 | 2 | ||
| 490 | 2 | private function isFirstKeyUpdateOperator() : bool |
|
| 497 | } |
||
| 498 |
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: