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 | 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) |
|
154 | |||
155 | 64 | public function __clone() |
|
159 | |||
160 | /** |
||
161 | * Return an array of information about the query structure for debugging. |
||
162 | * |
||
163 | * The $name parameter may be used to return a specific key from the |
||
164 | * internal $query array property. If omitted, the entire array will be |
||
165 | * returned. |
||
166 | */ |
||
167 | 27 | public function debug(?string $name = null) |
|
171 | |||
172 | /** |
||
173 | * Execute the query and returns the results. |
||
174 | * |
||
175 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
176 | * |
||
177 | * @throws MongoDBException |
||
178 | */ |
||
179 | 123 | public function execute() |
|
209 | |||
210 | /** |
||
211 | * Gets the ClassMetadata instance. |
||
212 | */ |
||
213 | public function getClass() : ClassMetadata |
||
217 | |||
218 | public function getDocumentManager() : DocumentManager |
||
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 | 83 | public function getIterator() : Iterator |
|
258 | |||
259 | /** |
||
260 | * Return the query structure. |
||
261 | */ |
||
262 | 14 | public function getQuery() : array |
|
266 | |||
267 | /** |
||
268 | * Execute the query and return the first result. |
||
269 | * |
||
270 | * @return array|object|null |
||
271 | */ |
||
272 | 64 | public function getSingleResult() |
|
278 | |||
279 | /** |
||
280 | * Return the query type. |
||
281 | */ |
||
282 | public function getType() : int |
||
286 | |||
287 | /** |
||
288 | * Sets whether or not to hydrate the documents to objects. |
||
289 | */ |
||
290 | public function setHydrate(bool $hydrate) : void |
||
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 | 163 | public function setReadOnly(bool $readOnly) : void |
|
305 | |||
306 | /** |
||
307 | * Set whether to refresh hydrated documents that are already in the |
||
308 | * identity map. |
||
309 | * |
||
310 | * This option has no effect if hydration is disabled. |
||
311 | */ |
||
312 | 163 | public function setRefresh(bool $refresh) : void |
|
316 | |||
317 | /** |
||
318 | * Execute the query and return its results as an array. |
||
319 | * |
||
320 | * @see IteratorAggregate::toArray() |
||
321 | */ |
||
322 | 11 | 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 | 121 | private function getQueryOptions(string ...$keys) : array |
|
340 | |||
341 | 106 | private function makeIterator(Cursor $cursor) : Iterator |
|
356 | |||
357 | /** |
||
358 | * Returns an array with its keys renamed based on the translation map. |
||
359 | * |
||
360 | * @return array $rename Translation map (from => to) for renaming keys |
||
361 | */ |
||
362 | 111 | private function renameQueryOptions(array $options, array $rename) : array |
|
383 | |||
384 | /** |
||
385 | * Execute the query and return its result. |
||
386 | * |
||
387 | * The return value will vary based on the query type. Commands with results |
||
388 | * (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other |
||
389 | * commands and operations may return a status array or a boolean, depending |
||
390 | * 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 | 123 | public function runQuery() |
|
489 | |||
490 | 19 | 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: