Complex classes like PersistentCollectionTrait 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 PersistentCollectionTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | trait PersistentCollectionTrait |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * A snapshot of the collection at the moment it was fetched from the database. |
||
| 28 | * This is used to create a diff of the collection at commit time. |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $snapshot = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Collection's owning entity |
||
| 36 | * |
||
| 37 | * @var object|null |
||
| 38 | */ |
||
| 39 | private $owner; |
||
| 40 | |||
| 41 | /** @var array|null */ |
||
| 42 | private $mapping; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Whether the collection is dirty and needs to be synchronized with the database |
||
| 46 | * when the UnitOfWork that manages its persistent state commits. |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | private $isDirty = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Whether the collection has already been initialized. |
||
| 54 | * |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | private $initialized = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The wrapped Collection instance. |
||
| 61 | * |
||
| 62 | * @var BaseCollection |
||
| 63 | */ |
||
| 64 | private $coll; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The DocumentManager that manages the persistence of the collection. |
||
| 68 | * |
||
| 69 | * @var DocumentManager|null |
||
| 70 | */ |
||
| 71 | private $dm; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The UnitOfWork that manages the persistence of the collection. |
||
| 75 | * |
||
| 76 | * @var UnitOfWork |
||
| 77 | */ |
||
| 78 | private $uow; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The raw mongo data that will be used to initialize this collection. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $mongoData = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Any hints to account for during reconstitution/lookup of the documents. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private $hints = []; |
||
| 93 | |||
| 94 | /** {@inheritdoc} */ |
||
| 95 | 4 | public function setDocumentManager(DocumentManager $dm) |
|
| 100 | |||
| 101 | /** {@inheritdoc} */ |
||
| 102 | 169 | public function setMongoData(array $mongoData) |
|
| 106 | |||
| 107 | /** {@inheritdoc} */ |
||
| 108 | 167 | public function getMongoData() |
|
| 112 | |||
| 113 | /** {@inheritdoc} */ |
||
| 114 | 254 | public function setHints(array $hints) |
|
| 118 | |||
| 119 | /** {@inheritdoc} */ |
||
| 120 | 166 | public function getHints() |
|
| 124 | |||
| 125 | /** {@inheritdoc} */ |
||
| 126 | 413 | public function initialize() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Marks this collection as changed/dirty. |
||
| 165 | */ |
||
| 166 | 211 | private function changed() |
|
| 167 | { |
||
| 168 | 211 | if ($this->isDirty) { |
|
| 169 | 132 | return; |
|
| 170 | } |
||
| 171 | |||
| 172 | 211 | $this->isDirty = true; |
|
| 173 | |||
| 174 | 211 | if (! $this->needsSchedulingForSynchronization() || $this->owner === null) { |
|
| 175 | 210 | return; |
|
| 176 | } |
||
| 177 | |||
| 178 | 1 | $this->uow->scheduleForSynchronization($this->owner); |
|
| 179 | 1 | } |
|
| 180 | |||
| 181 | /** {@inheritdoc} */ |
||
| 182 | 425 | public function isDirty() |
|
| 183 | { |
||
| 184 | 425 | if ($this->isDirty) { |
|
| 185 | 278 | return true; |
|
| 186 | } |
||
| 187 | 375 | if (! $this->initialized && count($this->coll)) { |
|
| 188 | // not initialized collection with added elements |
||
| 189 | return true; |
||
| 190 | } |
||
| 191 | 375 | if ($this->initialized) { |
|
| 192 | // if initialized let's check with last known snapshot |
||
| 193 | 369 | return $this->coll->toArray() !== $this->snapshot; |
|
| 194 | } |
||
| 195 | |||
| 196 | 95 | return false; |
|
| 197 | } |
||
| 198 | |||
| 199 | /** {@inheritdoc} */ |
||
| 200 | 413 | public function setDirty($dirty) |
|
| 204 | |||
| 205 | /** {@inheritdoc} */ |
||
| 206 | 441 | public function setOwner(object $document, array $mapping) |
|
| 211 | |||
| 212 | /** {@inheritdoc} */ |
||
| 213 | 299 | public function takeSnapshot() |
|
| 214 | { |
||
| 215 | 299 | if ($this->mapping !== null && CollectionHelper::isList($this->mapping['strategy'])) { |
|
| 216 | 274 | $array = $this->coll->toArray(); |
|
| 217 | 274 | $this->coll->clear(); |
|
| 218 | 274 | foreach ($array as $document) { |
|
| 219 | 249 | $this->coll->add($document); |
|
| 220 | } |
||
| 221 | } |
||
| 222 | 299 | $this->snapshot = $this->coll->toArray(); |
|
| 223 | 299 | $this->isDirty = false; |
|
| 224 | 299 | } |
|
| 225 | |||
| 226 | /** {@inheritdoc} */ |
||
| 227 | 29 | public function clearSnapshot() |
|
| 232 | |||
| 233 | /** {@inheritdoc} */ |
||
| 234 | 1 | public function getSnapshot() |
|
| 238 | |||
| 239 | /** {@inheritdoc} */ |
||
| 240 | 96 | public function getDeleteDiff() |
|
| 250 | |||
| 251 | /** {@inheritdoc} */ |
||
| 252 | 157 | public function getDeletedDocuments() |
|
| 267 | |||
| 268 | /** {@inheritdoc} */ |
||
| 269 | 96 | public function getInsertDiff() |
|
| 279 | |||
| 280 | /** {@inheritdoc} */ |
||
| 281 | 4 | public function getInsertedDocuments() |
|
| 296 | |||
| 297 | /** {@inheritdoc} */ |
||
| 298 | 425 | public function getOwner() : ?object |
|
| 302 | |||
| 303 | /** {@inheritdoc} */ |
||
| 304 | 295 | public function getMapping() |
|
| 308 | |||
| 309 | /** {@inheritdoc} */ |
||
| 310 | 5 | public function getTypeClass() |
|
| 326 | |||
| 327 | /** {@inheritdoc} */ |
||
| 328 | 257 | public function setInitialized($bool) |
|
| 332 | |||
| 333 | /** {@inheritdoc} */ |
||
| 334 | 19 | public function isInitialized() |
|
| 338 | |||
| 339 | /** {@inheritdoc} */ |
||
| 340 | 16 | public function first() |
|
| 346 | |||
| 347 | /** {@inheritdoc} */ |
||
| 348 | 2 | public function last() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | 6 | public function remove($key) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | 16 | public function removeElement($element) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * {@inheritdoc} |
||
| 382 | */ |
||
| 383 | public function containsKey($key) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * {@inheritdoc} |
||
| 392 | */ |
||
| 393 | 3 | public function contains($element) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * {@inheritdoc} |
||
| 402 | */ |
||
| 403 | public function exists(Closure $p) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * {@inheritdoc} |
||
| 412 | */ |
||
| 413 | 2 | public function indexOf($element) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * {@inheritdoc} |
||
| 422 | */ |
||
| 423 | 20 | public function get($key) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * {@inheritdoc} |
||
| 432 | */ |
||
| 433 | public function getKeys() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * {@inheritdoc} |
||
| 442 | */ |
||
| 443 | public function getValues() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * {@inheritdoc} |
||
| 452 | */ |
||
| 453 | 117 | public function count() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * {@inheritdoc} |
||
| 463 | */ |
||
| 464 | 35 | public function set($key, $value) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * {@inheritdoc} |
||
| 471 | */ |
||
| 472 | 170 | public function add($value) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * {@inheritdoc} |
||
| 479 | */ |
||
| 480 | 394 | public function isEmpty() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * {@inheritdoc} |
||
| 487 | */ |
||
| 488 | 335 | public function getIterator() |
|
| 494 | |||
| 495 | /** |
||
| 496 | * {@inheritdoc} |
||
| 497 | */ |
||
| 498 | 234 | public function map(Closure $func) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * {@inheritdoc} |
||
| 507 | */ |
||
| 508 | public function filter(Closure $p) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * {@inheritdoc} |
||
| 517 | */ |
||
| 518 | public function forAll(Closure $p) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * {@inheritdoc} |
||
| 527 | */ |
||
| 528 | public function partition(Closure $p) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * {@inheritdoc} |
||
| 537 | */ |
||
| 538 | 23 | public function toArray() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * {@inheritdoc} |
||
| 547 | */ |
||
| 548 | 33 | public function clear() |
|
| 578 | |||
| 579 | /** |
||
| 580 | * {@inheritdoc} |
||
| 581 | */ |
||
| 582 | 1 | public function slice($offset, $length = null) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Called by PHP when this collection is serialized. Ensures that the |
||
| 591 | * internal state of the collection can be reproduced after serialization |
||
| 592 | */ |
||
| 593 | 6 | public function __sleep() |
|
| 597 | |||
| 598 | /* ArrayAccess implementation */ |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @see containsKey() |
||
| 602 | */ |
||
| 603 | 2 | public function offsetExists($offset) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * @see get() |
||
| 612 | */ |
||
| 613 | 78 | public function offsetGet($offset) |
|
| 619 | |||
| 620 | /** |
||
| 621 | * @see add() |
||
| 622 | * @see set() |
||
| 623 | */ |
||
| 624 | 42 | public function offsetSet($offset, $value) |
|
| 632 | |||
| 633 | /** |
||
| 634 | * @see remove() |
||
| 635 | */ |
||
| 636 | 19 | public function offsetUnset($offset) |
|
| 640 | |||
| 641 | public function key() |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Gets the element of the collection at the current iterator position. |
||
| 648 | */ |
||
| 649 | 1 | public function current() |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Moves the internal iterator position to the next element. |
||
| 656 | */ |
||
| 657 | public function next() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * {@inheritdoc} |
||
| 664 | */ |
||
| 665 | 424 | public function unwrap() |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Cleanup internal state of cloned persistent collection. |
||
| 672 | * |
||
| 673 | * The following problems have to be prevented: |
||
| 674 | * 1. Added documents are added to old PersistentCollection |
||
| 675 | * 2. New collection is not dirty, if reused on other document nothing |
||
| 676 | * changes. |
||
| 677 | * 3. Snapshot leads to invalid diffs being generated. |
||
| 678 | * 4. Lazy loading grabs entities from old owner object. |
||
| 679 | * 5. New collection is connected to old owner and leads to duplicate keys. |
||
| 680 | */ |
||
| 681 | 8 | public function __clone() |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Actual logic for adding an element to the collection. |
||
| 697 | * |
||
| 698 | * @param mixed $value |
||
| 699 | * @param bool $arrayAccess |
||
| 700 | * |
||
| 701 | * @return bool |
||
| 702 | */ |
||
| 703 | 186 | private function doAdd($value, $arrayAccess) |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Actual logic for removing element by its key. |
||
| 724 | * |
||
| 725 | * @param mixed $offset |
||
| 726 | * @param bool $arrayAccess |
||
| 727 | * |
||
| 728 | * @return bool |
||
| 729 | */ |
||
| 730 | 25 | private function doRemove($offset, $arrayAccess) |
|
| 748 | |||
| 749 | /** |
||
| 750 | * Actual logic for setting an element in the collection. |
||
| 751 | * |
||
| 752 | * @param mixed $offset |
||
| 753 | * @param mixed $value |
||
| 754 | * @param bool $arrayAccess |
||
| 755 | */ |
||
| 756 | 36 | private function doSet($offset, $value, $arrayAccess) |
|
| 767 | |||
| 768 | /** |
||
| 769 | * Returns whether or not this collection has orphan removal enabled. |
||
| 770 | * |
||
| 771 | * Embedded documents are automatically considered as "orphan removal enabled" because they might have references |
||
| 772 | * that require to trigger cascade remove operations. |
||
| 773 | * |
||
| 774 | * @return bool |
||
| 775 | */ |
||
| 776 | 204 | private function isOrphanRemovalEnabled() |
|
| 788 | |||
| 789 | /** |
||
| 790 | * Checks whether collection owner needs to be scheduled for dirty change in case the collection is modified. |
||
| 791 | * |
||
| 792 | * @return bool |
||
| 793 | */ |
||
| 794 | 211 | private function needsSchedulingForSynchronization() |
|
| 799 | } |
||
| 800 |