Complex classes like PersistenceBuilder 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 PersistenceBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class PersistenceBuilder |
||
26 | { |
||
27 | /** |
||
28 | * The DocumentManager instance. |
||
29 | * |
||
30 | * @var DocumentManager |
||
31 | */ |
||
32 | private $dm; |
||
33 | |||
34 | /** |
||
35 | * The UnitOfWork instance. |
||
36 | * |
||
37 | * @var UnitOfWork |
||
38 | */ |
||
39 | private $uow; |
||
40 | |||
41 | /** |
||
42 | * Initializes a new PersistenceBuilder instance. |
||
43 | */ |
||
44 | 1123 | public function __construct(DocumentManager $dm, UnitOfWork $uow) |
|
49 | |||
50 | /** |
||
51 | * Prepares the array that is ready to be inserted to mongodb for a given object document. |
||
52 | * |
||
53 | * @param object $document |
||
54 | * |
||
55 | * @return array $insertData |
||
56 | */ |
||
57 | 515 | public function prepareInsertData($document) |
|
111 | |||
112 | /** |
||
113 | * Prepares the update query to update a given document object in mongodb. |
||
114 | * |
||
115 | * @param object $document |
||
116 | * |
||
117 | * @return array $updateData |
||
118 | */ |
||
119 | 220 | public function prepareUpdateData($document) |
|
219 | |||
220 | /** |
||
221 | * Prepares the update query to upsert a given document object in mongodb. |
||
222 | * |
||
223 | * @param object $document |
||
224 | * |
||
225 | * @return array $updateData |
||
226 | */ |
||
227 | 86 | public function prepareUpsertData($document) |
|
298 | |||
299 | /** |
||
300 | * Returns the reference representation to be stored in MongoDB. |
||
301 | * |
||
302 | * If the document does not have an identifier and the mapping calls for a |
||
303 | * simple reference, null may be returned. |
||
304 | * |
||
305 | * @param array $referenceMapping |
||
306 | * @param object $document |
||
307 | * |
||
308 | * @return array|null |
||
309 | */ |
||
310 | 212 | public function prepareReferencedDocumentValue(array $referenceMapping, $document) |
|
314 | |||
315 | /** |
||
316 | * Returns the embedded document to be stored in MongoDB. |
||
317 | * |
||
318 | * The return value will usually be an associative array with string keys |
||
319 | * corresponding to field names on the embedded document. An object may be |
||
320 | * returned if the document is empty, to ensure that a BSON object will be |
||
321 | * stored in lieu of an array. |
||
322 | * |
||
323 | * If $includeNestedCollections is true, nested collections will be included |
||
324 | * in this prepared value and the option will cascade to all embedded |
||
325 | * associations. If any nested PersistentCollections (embed or reference) |
||
326 | * within this value were previously scheduled for deletion or update, they |
||
327 | * will also be unscheduled. |
||
328 | * |
||
329 | * @param array $embeddedMapping |
||
330 | * @param object $embeddedDocument |
||
331 | * @param bool $includeNestedCollections |
||
332 | * |
||
333 | * @return array|object |
||
334 | * |
||
335 | * @throws UnexpectedValueException If an unsupported associating mapping is found. |
||
336 | */ |
||
337 | 179 | public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDocument, $includeNestedCollections = false) |
|
338 | { |
||
339 | 179 | $embeddedDocumentValue = []; |
|
340 | 179 | $class = $this->dm->getClassMetadata(get_class($embeddedDocument)); |
|
341 | |||
342 | 179 | foreach ($class->fieldMappings as $mapping) { |
|
343 | // Skip notSaved fields |
||
344 | 177 | if (! empty($mapping['notSaved'])) { |
|
345 | 1 | continue; |
|
346 | } |
||
347 | |||
348 | // Inline ClassMetadata::getFieldValue() |
||
349 | 177 | $rawValue = $class->reflFields[$mapping['fieldName']]->getValue($embeddedDocument); |
|
350 | |||
351 | 177 | $value = null; |
|
352 | |||
353 | 177 | if ($rawValue !== null) { |
|
354 | 174 | switch ($mapping['association'] ?? null) { |
|
355 | // @Field, @String, @Date, etc. |
||
356 | case null: |
||
357 | 168 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($rawValue); |
|
358 | 168 | break; |
|
359 | |||
360 | 65 | case ClassMetadata::EMBED_ONE: |
|
361 | 62 | case ClassMetadata::REFERENCE_ONE: |
|
362 | // Nested collections should only be included for embedded relationships |
||
363 | 21 | $value = $this->prepareAssociatedDocumentValue($mapping, $rawValue, $includeNestedCollections && isset($mapping['embedded'])); |
|
364 | 21 | break; |
|
365 | |||
366 | 46 | case ClassMetadata::EMBED_MANY: |
|
367 | 4 | case ClassMetadata::REFERENCE_MANY: |
|
368 | // Skip PersistentCollections already scheduled for deletion |
||
369 | 46 | if (! $includeNestedCollections && $rawValue instanceof PersistentCollectionInterface |
|
370 | 46 | && $this->uow->isCollectionScheduledForDeletion($rawValue)) { |
|
371 | break; |
||
372 | } |
||
373 | |||
374 | 46 | $value = $this->prepareAssociatedCollectionValue($rawValue, $includeNestedCollections); |
|
375 | 46 | break; |
|
376 | |||
377 | default: |
||
378 | throw new UnexpectedValueException('Unsupported mapping association: ' . $mapping['association']); |
||
379 | } |
||
380 | } |
||
381 | |||
382 | // Omit non-nullable fields that would have a null value |
||
383 | 177 | if ($value === null && $mapping['nullable'] === false) { |
|
384 | 63 | continue; |
|
385 | } |
||
386 | |||
387 | 174 | $embeddedDocumentValue[$mapping['name']] = $value; |
|
388 | } |
||
389 | |||
390 | /* Add a discriminator value if the embedded document is not mapped |
||
391 | * explicitly to a targetDocument class. |
||
392 | */ |
||
393 | 179 | if (! isset($embeddedMapping['targetDocument'])) { |
|
394 | 16 | $discriminatorField = $embeddedMapping['discriminatorField']; |
|
395 | 16 | if (! empty($embeddedMapping['discriminatorMap'])) { |
|
396 | 5 | $discriminatorValue = array_search($class->name, $embeddedMapping['discriminatorMap']); |
|
397 | |||
398 | 5 | if ($discriminatorValue === false) { |
|
399 | 5 | throw MappingException::unlistedClassInDiscriminatorMap($class->name); |
|
400 | } |
||
401 | } else { |
||
402 | 11 | $discriminatorValue = $class->name; |
|
403 | } |
||
404 | |||
405 | 15 | $embeddedDocumentValue[$discriminatorField] = $discriminatorValue; |
|
406 | } |
||
407 | |||
408 | /* If the class has a discriminator (field and value), use it. A child |
||
409 | * class that is not defined in the discriminator map may only have a |
||
410 | * discriminator field and no value, so default to the full class name. |
||
411 | */ |
||
412 | 178 | if (isset($class->discriminatorField)) { |
|
413 | 8 | if ($class->discriminatorValue === null) { |
|
414 | 4 | if (! empty($class->discriminatorMap)) { |
|
415 | 4 | throw MappingException::unlistedClassInDiscriminatorMap($class->name); |
|
416 | } |
||
417 | $class->discriminatorValue = $class->name; |
||
418 | } |
||
419 | 6 | $embeddedDocumentValue[$class->discriminatorField] = $class->discriminatorValue; |
|
420 | } |
||
421 | |||
422 | // Ensure empty embedded documents are stored as BSON objects |
||
423 | 176 | if (empty($embeddedDocumentValue)) { |
|
424 | 6 | return (object) $embeddedDocumentValue; |
|
425 | } |
||
426 | |||
427 | /* @todo Consider always casting the return value to an object, or |
||
428 | * building $embeddedDocumentValue as an object instead of an array, to |
||
429 | * handle the edge case where all database field names are sequential, |
||
430 | * numeric keys. |
||
431 | */ |
||
432 | 172 | return $embeddedDocumentValue; |
|
433 | } |
||
434 | |||
435 | /** |
||
436 | * Returns the embedded document or reference representation to be stored. |
||
437 | * |
||
438 | * @param array $mapping |
||
439 | * @param object $document |
||
440 | * @param bool $includeNestedCollections |
||
441 | * |
||
442 | * @return array|object|null |
||
443 | * |
||
444 | * @throws InvalidArgumentException If the mapping is neither embedded nor reference. |
||
445 | */ |
||
446 | 21 | public function prepareAssociatedDocumentValue(array $mapping, $document, $includeNestedCollections = false) |
|
458 | |||
459 | /** |
||
460 | * Returns the collection representation to be stored and unschedules it afterwards. |
||
461 | * |
||
462 | * @param bool $includeNestedCollections |
||
463 | * |
||
464 | * @return array |
||
465 | */ |
||
466 | 225 | public function prepareAssociatedCollectionValue(PersistentCollectionInterface $coll, $includeNestedCollections = false) |
|
488 | } |
||
489 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.