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 register documents in UnitOfWork. |
||
| 78 | * |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | private $readOnly; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Construct a Builder |
||
| 85 | * |
||
| 86 | * @param DocumentManager $dm |
||
| 87 | * @param string[]|string|null $documentName (optional) an array of document names, the document name, or none |
||
| 88 | */ |
||
| 89 | 210 | public function __construct(DocumentManager $dm, $documentName = null) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Set the current field to operate on. |
||
| 100 | * |
||
| 101 | * @param string $field |
||
| 102 | * @return $this |
||
| 103 | */ |
||
| 104 | 134 | public function field($field) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Use a primer to eagerly load all references in the current field. |
||
| 114 | * |
||
| 115 | * If $primer is true or a callable is provided, referenced documents for |
||
| 116 | * this field will loaded into UnitOfWork immediately after the query is |
||
| 117 | * executed. This will avoid multiple queries due to lazy initialization of |
||
| 118 | * Proxy objects. |
||
| 119 | * |
||
| 120 | * If $primer is false, no priming will take place. That is also the default |
||
| 121 | * behavior. |
||
| 122 | * |
||
| 123 | * If a custom callable is used, its signature should conform to the default |
||
| 124 | * Closure defined in {@link ReferencePrimer::__construct()}. |
||
| 125 | * |
||
| 126 | * @param boolean|callable $primer |
||
| 127 | * @return $this |
||
| 128 | * @throws \InvalidArgumentException If $primer is not boolean or callable |
||
| 129 | */ |
||
| 130 | 27 | public function prime($primer = true) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | 6 | public function eagerCursor($bool = true) |
|
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * @param bool $bool |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | 16 | public function hydrate($bool = true) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param bool $bool |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | 2 | public function readOnly($bool = true) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param bool $bool |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | 5 | public function refresh($bool = true) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Change the query type to find and optionally set and change the class being queried. |
||
| 195 | * |
||
| 196 | * @param string $documentName |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | 12 | public function find($documentName = null) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $documentName |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | 13 | public function findAndUpdate($documentName = null) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param bool $bool |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | 4 | public function returnNew($bool = true) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param string $documentName |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | 1 | public function findAndRemove($documentName = null) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $documentName |
||
| 245 | * @return $this |
||
| 246 | * |
||
| 247 | * @deprecated Deprecated in version 1.2 - use updateOne or updateMany instead |
||
| 248 | */ |
||
| 249 | 12 | public function update($documentName = null) |
|
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $documentName |
||
| 263 | * @return $this |
||
| 264 | */ |
||
| 265 | public function updateOne($documentName = null) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $documentName |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | public function updateMany($documentName = null) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $documentName |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | 1 | public function insert($documentName = null) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $documentName |
||
| 299 | * @return $this |
||
| 300 | */ |
||
| 301 | 1 | public function remove($documentName = null) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * @param object $document |
||
| 311 | * @return $this |
||
| 312 | */ |
||
| 313 | 10 | public function references($document) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param object $document |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | 6 | public function includesReferenceTo($document) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @inheritdoc |
||
| 331 | * @deprecated Deprecated in version 1.2 - use Aggregation Builder's group stage instead. |
||
| 332 | */ |
||
| 333 | 1 | public function group($keys, array $initial, $reduce = null, array $options = []) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * {@inheritdoc} |
||
| 344 | * |
||
| 345 | * @deprecated in version 1.2 - use setReadPreference instead. |
||
| 346 | */ |
||
| 347 | 3 | public function slaveOkay($bool = true) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Gets the Query executable. |
||
| 361 | * |
||
| 362 | * @param array $options |
||
| 363 | * @return Query $query |
||
| 364 | */ |
||
| 365 | 179 | public function getQuery(array $options = array()) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Create a new Expr instance that can be used as an expression with the Builder |
||
| 430 | * |
||
| 431 | * @return Expr $expr |
||
| 432 | */ |
||
| 433 | 20 | public function expr() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * @param string[]|string $documentName an array of document names or just one. |
||
| 443 | */ |
||
| 444 | 209 | private function setDocumentName($documentName) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get Discriminator Values |
||
| 473 | * |
||
| 474 | * @param \Iterator|array $classNames |
||
| 475 | * @return array an array of discriminatorValues (mixed type) |
||
| 476 | * @throws \InvalidArgumentException if the number of found collections > 1 |
||
| 477 | */ |
||
| 478 | 2 | private function getDiscriminatorValues($classNames) |
|
| 493 | } |
||
| 494 |
If you suppress an error, we recommend checking for the error condition explicitly: