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 | 1211 | 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 | 541 | public function prepareInsertData($document) |
|
60 | { |
||
61 | 541 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
62 | 541 | $changeset = $this->uow->getDocumentChangeSet($document); |
|
63 | |||
64 | 541 | $insertData = []; |
|
65 | 541 | foreach ($class->fieldMappings as $mapping) { |
|
66 | 541 | $new = $changeset[$mapping['fieldName']][1] ?? null; |
|
67 | |||
68 | 541 | 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 | 541 | if ($new === null) { |
|
76 | 368 | continue; |
|
77 | } |
||
78 | |||
79 | // @Field, @String, @Date, etc. |
||
80 | 541 | if (! isset($mapping['association'])) { |
|
81 | 541 | $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 | 528 | if (isset($class->discriminatorField)) { |
|
102 | 28 | $discriminatorValue = $class->discriminatorValue; |
|
103 | |||
104 | 28 | if ($discriminatorValue === null) { |
|
105 | 6 | if (! empty($class->discriminatorMap)) { |
|
106 | 4 | throw MappingException::unlistedClassInDiscriminatorMap($class->name); |
|
107 | } |
||
108 | |||
109 | 2 | $discriminatorValue = $class->name; |
|
110 | } |
||
111 | |||
112 | 27 | $insertData[$class->discriminatorField] = $discriminatorValue; |
|
113 | } |
||
114 | |||
115 | 528 | 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 | 235 | return $updateData; |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Prepares the update query to upsert a given document object in mongodb. |
||
228 | * |
||
229 | * @param object $document |
||
230 | * |
||
231 | * @return array $updateData |
||
232 | */ |
||
233 | 86 | public function prepareUpsertData($document) |
|
234 | { |
||
235 | 86 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
236 | 86 | $changeset = $this->uow->getDocumentChangeSet($document); |
|
237 | |||
238 | 86 | $updateData = []; |
|
239 | 86 | foreach ($changeset as $fieldName => $change) { |
|
240 | 86 | $mapping = $class->fieldMappings[$fieldName]; |
|
241 | |||
242 | 86 | [$old, $new] = $change; |
|
243 | |||
244 | // Scalar fields |
||
245 | 86 | if (! isset($mapping['association'])) { |
|
246 | 86 | if ($new !== null) { |
|
247 | 86 | if (empty($mapping['id']) && isset($mapping['strategy']) && $mapping['strategy'] === ClassMetadata::STORAGE_STRATEGY_INCREMENT) { |
|
248 | 3 | $operator = '$inc'; |
|
249 | 3 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($new - $old); |
|
250 | } else { |
||
251 | 86 | $operator = '$set'; |
|
252 | 86 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($new); |
|
253 | } |
||
254 | |||
255 | 86 | $updateData[$operator][$mapping['name']] = $value; |
|
256 | 11 | } elseif ($mapping['nullable'] === true) { |
|
257 | 86 | $updateData['$setOnInsert'][$mapping['name']] = null; |
|
258 | } |
||
259 | |||
260 | // @EmbedOne |
||
261 | 27 | } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::EMBED_ONE) { |
|
262 | // If we don't have a new value then do nothing on upsert |
||
263 | // If we have a new embedded document then lets set the whole thing |
||
264 | 8 | if ($new && $this->uow->isScheduledForInsert($new)) { |
|
265 | 5 | $updateData['$set'][$mapping['name']] = $this->prepareEmbeddedDocumentValue($mapping, $new); |
|
266 | 3 | } elseif ($new) { |
|
267 | // Update existing embedded document |
||
268 | $update = $this->prepareUpsertData($new); |
||
269 | 8 | foreach ($update as $cmd => $values) { |
|
270 | foreach ($values as $key => $value) { |
||
271 | $updateData[$cmd][$mapping['name'] . '.' . $key] = $value; |
||
272 | } |
||
273 | } |
||
274 | } |
||
275 | |||
276 | // @ReferenceOne |
||
277 | 24 | } elseif (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
278 | 13 | if (isset($new) || $mapping['nullable'] === true) { |
|
279 | 13 | $updateData['$set'][$mapping['name']] = $new === null ? null : $this->prepareReferencedDocumentValue($mapping, $new); |
|
280 | } |
||
281 | |||
282 | // @ReferenceMany, @EmbedMany |
||
283 | 15 | } elseif ($mapping['type'] === ClassMetadata::MANY && ! $mapping['isInverseSide'] |
|
284 | 15 | && $new instanceof PersistentCollectionInterface && $new->isDirty() |
|
285 | 15 | && CollectionHelper::isAtomic($mapping['strategy'])) { |
|
286 | 1 | $updateData['$set'][$mapping['name']] = $this->prepareAssociatedCollectionValue($new, true); |
|
287 | } |
||
288 | // @EmbedMany and @ReferenceMany are handled by CollectionPersister |
||
289 | } |
||
290 | |||
291 | // add discriminator if the class has one |
||
292 | 86 | if (isset($class->discriminatorField)) { |
|
293 | 5 | $discriminatorValue = $class->discriminatorValue; |
|
294 | |||
295 | 5 | if ($discriminatorValue === null) { |
|
296 | if (! empty($class->discriminatorMap)) { |
||
297 | throw MappingException::unlistedClassInDiscriminatorMap($class->name); |
||
298 | } |
||
299 | |||
300 | $discriminatorValue = $class->name; |
||
301 | } |
||
302 | |||
303 | 5 | $updateData['$set'][$class->discriminatorField] = $discriminatorValue; |
|
304 | } |
||
305 | |||
306 | 86 | return $updateData; |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * Returns the reference representation to be stored in MongoDB. |
||
311 | * |
||
312 | * If the document does not have an identifier and the mapping calls for a |
||
313 | * simple reference, null may be returned. |
||
314 | * |
||
315 | * @param array $referenceMapping |
||
316 | * @param object $document |
||
317 | * |
||
318 | * @return array|null |
||
319 | */ |
||
320 | 222 | public function prepareReferencedDocumentValue(array $referenceMapping, $document) |
|
324 | |||
325 | /** |
||
326 | * Returns the embedded document to be stored in MongoDB. |
||
327 | * |
||
328 | * The return value will usually be an associative array with string keys |
||
329 | * corresponding to field names on the embedded document. An object may be |
||
330 | * returned if the document is empty, to ensure that a BSON object will be |
||
331 | * stored in lieu of an array. |
||
332 | * |
||
333 | * If $includeNestedCollections is true, nested collections will be included |
||
334 | * in this prepared value and the option will cascade to all embedded |
||
335 | * associations. If any nested PersistentCollections (embed or reference) |
||
336 | * within this value were previously scheduled for deletion or update, they |
||
337 | * will also be unscheduled. |
||
338 | * |
||
339 | * @param array $embeddedMapping |
||
340 | * @param object $embeddedDocument |
||
341 | * @param bool $includeNestedCollections |
||
342 | * |
||
343 | * @return array|object |
||
344 | * |
||
345 | * @throws UnexpectedValueException If an unsupported associating mapping is found. |
||
346 | */ |
||
347 | 189 | public function prepareEmbeddedDocumentValue(array $embeddedMapping, $embeddedDocument, $includeNestedCollections = false) |
|
448 | |||
449 | /** |
||
450 | * Returns the embedded document or reference representation to be stored. |
||
451 | * |
||
452 | * @param array $mapping |
||
453 | * @param object $document |
||
454 | * @param bool $includeNestedCollections |
||
455 | * |
||
456 | * @return array|object|null |
||
457 | * |
||
458 | * @throws InvalidArgumentException If the mapping is neither embedded nor reference. |
||
459 | */ |
||
460 | 24 | public function prepareAssociatedDocumentValue(array $mapping, $document, $includeNestedCollections = false) |
|
472 | |||
473 | /** |
||
474 | * Returns the collection representation to be stored and unschedules it afterwards. |
||
475 | * |
||
476 | * @param bool $includeNestedCollections |
||
477 | * |
||
478 | * @return array |
||
479 | */ |
||
480 | 232 | public function prepareAssociatedCollectionValue(PersistentCollectionInterface $coll, $includeNestedCollections = false) |
|
502 | } |
||
503 |
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.