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