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 HydratorFactory 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 HydratorFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class HydratorFactory |
||
40 | { |
||
41 | /** |
||
42 | * The DocumentManager this factory is bound to. |
||
43 | * |
||
44 | * @var \Doctrine\ODM\MongoDB\DocumentManager |
||
45 | */ |
||
46 | private $dm; |
||
47 | |||
48 | /** |
||
49 | * The UnitOfWork used to coordinate object-level transactions. |
||
50 | * |
||
51 | * @var \Doctrine\ODM\MongoDB\UnitOfWork |
||
52 | */ |
||
53 | private $unitOfWork; |
||
54 | |||
55 | /** |
||
56 | * The EventManager associated with this Hydrator |
||
57 | * |
||
58 | * @var \Doctrine\Common\EventManager |
||
59 | */ |
||
60 | private $evm; |
||
61 | |||
62 | /** |
||
63 | * Which algorithm to use to automatically (re)generate hydrator classes. |
||
64 | * |
||
65 | * @var integer |
||
66 | */ |
||
67 | private $autoGenerate; |
||
68 | |||
69 | /** |
||
70 | * The namespace that contains all hydrator classes. |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $hydratorNamespace; |
||
75 | |||
76 | /** |
||
77 | * The directory that contains all hydrator classes. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $hydratorDir; |
||
82 | |||
83 | /** |
||
84 | * Array of instantiated document hydrators. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | private $hydrators = array(); |
||
89 | |||
90 | /** |
||
91 | * @param DocumentManager $dm |
||
92 | * @param EventManager $evm |
||
93 | * @param string $hydratorDir |
||
94 | * @param string $hydratorNs |
||
95 | * @param integer $autoGenerate |
||
96 | * @throws HydratorException |
||
97 | */ |
||
98 | 1114 | public function __construct(DocumentManager $dm, EventManager $evm, $hydratorDir, $hydratorNs, $autoGenerate) |
|
112 | |||
113 | /** |
||
114 | * Sets the UnitOfWork instance. |
||
115 | * |
||
116 | * @param UnitOfWork $uow |
||
117 | */ |
||
118 | 1114 | public function setUnitOfWork(UnitOfWork $uow) |
|
122 | |||
123 | /** |
||
124 | * Gets the hydrator object for the given document class. |
||
125 | * |
||
126 | * @param string $className |
||
127 | * @return \Doctrine\ODM\MongoDB\Hydrator\HydratorInterface $hydrator |
||
128 | */ |
||
129 | 412 | public function getHydratorFor($className) |
|
130 | { |
||
131 | 412 | if (isset($this->hydrators[$className])) { |
|
132 | 211 | return $this->hydrators[$className]; |
|
133 | } |
||
134 | 412 | $hydratorClassName = str_replace('\\', '', $className) . 'Hydrator'; |
|
135 | 412 | $fqn = $this->hydratorNamespace . '\\' . $hydratorClassName; |
|
136 | 412 | $class = $this->dm->getClassMetadata($className); |
|
137 | |||
138 | 412 | View Code Duplication | if ( ! class_exists($fqn, false)) { |
|
|||
139 | 182 | $fileName = $this->hydratorDir . DIRECTORY_SEPARATOR . $hydratorClassName . '.php'; |
|
140 | 182 | switch ($this->autoGenerate) { |
|
141 | case Configuration::AUTOGENERATE_NEVER: |
||
142 | require $fileName; |
||
143 | break; |
||
144 | |||
145 | case Configuration::AUTOGENERATE_ALWAYS: |
||
146 | 182 | $this->generateHydratorClass($class, $hydratorClassName, $fileName); |
|
147 | 182 | require $fileName; |
|
148 | 182 | break; |
|
149 | |||
150 | case Configuration::AUTOGENERATE_FILE_NOT_EXISTS: |
||
151 | if (!file_exists($fileName)) { |
||
152 | $this->generateHydratorClass($class, $hydratorClassName, $fileName); |
||
153 | } |
||
154 | require $fileName; |
||
155 | break; |
||
156 | |||
157 | case Configuration::AUTOGENERATE_EVAL: |
||
158 | $this->generateHydratorClass($class, $hydratorClassName, false); |
||
159 | break; |
||
160 | } |
||
161 | } |
||
162 | 412 | $this->hydrators[$className] = new $fqn($this->dm, $this->unitOfWork, $class); |
|
163 | 412 | return $this->hydrators[$className]; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Generates hydrator classes for all given classes. |
||
168 | * |
||
169 | * @param array $classes The classes (ClassMetadata instances) for which to generate hydrators. |
||
170 | * @param string $toDir The target directory of the hydrator classes. If not specified, the |
||
171 | * directory configured on the Configuration of the DocumentManager used |
||
172 | * by this factory is used. |
||
173 | */ |
||
174 | public function generateHydratorClasses(array $classes, $toDir = null) |
||
184 | |||
185 | /** |
||
186 | * @param ClassMetadata $class |
||
187 | * @param string $hydratorClassName |
||
188 | * @param string $fileName |
||
189 | */ |
||
190 | 182 | private function generateHydratorClass(ClassMetadata $class, $hydratorClassName, $fileName) |
|
191 | { |
||
192 | 182 | $code = ''; |
|
193 | |||
194 | 182 | foreach ($class->fieldMappings as $fieldName => $mapping) { |
|
195 | 182 | if (isset($mapping['alsoLoadFields'])) { |
|
196 | 5 | foreach ($mapping['alsoLoadFields'] as $name) { |
|
197 | 5 | $code .= sprintf(<<<EOF |
|
198 | |||
199 | 5 | /** @AlsoLoad("$name") */ |
|
200 | 5 | if (!array_key_exists('%1\$s', \$data) && array_key_exists('$name', \$data)) { |
|
201 | 5 | \$data['%1\$s'] = \$data['$name']; |
|
202 | } |
||
203 | |||
204 | EOF |
||
205 | , |
||
206 | 5 | $mapping['name'] |
|
207 | ); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | 182 | if ($mapping['type'] === 'date') { |
|
212 | 9 | $code .= sprintf(<<<EOF |
|
213 | |||
214 | /** @Field(type="date") */ |
||
215 | if (isset(\$data['%1\$s'])) { |
||
216 | \$value = \$data['%1\$s']; |
||
217 | %3\$s |
||
218 | \$this->class->reflFields['%2\$s']->setValue(\$document, clone \$return); |
||
219 | \$hydratedData['%2\$s'] = \$return; |
||
220 | } |
||
221 | |||
222 | EOF |
||
223 | , |
||
224 | 9 | $mapping['name'], |
|
225 | 9 | $mapping['fieldName'], |
|
226 | 9 | Type::getType($mapping['type'])->closureToPHP() |
|
227 | ); |
||
228 | |||
229 | |||
230 | 182 | } elseif ( ! isset($mapping['association'])) { |
|
231 | 182 | $code .= sprintf(<<<EOF |
|
232 | |||
233 | 182 | /** @Field(type="{$mapping['type']}") */ |
|
234 | if (isset(\$data['%1\$s']) || (! empty(\$this->class->fieldMappings['%2\$s']['nullable']) && array_key_exists('%1\$s', \$data))) { |
||
235 | \$value = \$data['%1\$s']; |
||
236 | if (\$value !== null) { |
||
237 | \$typeIdentifier = \$this->class->fieldMappings['%2\$s']['type']; |
||
238 | %3\$s |
||
239 | } else { |
||
240 | \$return = null; |
||
241 | } |
||
242 | \$this->class->reflFields['%2\$s']->setValue(\$document, \$return); |
||
243 | \$hydratedData['%2\$s'] = \$return; |
||
244 | } |
||
245 | |||
246 | EOF |
||
247 | , |
||
248 | 182 | $mapping['name'], |
|
249 | 182 | $mapping['fieldName'], |
|
250 | 182 | Type::getType($mapping['type'])->closureToPHP() |
|
251 | ); |
||
252 | 122 | View Code Duplication | } elseif ($mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isOwningSide']) { |
253 | 50 | $code .= sprintf(<<<EOF |
|
254 | |||
255 | /** @ReferenceOne */ |
||
256 | if (isset(\$data['%1\$s'])) { |
||
257 | \$reference = \$data['%1\$s']; |
||
258 | if (isset(\$this->class->fieldMappings['%2\$s']['storeAs']) && \$this->class->fieldMappings['%2\$s']['storeAs'] === ClassMetadataInfo::REFERENCE_STORE_AS_ID) { |
||
259 | \$className = \$this->class->fieldMappings['%2\$s']['targetDocument']; |
||
260 | \$mongoId = \$reference; |
||
261 | } else { |
||
262 | \$className = \$this->unitOfWork->getClassNameForAssociation(\$this->class->fieldMappings['%2\$s'], \$reference); |
||
263 | \$mongoId = \$reference['\$id']; |
||
264 | } |
||
265 | \$targetMetadata = \$this->dm->getClassMetadata(\$className); |
||
266 | \$id = \$targetMetadata->getPHPIdentifierValue(\$mongoId); |
||
267 | \$return = \$this->dm->getReference(\$className, \$id); |
||
268 | \$this->class->reflFields['%2\$s']->setValue(\$document, \$return); |
||
269 | \$hydratedData['%2\$s'] = \$return; |
||
270 | } |
||
271 | |||
272 | EOF |
||
273 | , |
||
274 | 50 | $mapping['name'], |
|
275 | 50 | $mapping['fieldName'] |
|
276 | ); |
||
277 | 104 | } elseif ($mapping['association'] === ClassMetadata::REFERENCE_ONE && $mapping['isInverseSide']) { |
|
278 | 6 | if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) { |
|
279 | 1 | $code .= sprintf(<<<EOF |
|
280 | |||
281 | \$className = \$this->class->fieldMappings['%2\$s']['targetDocument']; |
||
282 | \$return = \$this->dm->getRepository(\$className)->%3\$s(\$document); |
||
283 | \$this->class->reflFields['%2\$s']->setValue(\$document, \$return); |
||
284 | \$hydratedData['%2\$s'] = \$return; |
||
285 | |||
286 | EOF |
||
287 | , |
||
288 | 1 | $mapping['name'], |
|
289 | 1 | $mapping['fieldName'], |
|
290 | 1 | $mapping['repositoryMethod'] |
|
291 | ); |
||
292 | } else { |
||
293 | 6 | $code .= sprintf(<<<EOF |
|
294 | |||
295 | \$mapping = \$this->class->fieldMappings['%2\$s']; |
||
296 | \$className = \$mapping['targetDocument']; |
||
297 | \$targetClass = \$this->dm->getClassMetadata(\$mapping['targetDocument']); |
||
298 | \$mappedByMapping = \$targetClass->fieldMappings[\$mapping['mappedBy']]; |
||
299 | \$mappedByFieldName = isset(\$mappedByMapping['storeAs']) && \$mappedByMapping['storeAs'] === ClassMetadataInfo::REFERENCE_STORE_AS_ID ? \$mapping['mappedBy'] : \$mapping['mappedBy'].'.\$id'; |
||
300 | \$criteria = array_merge( |
||
301 | array(\$mappedByFieldName => \$data['_id']), |
||
302 | isset(\$this->class->fieldMappings['%2\$s']['criteria']) ? \$this->class->fieldMappings['%2\$s']['criteria'] : array() |
||
303 | ); |
||
304 | \$sort = isset(\$this->class->fieldMappings['%2\$s']['sort']) ? \$this->class->fieldMappings['%2\$s']['sort'] : array(); |
||
305 | \$return = \$this->unitOfWork->getDocumentPersister(\$className)->load(\$criteria, null, array(), 0, \$sort); |
||
306 | \$this->class->reflFields['%2\$s']->setValue(\$document, \$return); |
||
307 | \$hydratedData['%2\$s'] = \$return; |
||
308 | |||
309 | EOF |
||
310 | , |
||
311 | 6 | $mapping['name'], |
|
312 | 6 | $mapping['fieldName'] |
|
313 | ); |
||
314 | } |
||
315 | 103 | View Code Duplication | } elseif ($mapping['association'] === ClassMetadata::REFERENCE_MANY || $mapping['association'] === ClassMetadata::EMBED_MANY) { |
316 | 82 | $code .= sprintf(<<<EOF |
|
317 | |||
318 | /** @Many */ |
||
319 | \$mongoData = isset(\$data['%1\$s']) ? \$data['%1\$s'] : null; |
||
320 | \$return = \$this->dm->getConfiguration()->getPersistentCollectionFactory()->create(\$this->dm, \$this->class->fieldMappings['%2\$s']); |
||
321 | \$return->setHints(\$hints); |
||
322 | \$return->setOwner(\$document, \$this->class->fieldMappings['%2\$s']); |
||
323 | \$return->setInitialized(false); |
||
324 | if (\$mongoData) { |
||
325 | \$return->setMongoData(\$mongoData); |
||
326 | } |
||
327 | \$this->class->reflFields['%2\$s']->setValue(\$document, \$return); |
||
328 | \$hydratedData['%2\$s'] = \$return; |
||
329 | |||
330 | EOF |
||
331 | , |
||
332 | 82 | $mapping['name'], |
|
333 | 82 | $mapping['fieldName'] |
|
334 | ); |
||
335 | 45 | } elseif ($mapping['association'] === ClassMetadata::EMBED_ONE) { |
|
336 | 45 | $code .= sprintf(<<<EOF |
|
337 | |||
338 | /** @EmbedOne */ |
||
339 | if (isset(\$data['%1\$s'])) { |
||
340 | \$embeddedDocument = \$data['%1\$s']; |
||
341 | \$className = \$this->unitOfWork->getClassNameForAssociation(\$this->class->fieldMappings['%2\$s'], \$embeddedDocument); |
||
342 | \$embeddedMetadata = \$this->dm->getClassMetadata(\$className); |
||
343 | \$return = \$embeddedMetadata->newInstance(); |
||
344 | |||
345 | \$this->unitOfWork->setParentAssociation(\$return, \$this->class->fieldMappings['%2\$s'], \$document, '%1\$s'); |
||
346 | |||
347 | \$embeddedData = \$this->dm->getHydratorFactory()->hydrate(\$return, \$embeddedDocument, \$hints); |
||
348 | \$embeddedId = \$embeddedMetadata->identifier && isset(\$embeddedData[\$embeddedMetadata->identifier]) ? \$embeddedData[\$embeddedMetadata->identifier] : null; |
||
349 | |||
350 | if (empty(\$hints[Query::HINT_READ_ONLY])) { |
||
351 | \$this->unitOfWork->registerManaged(\$return, \$embeddedId, \$embeddedData); |
||
352 | } |
||
353 | |||
354 | \$this->class->reflFields['%2\$s']->setValue(\$document, \$return); |
||
355 | \$hydratedData['%2\$s'] = \$return; |
||
356 | } |
||
357 | |||
358 | EOF |
||
359 | , |
||
360 | 45 | $mapping['name'], |
|
361 | 182 | $mapping['fieldName'] |
|
362 | ); |
||
363 | } |
||
364 | } |
||
365 | |||
366 | 182 | $namespace = $this->hydratorNamespace; |
|
367 | 182 | $code = sprintf(<<<EOF |
|
368 | <?php |
||
369 | |||
370 | 182 | namespace $namespace; |
|
371 | |||
372 | use Doctrine\ODM\MongoDB\DocumentManager; |
||
373 | use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
||
374 | use Doctrine\ODM\MongoDB\Hydrator\HydratorInterface; |
||
375 | use Doctrine\ODM\MongoDB\Query\Query; |
||
376 | use Doctrine\ODM\MongoDB\UnitOfWork; |
||
377 | use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo; |
||
378 | |||
379 | /** |
||
380 | * THIS CLASS WAS GENERATED BY THE DOCTRINE ODM. DO NOT EDIT THIS FILE. |
||
381 | */ |
||
382 | 182 | class $hydratorClassName implements HydratorInterface |
|
383 | { |
||
384 | private \$dm; |
||
385 | private \$unitOfWork; |
||
386 | private \$class; |
||
387 | |||
388 | public function __construct(DocumentManager \$dm, UnitOfWork \$uow, ClassMetadata \$class) |
||
389 | { |
||
390 | \$this->dm = \$dm; |
||
391 | \$this->unitOfWork = \$uow; |
||
392 | \$this->class = \$class; |
||
393 | } |
||
394 | |||
395 | public function hydrate(\$document, \$data, array \$hints = array()) |
||
396 | { |
||
397 | \$hydratedData = array(); |
||
398 | %s return \$hydratedData; |
||
399 | } |
||
400 | } |
||
401 | EOF |
||
402 | , |
||
403 | 182 | $code |
|
404 | ); |
||
405 | |||
406 | 182 | if ($fileName === false) { |
|
407 | if ( ! class_exists($namespace . '\\' . $hydratorClassName)) { |
||
408 | eval(substr($code, 5)); |
||
409 | } |
||
410 | View Code Duplication | } else { |
|
411 | 182 | $parentDirectory = dirname($fileName); |
|
412 | |||
413 | 182 | if ( ! is_dir($parentDirectory) && (false === @mkdir($parentDirectory, 0775, true))) { |
|
414 | throw HydratorException::hydratorDirectoryNotWritable(); |
||
415 | } |
||
416 | |||
417 | 182 | if ( ! is_writable($parentDirectory)) { |
|
418 | throw HydratorException::hydratorDirectoryNotWritable(); |
||
419 | } |
||
420 | |||
421 | 182 | $tmpFileName = $fileName . '.' . uniqid('', true); |
|
422 | 182 | file_put_contents($tmpFileName, $code); |
|
423 | 182 | rename($tmpFileName, $fileName); |
|
424 | 182 | chmod($fileName, 0664); |
|
425 | } |
||
426 | 182 | } |
|
427 | |||
428 | /** |
||
429 | * Hydrate array of MongoDB document data into the given document object. |
||
430 | * |
||
431 | * @param object $document The document object to hydrate the data into. |
||
432 | * @param array $data The array of document data. |
||
433 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
434 | * @return array $values The array of hydrated values. |
||
435 | */ |
||
436 | 412 | public function hydrate($document, $data, array $hints = array()) |
|
485 | } |
||
486 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.