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_DISTINCT = 9; |
||
49 | public const TYPE_COUNT = 11; |
||
50 | |||
51 | public const HINT_REFRESH = 1; |
||
52 | // 2 was used for HINT_SLAVE_OKAY, which was removed in 2.0 |
||
53 | public const HINT_READ_PREFERENCE = 3; |
||
54 | public const HINT_READ_ONLY = 5; |
||
55 | |||
56 | /** |
||
57 | * The DocumentManager instance. |
||
58 | * |
||
59 | * @var DocumentManager |
||
60 | */ |
||
61 | private $dm; |
||
62 | |||
63 | /** |
||
64 | * The ClassMetadata instance. |
||
65 | * |
||
66 | * @var ClassMetadata |
||
67 | */ |
||
68 | private $class; |
||
69 | |||
70 | /** |
||
71 | * Whether to hydrate results as document class instances. |
||
72 | * |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $hydrate = true; |
||
76 | |||
77 | /** |
||
78 | * Array of primer Closure instances. |
||
79 | * |
||
80 | * @var array |
||
81 | */ |
||
82 | private $primers = []; |
||
83 | |||
84 | /** |
||
85 | * Hints for UnitOfWork behavior. |
||
86 | * |
||
87 | * @var array |
||
88 | */ |
||
89 | private $unitOfWorkHints = []; |
||
90 | |||
91 | /** |
||
92 | * The Collection instance. |
||
93 | * |
||
94 | * @var Collection |
||
95 | */ |
||
96 | protected $collection; |
||
97 | |||
98 | /** |
||
99 | * Query structure generated by the Builder class. |
||
100 | * |
||
101 | * @var array |
||
102 | */ |
||
103 | private $query; |
||
104 | |||
105 | /** @var Iterator|null */ |
||
106 | private $iterator; |
||
107 | |||
108 | /** |
||
109 | * Query options |
||
110 | * |
||
111 | * @var array |
||
112 | */ |
||
113 | private $options; |
||
114 | |||
115 | public function __construct(DocumentManager $dm, ClassMetadata $class, Collection $collection, array $query = [], array $options = [], bool $hydrate = true, bool $refresh = false, array $primers = [], bool $readOnly = false) |
||
151 | 162 | ||
152 | 162 | public function __clone() |
|
156 | |||
157 | /** |
||
158 | 6 | * Return an array of information about the query structure for debugging. |
|
159 | 6 | * |
|
160 | * The $name parameter may be used to return a specific key from the |
||
161 | 64 | * internal $query array property. If omitted, the entire array will be |
|
162 | * returned. |
||
163 | 64 | */ |
|
164 | 64 | public function debug(?string $name = null) |
|
168 | |||
169 | /** |
||
170 | * Execute the query and returns the results. |
||
171 | * |
||
172 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
173 | 27 | * |
|
174 | * @throws MongoDBException |
||
175 | 27 | */ |
|
176 | public function execute() |
||
206 | 1 | ||
207 | 1 | /** |
|
208 | 1 | * Gets the ClassMetadata instance. |
|
209 | */ |
||
210 | public function getClass() : ClassMetadata |
||
214 | |||
215 | public function getDocumentManager() : DocumentManager |
||
219 | |||
220 | /** |
||
221 | * Execute the query and return its result, which must be an Iterator. |
||
222 | * |
||
223 | * If the query type is not expected to return an Iterator, |
||
224 | * BadMethodCallException will be thrown before executing the query. |
||
225 | * Otherwise, the query will be executed and UnexpectedValueException will |
||
226 | * be thrown if {@link Query::execute()} does not return an Iterator. |
||
227 | * |
||
228 | * @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
||
229 | * |
||
230 | * @throws BadMethodCallException If the query type would not return an Iterator. |
||
231 | * @throws UnexpectedValueException If the query did not return an Iterator. |
||
232 | * @throws MongoDBException |
||
233 | */ |
||
234 | public function getIterator() : Iterator |
||
255 | |||
256 | 77 | /** |
|
257 | 77 | * Return the query structure. |
|
258 | 77 | */ |
|
259 | public function getQuery() : array |
||
263 | |||
264 | 77 | /** |
|
265 | * Execute the query and return the first result. |
||
266 | * |
||
267 | * @return array|object|null |
||
268 | */ |
||
269 | public function getSingleResult() |
||
275 | |||
276 | /** |
||
277 | * Return the query type. |
||
278 | */ |
||
279 | public function getType() : int |
||
283 | 64 | ||
284 | 64 | /** |
|
285 | * Sets whether or not to hydrate the documents to objects. |
||
286 | */ |
||
287 | public function setHydrate(bool $hydrate) : void |
||
291 | |||
292 | /** |
||
293 | * Set whether documents should be registered in UnitOfWork. If document would |
||
294 | * already be managed it will be left intact and new instance returned. |
||
295 | * |
||
296 | * This option has no effect if hydration is disabled. |
||
297 | */ |
||
298 | public function setReadOnly(bool $readOnly) : void |
||
302 | |||
303 | /** |
||
304 | * Set whether to refresh hydrated documents that are already in the |
||
305 | * identity map. |
||
306 | * |
||
307 | * This option has no effect if hydration is disabled. |
||
308 | */ |
||
309 | 162 | public function setRefresh(bool $refresh) : void |
|
313 | |||
314 | /** |
||
315 | * Execute the query and return its results as an array. |
||
316 | * |
||
317 | * @see IteratorAggregate::toArray() |
||
318 | */ |
||
319 | public function toArray() : array |
||
323 | 162 | ||
324 | /** |
||
325 | * Returns an array containing the specified keys and their values from the |
||
326 | * query array, provided they exist and are not null. |
||
327 | */ |
||
328 | private function getQueryOptions(string ...$keys) : array |
||
337 | |||
338 | private function makeIterator(Cursor $cursor) : Iterator |
||
353 | |||
354 | /** |
||
355 | 106 | * Returns an array with its keys renamed based on the translation map. |
|
356 | * |
||
357 | 106 | * @return array $rename Translation map (from => to) for renaming keys |
|
358 | 20 | */ |
|
359 | 20 | private function renameQueryOptions(array $options, array $rename) : array |
|
380 | 82 | ||
381 | 82 | /** |
|
382 | * Execute the query and return its result. |
||
383 | 82 | * |
|
384 | * The return value will vary based on the query type. Commands with results |
||
385 | * (e.g. aggregate, inline mapReduce) may return an ArrayIterator. Other |
||
386 | * commands and operations may return a status array or a boolean, depending |
||
387 | 82 | * on the driver's write concern. Queries and some mapReduce commands will |
|
388 | * return an Iterator. |
||
389 | 82 | * |
|
390 | * @return Iterator|UpdateResult|InsertOneResult|DeleteResult|array|object|int|null |
||
391 | */ |
||
392 | public function runQuery() |
||
472 | } |
||
473 |
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: