Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Builder 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 Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Builder extends \Doctrine\MongoDB\Query\Builder |
||
32 | { |
||
33 | /** |
||
34 | * The DocumentManager instance for this query |
||
35 | * |
||
36 | * @var DocumentManager |
||
37 | */ |
||
38 | private $dm; |
||
39 | |||
40 | /** |
||
41 | * The ClassMetadata instance. |
||
42 | * |
||
43 | * @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadata |
||
44 | */ |
||
45 | private $class; |
||
46 | |||
47 | /** |
||
48 | * The current field we are operating on. |
||
49 | * |
||
50 | * @todo Change this to private once ODM requires doctrine/mongodb 1.1+ |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $currentField; |
||
54 | |||
55 | /** |
||
56 | * Whether or not to hydrate the data to documents. |
||
57 | * |
||
58 | * @var boolean |
||
59 | */ |
||
60 | private $hydrate = true; |
||
61 | |||
62 | /** |
||
63 | * Whether or not to refresh the data for documents that are already in the identity map. |
||
64 | * |
||
65 | * @var boolean |
||
66 | */ |
||
67 | private $refresh = false; |
||
68 | |||
69 | /** |
||
70 | * Array of primer Closure instances. |
||
71 | * |
||
72 | * @var array |
||
73 | */ |
||
74 | private $primers = array(); |
||
75 | |||
76 | /** |
||
77 | * Whether or not to require indexes. |
||
78 | * |
||
79 | * @var bool |
||
80 | */ |
||
81 | private $requireIndexes; |
||
82 | |||
83 | /** |
||
84 | * Whether or not to register documents in UnitOfWork. |
||
85 | * |
||
86 | * @var bool |
||
87 | */ |
||
88 | private $readOnly; |
||
89 | |||
90 | /** |
||
91 | * Construct a Builder |
||
92 | * |
||
93 | * @param DocumentManager $dm |
||
94 | * @param string[]|string|null $documentName (optional) an array of document names, the document name, or none |
||
95 | */ |
||
96 | 234 | public function __construct(DocumentManager $dm, $documentName = null) |
|
104 | |||
105 | /** |
||
106 | * Set whether or not to require indexes. |
||
107 | * |
||
108 | * @param bool $requireIndexes |
||
109 | * @return $this |
||
110 | * |
||
111 | * @deprecated method was deprecated in 1.2 and will be removed in 2.0 |
||
112 | */ |
||
113 | 2 | public function requireIndexes($requireIndexes = true) |
|
122 | |||
123 | /** |
||
124 | * Set the current field to operate on. |
||
125 | * |
||
126 | * @param string $field |
||
127 | * @return $this |
||
128 | */ |
||
129 | 155 | public function field($field) |
|
136 | |||
137 | /** |
||
138 | * Use a primer to eagerly load all references in the current field. |
||
139 | * |
||
140 | * If $primer is true or a callable is provided, referenced documents for |
||
141 | * this field will loaded into UnitOfWork immediately after the query is |
||
142 | * executed. This will avoid multiple queries due to lazy initialization of |
||
143 | * Proxy objects. |
||
144 | * |
||
145 | * If $primer is false, no priming will take place. That is also the default |
||
146 | * behavior. |
||
147 | * |
||
148 | * If a custom callable is used, its signature should conform to the default |
||
149 | * Closure defined in {@link ReferencePrimer::__construct()}. |
||
150 | * |
||
151 | * @param boolean|callable $primer |
||
152 | * @return $this |
||
153 | * @throws \InvalidArgumentException If $primer is not boolean or callable |
||
154 | */ |
||
155 | 27 | public function prime($primer = true) |
|
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | 6 | public function eagerCursor($bool = true) |
|
186 | |||
187 | |||
188 | /** |
||
189 | * @param bool $bool |
||
190 | * @return $this |
||
191 | */ |
||
192 | 16 | public function hydrate($bool = true) |
|
197 | |||
198 | /** |
||
199 | * @param bool $bool |
||
200 | * @return $this |
||
201 | */ |
||
202 | 2 | public function readOnly($bool = true) |
|
207 | |||
208 | /** |
||
209 | * @param bool $bool |
||
210 | * @return $this |
||
211 | */ |
||
212 | 5 | public function refresh($bool = true) |
|
217 | |||
218 | /** |
||
219 | * Change the query type to find and optionally set and change the class being queried. |
||
220 | * |
||
221 | * @param string $documentName |
||
222 | * @return $this |
||
223 | */ |
||
224 | 12 | public function find($documentName = null) |
|
231 | |||
232 | /** |
||
233 | * @param string $documentName |
||
234 | * @return $this |
||
235 | */ |
||
236 | 13 | public function findAndUpdate($documentName = null) |
|
243 | |||
244 | /** |
||
245 | * @param bool $bool |
||
246 | * @return $this |
||
247 | */ |
||
248 | 4 | public function returnNew($bool = true) |
|
255 | |||
256 | /** |
||
257 | * @param string $documentName |
||
258 | * @return $this |
||
259 | */ |
||
260 | 1 | public function findAndRemove($documentName = null) |
|
267 | |||
268 | /** |
||
269 | * @param string $documentName |
||
270 | * @return $this |
||
271 | * |
||
272 | * @deprecated Deprecated in version 1.2 - use updateOne or updateMany instead |
||
273 | */ |
||
274 | 12 | public function update($documentName = null) |
|
285 | |||
286 | /** |
||
287 | * @param string $documentName |
||
288 | * @return $this |
||
289 | */ |
||
290 | public function updateOne($documentName = null) |
||
297 | |||
298 | /** |
||
299 | * @param string $documentName |
||
300 | * @return $this |
||
301 | */ |
||
302 | public function updateMany($documentName = null) |
||
309 | |||
310 | /** |
||
311 | * @param string $documentName |
||
312 | * @return $this |
||
313 | */ |
||
314 | 1 | public function insert($documentName = null) |
|
321 | |||
322 | /** |
||
323 | * @param string $documentName |
||
324 | * @return $this |
||
325 | */ |
||
326 | 1 | public function remove($documentName = null) |
|
333 | |||
334 | /** |
||
335 | * @param object $document |
||
336 | * @return $this |
||
337 | */ |
||
338 | 11 | public function references($document) |
|
343 | |||
344 | /** |
||
345 | * @param object $document |
||
346 | * @return $this |
||
347 | */ |
||
348 | 7 | public function includesReferenceTo($document) |
|
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | * @deprecated Deprecated in version 1.2 - use Aggregation Builder's group stage instead. |
||
357 | */ |
||
358 | 1 | public function group($keys, array $initial, $reduce = null, array $options = []) |
|
366 | |||
367 | /** |
||
368 | * {@inheritdoc} |
||
369 | * |
||
370 | * @deprecated in version 1.2 - use setReadPreference instead. |
||
371 | */ |
||
372 | 3 | public function slaveOkay($bool = true) |
|
383 | |||
384 | /** |
||
385 | * Gets the Query executable. |
||
386 | * |
||
387 | * @param array $options |
||
388 | * @return Query $query |
||
389 | */ |
||
390 | 203 | public function getQuery(array $options = array()) |
|
453 | |||
454 | /** |
||
455 | * Create a new Expr instance that can be used as an expression with the Builder |
||
456 | * |
||
457 | * @return Expr $expr |
||
458 | */ |
||
459 | 25 | public function expr() |
|
466 | |||
467 | /** |
||
468 | * @param string[]|string $documentName an array of document names or just one. |
||
469 | */ |
||
470 | 233 | private function setDocumentName($documentName) |
|
496 | |||
497 | /** |
||
498 | * Get Discriminator Values |
||
499 | * |
||
500 | * @param \Iterator|array $classNames |
||
501 | * @return array an array of discriminatorValues (mixed type) |
||
502 | * @throws \InvalidArgumentException if the number of found collections > 1 |
||
503 | */ |
||
504 | 2 | private function getDiscriminatorValues($classNames) |
|
519 | } |
||
520 |
If you suppress an error, we recommend checking for the error condition explicitly: