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 |
||
| 27 | final class PersistenceBuilder |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * The DocumentManager instance. |
||
| 31 | * |
||
| 32 | * @var DocumentManager |
||
| 33 | */ |
||
| 34 | private $dm; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The UnitOfWork instance. |
||
| 38 | * |
||
| 39 | * @var UnitOfWork |
||
| 40 | */ |
||
| 41 | private $uow; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Initializes a new PersistenceBuilder instance. |
||
| 45 | */ |
||
| 46 | 1212 | public function __construct(DocumentManager $dm, UnitOfWork $uow) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Prepares the array that is ready to be inserted to mongodb for a given object document. |
||
| 54 | * |
||
| 55 | * @param object $document |
||
| 56 | * |
||
| 57 | * @return array $insertData |
||
| 58 | */ |
||
| 59 | 543 | public function prepareInsertData($document) |
|
| 60 | { |
||
| 61 | 543 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 62 | 543 | $changeset = $this->uow->getDocumentChangeSet($document); |
|
| 63 | |||
| 64 | 543 | $insertData = []; |
|
| 65 | 543 | foreach ($class->fieldMappings as $mapping) { |
|
| 66 | 543 | $new = $changeset[$mapping['fieldName']][1] ?? null; |
|
| 67 | |||
| 68 | 543 | if ($new === null && $mapping['nullable']) { |
|
| 69 | 162 | $insertData[$mapping['name']] = null; |
|
| 70 | } |
||
| 71 | |||
| 72 | /* Nothing more to do for null values, since we're either storing |
||
| 73 | * them (if nullable was true) or not. |
||
| 74 | */ |
||
| 75 | 543 | if ($new === null) { |
|
| 76 | 368 | continue; |
|
| 77 | } |
||
| 78 | |||
| 79 | // @Field, @String, @Date, etc. |
||
| 80 | 543 | if (! isset($mapping['association'])) { |
|
| 81 | 543 | $insertData[$mapping['name']] = Type::getType($mapping['type'])->convertToDatabaseValue($new); |
|
| 82 | |||
| 83 | // @ReferenceOne |
||
| 84 | 427 | } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 85 | 108 | $insertData[$mapping['name']] = $this->prepareReferencedDocumentValue($mapping, $new); |
|
| 86 | |||
| 87 | // @EmbedOne |
||
| 88 | 394 | } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) { |
|
| 89 | 60 | $insertData[$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new); |
|
| 90 | |||
| 91 | // @ReferenceMany, @EmbedMany |
||
| 92 | // We're excluding collections using addToSet since there is a risk |
||
| 93 | // of duplicated entries stored in the collection |
||
| 94 | 376 | } elseif ($mapping['type'] === ClassMetadata::MANY && ! $mapping['isInverseSide'] |
|
| 95 | 376 | && $mapping['strategy'] !== ClassMetadata::STORAGE_STRATEGY_ADD_TO_SET && ! $new->isEmpty()) { |
|
| 96 | 216 | $insertData[$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true); |
|
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | // add discriminator if the class has one |
||
| 101 | 530 | if (isset($class->discriminatorField)) { |
|
| 102 | 30 | $discriminatorValue = $class->discriminatorValue; |
|
| 103 | |||
| 104 | 30 | if ($discriminatorValue === null) { |
|
| 105 | 7 | if (! empty($class->discriminatorMap)) { |
|
| 106 | 4 | throw MappingException::unlistedClassInDiscriminatorMap($class->name); |
|
| 107 | } |
||
| 108 | |||
| 109 | 3 | $discriminatorValue = $class->name; |
|
| 110 | } |
||
| 111 | |||
| 112 | 29 | $insertData[$class->discriminatorField] = $discriminatorValue; |
|
| 113 | } |
||
| 114 | |||
| 115 | 530 | return $insertData; |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Prepares the update query to update a given document object in mongodb. |
||
| 120 | * |
||
| 121 | * @param object $document |
||
| 122 | * |
||
| 123 | * @return array $updateData |
||
| 124 | */ |
||
| 125 | 235 | public function prepareUpdateData($document) |
|
| 126 | { |
||
| 127 | 235 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 128 | 235 | $changeset = $this->uow->getDocumentChangeSet($document); |
|
| 129 | |||
| 130 | 235 | $updateData = []; |
|
| 131 | 235 | foreach ($changeset as $fieldName => $change) { |
|
| 132 | 234 | $mapping = $class->fieldMappings[$fieldName]; |
|
| 133 | |||
| 134 | // skip non embedded document identifiers |
||
| 135 | 234 | if (! $class->isEmbeddedDocument && ! empty($mapping['id'])) { |
|
| 136 | 2 | continue; |
|
| 137 | } |
||
| 138 | |||
| 139 | 233 | [$old, $new] = $change; |
|
| 140 | |||
| 141 | // Scalar fields |
||
| 142 | 233 | if (! isset($mapping['association'])) { |
|
| 143 | 122 | if ($new === null && $mapping['nullable'] !== true) { |
|
| 144 | 2 | $updateData['$unset'][$mapping['name']] = true; |
|
| 145 | } else { |
||
| 146 | 121 | if ($new !== null && isset($mapping['strategy']) && $mapping['strategy'] === ClassMetadata::STORAGE_STRATEGY_INCREMENT) { |
|
| 147 | 4 | $operator = '$inc'; |
|
| 148 | 4 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($new - $old); |
|
| 149 | } else { |
||
| 150 | 118 | $operator = '$set'; |
|
| 151 | 118 | $value = $new === null ? null : Type::getType($mapping['type'])->convertToDatabaseValue($new); |
|
| 152 | } |
||
| 153 | |||
| 154 | 122 | $updateData[$operator][$mapping['name']] = $value; |
|
| 155 | } |
||
| 156 | |||
| 157 | // @EmbedOne |
||
| 158 | 153 | } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) { |
|
| 159 | // If we have a new embedded document then lets set the whole thing |
||
| 160 | 29 | if ($new && $this->uow->isScheduledForInsert($new)) { |
|
| 161 | 10 | $updateData['$set'][$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new); |
|
| 162 | |||
| 163 | // If we don't have a new value then lets unset the embedded document |
||
| 164 | 22 | } elseif (! $new) { |
|
| 165 | 3 | $updateData['$unset'][$mapping['name']] = true; |
|
| 166 | |||
| 167 | // Update existing embedded document |
||
| 168 | } else { |
||
| 169 | 19 | $update = $this->prepareUpdateData($new); |
|
| 170 | 29 | foreach ($update as $cmd => $values) { |
|
| 171 | 15 | foreach ($values as $key => $value) { |
|
| 172 | 15 | $updateData[$cmd][$mapping['name'] . '.' . $key] = $value; |
|
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | // @ReferenceMany, @EmbedMany |
||
| 178 | 136 | } elseif (isset($mapping['association']) && $mapping['type'] === 'many' && $new) { |
|
| 179 | 126 | if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($new)) { |
|
| 180 | 20 | $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true); |
|
| 181 | 108 | } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($new)) { |
|
| 182 | 2 | $updateData['$unset'][$mapping['name']] = true; |
|
| 183 | 2 | $this->uow->unscheduleCollectionDeletion($new); |
|
| 184 | 106 | } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($old)) { |
|
| 185 | 2 | $updateData['$unset'][$mapping['name']] = true; |
|
| 186 | 2 | $this->uow->unscheduleCollectionDeletion($old); |
|
| 187 | 104 | } elseif ($mapping['association'] === ClassMetadata::EMBED_MANY) { |
|
| 188 | 126 | foreach ($new as $key => $embeddedDoc) { |
|
| 189 | 58 | if ($this->uow->isScheduledForInsert($embeddedDoc)) { |
|
| 190 | 42 | continue; |
|
| 191 | } |
||
| 192 | |||
| 193 | 45 | $update = $this->prepareUpdateData($embeddedDoc); |
|
| 194 | 45 | foreach ($update as $cmd => $values) { |
|
| 195 | 14 | foreach ($values as $name => $value) { |
|
| 196 | 14 | $updateData[$cmd][$mapping['name'] . '.' . $key . '.' . $name] = $value; |
|
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | // @ReferenceOne |
||
| 203 | 16 | } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 204 | 12 | if (isset($new) || $mapping['nullable'] === true) { |
|
| 205 | 12 | $updateData['$set'][$mapping['name']] = $new === null ? null : $this->prepareReferencedDocumentValue($mapping, $new); |
|
| 206 | } else { |
||
| 207 | 2 | $updateData['$unset'][$mapping['name']] = true; |
|
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | // collections that aren't dirty but could be subject to update are |
||
| 212 | // excluded from change set, let's go through them now |
||
| 213 | 235 | foreach ($this->uow->getScheduledCollections($document) as $coll) { |
|
| 214 | 105 | $mapping = $coll->getMapping(); |
|
| 215 | 105 | if (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForUpdate($coll)) { |
|
| 216 | 3 | $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($coll, true); |
|
| 217 | 102 | } elseif (CollectionHelper::isAtomic($mapping['strategy']) && $this->uow->isCollectionScheduledForDeletion($coll)) { |
|
| 218 | 1 | $updateData['$unset'][$mapping['name']] = true; |
|
| 219 | 1 | $this->uow->unscheduleCollectionDeletion($coll); |
|
| 220 | } |
||
| 221 | // @ReferenceMany is handled by CollectionPersister |
||
| 222 | } |
||
| 223 | |||
| 224 | 235 | return $updateData; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Prepares the update query to upsert a given document object in mongodb. |
||
| 229 | * |
||
| 230 | * @param object $document |
||
| 231 | * |
||
| 232 | * @return array $updateData |
||
| 233 | */ |
||
| 234 | 86 | public function prepareUpsertData($document) |
|
| 235 | { |
||
| 236 | 86 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 237 | 86 | $changeset = $this->uow->getDocumentChangeSet($document); |
|
| 238 | |||
| 239 | 86 | $updateData = []; |
|
| 240 | 86 | foreach ($changeset as $fieldName => $change) { |
|
| 241 | 86 | $mapping = $class->fieldMappings[$fieldName]; |
|
| 242 | |||
| 243 | 86 | [$old, $new] = $change; |
|
| 244 | |||
| 245 | // Scalar fields |
||
| 246 | 86 | if (! isset($mapping['association'])) { |
|
| 247 | 86 | if ($new !== null) { |
|
| 248 | 86 | if (empty($mapping['id']) && isset($mapping['strategy']) && $mapping['strategy'] === ClassMetadata::STORAGE_STRATEGY_INCREMENT) { |
|
| 249 | 3 | $operator = '$inc'; |
|
| 250 | 3 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($new - $old); |
|
| 251 | } else { |
||
| 252 | 86 | $operator = '$set'; |
|
| 253 | 86 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($new); |
|
| 254 | } |
||
| 255 | |||
| 256 | 86 | $updateData[$operator][$mapping['name']] = $value; |
|
| 257 | 11 | } elseif ($mapping['nullable'] === true) { |
|
| 258 | 86 | $updateData['$setOnInsert'][$mapping['name']] = null; |
|
| 259 | } |
||
| 260 | |||
| 261 | // @EmbedOne |
||
| 262 | 27 | } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) { |
|
| 263 | // If we don't have a new value then do nothing on upsert |
||
| 264 | // If we have a new embedded document then lets set the whole thing |
||
| 265 | 8 | if ($new && $this->uow->isScheduledForInsert($new)) { |
|
| 266 | 5 | $updateData['$set'][$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new); |
|
| 267 | 3 | } elseif ($new) { |
|
| 268 | // Update existing embedded document |
||
| 269 | $update = $this->prepareUpsertData($new); |
||
| 270 | 8 | foreach ($update as $cmd => $values) { |
|
| 271 | foreach ($values as $key => $value) { |
||
| 272 | $updateData[$cmd][$mapping['name'] . '.' . $key] = $value; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | // @ReferenceOne |
||
| 278 | 24 | } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 279 | 13 | if (isset($new) || $mapping['nullable'] === true) { |
|
| 280 | 13 | $updateData['$set'][$mapping['name']] = $new === null ? null : $this->prepareReferencedDocumentValue($mapping, $new); |
|
| 281 | } |
||
| 282 | |||
| 283 | // @ReferenceMany, @EmbedMany |
||
| 284 | 15 | } elseif ($mapping['type'] === ClassMetadata::MANY && ! $mapping['isInverseSide'] |
|
| 285 | 15 | && $new instanceof PersistentCollectionInterface && $new->isDirty() |
|
| 286 | 15 | && CollectionHelper::isAtomic($mapping['strategy'])) { |
|
| 287 | 1 | $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true); |
|
| 288 | } |
||
| 289 | // @EmbedMany and @ReferenceMany are handled by CollectionPersister |
||
| 290 | } |
||
| 291 | |||
| 292 | // add discriminator if the class has one |
||
| 293 | 86 | if (isset($class->discriminatorField)) { |
|
| 294 | 5 | $discriminatorValue = $class->discriminatorValue; |
|
| 295 | |||
| 296 | 5 | if ($discriminatorValue === null) { |
|
| 297 | if (! empty($class->discriminatorMap)) { |
||
| 298 | throw MappingException::unlistedClassInDiscriminatorMap($class->name); |
||
| 299 | } |
||
| 300 | |||
| 301 | $discriminatorValue = $class->name; |
||
| 302 | } |
||
| 303 | |||
| 304 | 5 | $updateData['$set'][$class->discriminatorField] = $discriminatorValue; |
|
| 305 | } |
||
| 306 | |||
| 307 | 86 | return $updateData; |
|
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns the reference representation to be stored in MongoDB. |
||
| 312 | * |
||
| 313 | * If the document does not have an identifier and the mapping calls for a |
||
| 314 | * simple reference, null may be returned. |
||
| 315 | * |
||
| 316 | * @param array $referenceMapping |
||
| 317 | * @param object $document |
||
| 318 | * |
||
| 319 | * @return array|null |
||
| 320 | */ |
||
| 321 | 222 | public function prepareReferencedDocumentValue(array $referenceMapping, $document) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Returns the embedded document to be stored in MongoDB. |
||
| 328 | * |
||
| 329 | * The return value will usually be an associative array with string keys |
||
| 330 | * corresponding to field names on the embedded document. An object may be |
||
| 331 | * returned if the document is empty, to ensure that a BSON object will be |
||
| 332 | * stored in lieu of an array. |
||
| 333 | * |
||
| 334 | * If $includeNestedCollections is true, nested collections will be included |
||
| 335 | * in this prepared value and the option will cascade to all embedded |
||
| 336 | * associations. If any nested PersistentCollections (embed or reference) |
||
| 337 | * within this value were previously scheduled for deletion or update, they |
||
| 338 | * will also be unscheduled. |
||
| 339 | * |
||
| 340 | * @param array $embeddedMapping |
||
| 341 | * @param object $embeddedDocument |
||
| 342 | * @param bool $includeNestedCollections |
||
| 343 | * |
||
| 344 | * @return array|object |
||
| 345 | * |
||
| 346 | * @throws UnexpectedValueException If an unsupported associating mapping is found. |
||
| 347 | */ |
||
| 348 | 189 | public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDocument, $includeNestedCollections = false) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Returns the embedded document or reference representation to be stored. |
||
| 452 | * |
||
| 453 | * @param array $mapping |
||
| 454 | * @param object $document |
||
| 455 | * @param bool $includeNestedCollections |
||
| 456 | * |
||
| 457 | * @return array|object|null |
||
| 458 | * |
||
| 459 | * @throws InvalidArgumentException If the mapping is neither embedded nor reference. |
||
| 460 | */ |
||
| 461 | 24 | public function prepareAssociatedDocumentValue(array $mapping, $document, $includeNestedCollections = false) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Returns the collection representation to be stored and unschedules it afterwards. |
||
| 476 | * |
||
| 477 | * @param bool $includeNestedCollections |
||
| 478 | * |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | 232 | public function prepareAssociatedCollectionValue(PersistentCollectionInterface $coll, $includeNestedCollections = false) |
|
| 503 | } |
||
| 504 |
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.