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 | 216 | 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 | 2 | public function requireIndexes($requireIndexes = true) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Set the current field to operate on. |
||
| 119 | * |
||
| 120 | * @param string $field |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | 136 | public function field($field) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Use a primer to eagerly load all references in the current field. |
||
| 133 | * |
||
| 134 | * If $primer is true or a callable is provided, referenced documents for |
||
| 135 | * this field will loaded into UnitOfWork immediately after the query is |
||
| 136 | * executed. This will avoid multiple queries due to lazy initialization of |
||
| 137 | * Proxy objects. |
||
| 138 | * |
||
| 139 | * If $primer is false, no priming will take place. That is also the default |
||
| 140 | * behavior. |
||
| 141 | * |
||
| 142 | * If a custom callable is used, its signature should conform to the default |
||
| 143 | * Closure defined in {@link ReferencePrimer::__construct()}. |
||
| 144 | * |
||
| 145 | * @param boolean|callable $primer |
||
| 146 | * @return $this |
||
| 147 | * @throws \InvalidArgumentException If $primer is not boolean or callable |
||
| 148 | */ |
||
| 149 | 21 | public function prime($primer = true) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | 5 | public function eagerCursor($bool = true) |
|
| 180 | |||
| 181 | |||
| 182 | /** |
||
| 183 | * @param bool $bool |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | 16 | public function hydrate($bool = true) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * @param bool $bool |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | 1 | public function readOnly($bool = true) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @param bool $bool |
||
| 204 | * @return $this |
||
| 205 | */ |
||
| 206 | 5 | public function refresh($bool = true) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Change the query type to find and optionally set and change the class being queried. |
||
| 214 | * |
||
| 215 | * @param string $documentName |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | 12 | public function find($documentName = null) |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $documentName |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | 7 | public function findAndUpdate($documentName = null) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @param bool $bool |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | 4 | public function returnNew($bool = true) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $documentName |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | 1 | public function findAndRemove($documentName = null) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @param string $documentName |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | 12 | public function update($documentName = null) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $documentName |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | 1 | public function insert($documentName = null) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param string $documentName |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | 1 | public function remove($documentName = null) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @param object $document |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | 8 | public function references($document) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @param object $document |
||
| 310 | * @return $this |
||
| 311 | */ |
||
| 312 | 5 | public function includesReferenceTo($document) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Gets the Query executable. |
||
| 320 | * |
||
| 321 | * @param array $options |
||
| 322 | * @return Query $query |
||
| 323 | */ |
||
| 324 | 185 | public function getQuery(array $options = array()) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Create a new Expr instance that can be used as an expression with the Builder |
||
| 385 | * |
||
| 386 | * @return Expr $expr |
||
| 387 | */ |
||
| 388 | 25 | public function expr() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @param string[]|string $documentName an array of document names or just one. |
||
| 398 | */ |
||
| 399 | 215 | private function setDocumentName($documentName) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Get Discriminator Values |
||
| 428 | * |
||
| 429 | * @param \Iterator|array $classNames |
||
| 430 | * @return array an array of discriminatorValues (mixed type) |
||
| 431 | * @throws \InvalidArgumentException if the number of found collections > 1 |
||
| 432 | */ |
||
| 433 | 2 | private function getDiscriminatorValues($classNames) |
|
| 448 | } |
||
| 449 |
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 sub-classes 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 parent class: