Complex classes like CollectionPersister 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 CollectionPersister, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class CollectionPersister |
||
| 43 | { |
||
| 44 | /** @var DocumentManager */ |
||
| 45 | private $dm; |
||
| 46 | |||
| 47 | /** @var PersistenceBuilder */ |
||
| 48 | private $pb; |
||
| 49 | |||
| 50 | /** @var UnitOfWork */ |
||
| 51 | private $uow; |
||
| 52 | |||
| 53 | 1128 | public function __construct(DocumentManager $dm, PersistenceBuilder $pb, UnitOfWork $uow) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Deletes a PersistentCollection instances completely from a document using $unset. |
||
| 62 | * |
||
| 63 | * @param PersistentCollectionInterface[] $collections |
||
| 64 | * @param array $options |
||
| 65 | */ |
||
| 66 | 38 | public function delete(object $parent, array $collections, array $options) : void |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Updates a list PersistentCollection instances deleting removed rows and inserting new rows. |
||
| 96 | * |
||
| 97 | * @param PersistentCollectionInterface[] $collections |
||
| 98 | * @param array $options |
||
| 99 | */ |
||
| 100 | 106 | public function update(object $parent, array $collections, array $options) : void |
|
| 101 | { |
||
| 102 | 106 | $setStrategyColls = []; |
|
| 103 | 106 | $addPushStrategyColls = []; |
|
| 104 | |||
| 105 | 106 | foreach ($collections as $coll) { |
|
| 106 | 106 | $mapping = $coll->getMapping(); |
|
| 107 | |||
| 108 | 106 | if ($mapping['isInverseSide']) { |
|
| 109 | continue; // ignore inverse side |
||
| 110 | } |
||
| 111 | 106 | switch ($mapping['strategy']) { |
|
| 112 | case ClassMetadata::STORAGE_STRATEGY_ATOMIC_SET: |
||
| 113 | case ClassMetadata::STORAGE_STRATEGY_ATOMIC_SET_ARRAY: |
||
| 114 | throw new UnexpectedValueException($mapping['strategy'] . ' update collection strategy should have been handled by DocumentPersister. Please report a bug in issue tracker'); |
||
| 115 | |||
| 116 | case ClassMetadata::STORAGE_STRATEGY_SET: |
||
| 117 | case ClassMetadata::STORAGE_STRATEGY_SET_ARRAY: |
||
| 118 | 14 | $setStrategyColls[] = $coll; |
|
| 119 | 14 | break; |
|
| 120 | |||
| 121 | case ClassMetadata::STORAGE_STRATEGY_ADD_TO_SET: |
||
| 122 | case ClassMetadata::STORAGE_STRATEGY_PUSH_ALL: |
||
| 123 | 97 | $addPushStrategyColls[] = $coll; |
|
| 124 | 97 | break; |
|
| 125 | |||
| 126 | default: |
||
| 127 | throw new UnexpectedValueException('Unsupported collection strategy: ' . $mapping['strategy']); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | 106 | if (! empty($setStrategyColls)) { |
|
| 132 | 14 | $this->setCollections($parent, $setStrategyColls, $options); |
|
| 133 | } |
||
| 134 | 106 | if (empty($addPushStrategyColls)) { |
|
| 135 | 9 | return; |
|
| 136 | } |
||
| 137 | |||
| 138 | 97 | $this->deleteElements($parent, $addPushStrategyColls, $options); |
|
| 139 | 97 | $this->insertElements($parent, $addPushStrategyColls, $options); |
|
| 140 | 96 | } |
|
| 141 | |||
| 142 | /** |
||
| 143 | * Sets a list of PersistentCollection instances. |
||
| 144 | * |
||
| 145 | * This method is intended to be used with the "set" or "setArray" |
||
| 146 | * strategies. The "setArray" strategy will ensure that the collections is |
||
| 147 | * set as a BSON array, which means the collections elements will be |
||
| 148 | * reindexed numerically before storage. |
||
| 149 | * |
||
| 150 | * @param PersistentCollectionInterface[] $collections |
||
| 151 | * @param array $options |
||
| 152 | */ |
||
| 153 | 14 | private function setCollections(object $parent, array $collections, array $options) : void |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Deletes removed elements from a list of PersistentCollection instances. |
||
| 186 | * |
||
| 187 | * This method is intended to be used with the "pushAll" and "addToSet" strategies. |
||
| 188 | * |
||
| 189 | * @param PersistentCollectionInterface[] $collections |
||
| 190 | * @param array $options |
||
| 191 | */ |
||
| 192 | 97 | private function deleteElements(object $parent, array $collections, array $options) : void |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Inserts new elements for a PersistentCollection instances. |
||
| 245 | * |
||
| 246 | * This method is intended to be used with the "pushAll" and "addToSet" strategies. |
||
| 247 | * |
||
| 248 | * @param PersistentCollectionInterface[] $collections |
||
| 249 | * @param array $options |
||
| 250 | */ |
||
| 251 | 97 | private function insertElements(object $parent, array $collections, array $options) : void |
|
| 252 | { |
||
| 253 | 97 | $pushAllPathCollMap = []; |
|
| 254 | 97 | $addToSetPathCollMap = []; |
|
| 255 | 97 | $pushAllPaths = []; |
|
| 256 | 97 | $addToSetPaths = []; |
|
| 257 | 97 | $diffsMap = []; |
|
| 258 | |||
| 259 | 97 | foreach ($collections as $coll) { |
|
| 260 | 97 | $coll->initialize(); |
|
| 261 | 97 | if (! $this->uow->isCollectionScheduledForUpdate($coll)) { |
|
| 262 | 2 | continue; |
|
| 263 | } |
||
| 264 | 95 | $insertDiff = $coll->getInsertDiff(); |
|
| 265 | |||
| 266 | 95 | if (empty($insertDiff)) { |
|
| 267 | 21 | continue; |
|
| 268 | } |
||
| 269 | |||
| 270 | 80 | $mapping = $coll->getMapping(); |
|
| 271 | 80 | $strategy = $mapping['strategy']; |
|
| 272 | |||
| 273 | 80 | [$propertyPath ] = $this->getPathAndParent($coll); |
|
| 274 | 80 | $diffsMap[$propertyPath] = $insertDiff; |
|
| 275 | |||
| 276 | 80 | switch ($strategy) { |
|
| 277 | case ClassMetadata::STORAGE_STRATEGY_PUSH_ALL: |
||
| 278 | 75 | $pushAllPathCollMap[$propertyPath] = $coll; |
|
| 279 | 75 | $pushAllPaths[] = $propertyPath; |
|
| 280 | 75 | break; |
|
| 281 | |||
| 282 | case ClassMetadata::STORAGE_STRATEGY_ADD_TO_SET: |
||
| 283 | 7 | $addToSetPathCollMap[$propertyPath] = $coll; |
|
| 284 | 7 | $addToSetPaths[] = $propertyPath; |
|
| 285 | 7 | break; |
|
| 286 | |||
| 287 | default: |
||
| 288 | throw new LogicException('Invalid strategy ' . $strategy . ' given for insertCollections'); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | 97 | if (! empty($pushAllPaths)) { |
|
| 293 | 75 | $this->pushAllCollections( |
|
| 294 | 75 | $parent, |
|
| 295 | 75 | $pushAllPaths, |
|
| 296 | 75 | $pushAllPathCollMap, |
|
| 297 | 75 | $diffsMap, |
|
| 298 | 75 | $options |
|
| 299 | ); |
||
| 300 | } |
||
| 301 | 96 | if (empty($addToSetPaths)) { |
|
| 302 | 90 | return; |
|
| 303 | } |
||
| 304 | |||
| 305 | 7 | $this->addToSetCollections( |
|
| 306 | 7 | $parent, |
|
| 307 | 7 | $addToSetPaths, |
|
| 308 | 7 | $addToSetPathCollMap, |
|
| 309 | 7 | $diffsMap, |
|
| 310 | 7 | $options |
|
| 311 | ); |
||
| 312 | 7 | } |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Perform collections update for 'pushAll' strategy. |
||
| 316 | * |
||
| 317 | * @param object $parent Parent object to which passed collections is belong. |
||
| 318 | * @param array $collsPaths Paths of collections that is passed. |
||
| 319 | * @param array $pathCollsMap List of collections indexed by their paths. |
||
| 320 | * @param array $diffsMap List of collection diffs indexed by collections paths. |
||
| 321 | * @param array $options |
||
| 322 | */ |
||
| 323 | 75 | private function pushAllCollections(object $parent, array $collsPaths, array $pathCollsMap, array $diffsMap, array $options) : void |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Perform collections update by 'addToSet' strategy. |
||
| 350 | * |
||
| 351 | * @param object $parent Parent object to which passed collections is belong. |
||
| 352 | * @param array $collsPaths Paths of collections that is passed. |
||
| 353 | * @param array $pathCollsMap List of collections indexed by their paths. |
||
| 354 | * @param array $diffsMap List of collection diffs indexed by collections paths. |
||
| 355 | * @param array $options |
||
| 356 | */ |
||
| 357 | 7 | private function addToSetCollections(object $parent, array $collsPaths, array $pathCollsMap, array $diffsMap, array $options) : void |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Return callback instance for specified collection. This callback will prepare values for query from documents |
||
| 379 | * that collection contain. |
||
| 380 | */ |
||
| 381 | 80 | private function getValuePrepareCallback(PersistentCollectionInterface $coll) : Closure |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Gets the parent information for a given PersistentCollection. It will |
||
| 397 | * retrieve the top-level persistent Document that the PersistentCollection |
||
| 398 | * lives in. We can use this to issue queries when updating a |
||
| 399 | * PersistentCollection that is multiple levels deep inside an embedded |
||
| 400 | * document. |
||
| 401 | * |
||
| 402 | * <code> |
||
| 403 | * list($path, $parent) = $this->getPathAndParent($coll) |
||
| 404 | * </code> |
||
| 405 | */ |
||
| 406 | 118 | private function getPathAndParent(PersistentCollectionInterface $coll) : array |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Executes a query updating the given document. |
||
| 429 | */ |
||
| 430 | 118 | private function executeQuery(object $document, array $newObj, array $options) : void |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Remove from passed paths list all sub-paths. |
||
| 448 | * |
||
| 449 | * @param string[] $paths |
||
| 450 | * |
||
| 451 | * @return string[] |
||
| 452 | */ |
||
| 453 | 119 | private function excludeSubPaths(array $paths) : array |
|
| 473 | } |
||
| 474 |
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.