Complex classes like PersistentCollection 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 PersistentCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | final class PersistentCollection extends AbstractLazyCollection implements Selectable |
||
| 45 | { |
||
| 46 | /** |
||
| 47 | * A snapshot of the collection at the moment it was fetched from the database. |
||
| 48 | * This is used to create a diff of the collection at commit time. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $snapshot = array(); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The entity that owns this collection. |
||
| 56 | * |
||
| 57 | * @var object |
||
| 58 | */ |
||
| 59 | private $owner; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The association mapping the collection belongs to. |
||
| 63 | * This is currently either a OneToManyMapping or a ManyToManyMapping. |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | private $association; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The EntityManager that manages the persistence of the collection. |
||
| 71 | * |
||
| 72 | * @var \Doctrine\ORM\EntityManagerInterface |
||
| 73 | */ |
||
| 74 | private $em; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The name of the field on the target entities that points to the owner |
||
| 78 | * of the collection. This is only set if the association is bi-directional. |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $backRefFieldName; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The class descriptor of the collection's entity type. |
||
| 86 | * |
||
| 87 | * @var ClassMetadata |
||
| 88 | */ |
||
| 89 | private $typeClass; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Whether the collection is dirty and needs to be synchronized with the database |
||
| 93 | * when the UnitOfWork that manages its persistent state commits. |
||
| 94 | * |
||
| 95 | * @var boolean |
||
| 96 | */ |
||
| 97 | private $isDirty = false; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Creates a new persistent collection. |
||
| 101 | * |
||
| 102 | * @param EntityManagerInterface $em The EntityManager the collection will be associated with. |
||
| 103 | * @param ClassMetadata $class The class descriptor of the entity type of this collection. |
||
| 104 | * @param Collection $collection The collection elements. |
||
| 105 | */ |
||
| 106 | 871 | public function __construct(EntityManagerInterface $em, $class, Collection $collection) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * INTERNAL: |
||
| 116 | * Sets the collection's owning entity together with the AssociationMapping that |
||
| 117 | * describes the association between the owner and the elements of the collection. |
||
| 118 | * |
||
| 119 | * @param object $entity |
||
| 120 | * @param array $assoc |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | 864 | public function setOwner($entity, array $assoc) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * INTERNAL: |
||
| 133 | * Gets the collection owner. |
||
| 134 | * |
||
| 135 | * @return object |
||
| 136 | */ |
||
| 137 | 522 | public function getOwner() |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @return Mapping\ClassMetadata |
||
| 144 | */ |
||
| 145 | 21 | public function getTypeClass() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * INTERNAL: |
||
| 152 | * Adds an element to a collection during hydration. This will automatically |
||
| 153 | * complete bidirectional associations in the case of a one-to-many association. |
||
| 154 | * |
||
| 155 | * @param mixed $element The element to add. |
||
| 156 | * |
||
| 157 | * @return void |
||
| 158 | */ |
||
| 159 | 154 | public function hydrateAdd($element) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * INTERNAL: |
||
| 179 | * Sets a keyed element in the collection during hydration. |
||
| 180 | * |
||
| 181 | * @param mixed $key The key to set. |
||
| 182 | * @param mixed $element The element to set. |
||
| 183 | * |
||
| 184 | * @return void |
||
| 185 | */ |
||
| 186 | 38 | public function hydrateSet($key, $element) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Initializes the collection by loading its contents from the database |
||
| 202 | * if the collection is not yet initialized. |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | 749 | public function initialize() |
|
| 216 | |||
| 217 | /** |
||
| 218 | * INTERNAL: |
||
| 219 | * Tells this collection to take a snapshot of its current state. |
||
| 220 | * |
||
| 221 | * @return void |
||
| 222 | */ |
||
| 223 | 577 | public function takeSnapshot() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * INTERNAL: |
||
| 231 | * Returns the last snapshot of the elements in the collection. |
||
| 232 | * |
||
| 233 | * @return array The last snapshot of the elements. |
||
| 234 | */ |
||
| 235 | 22 | public function getSnapshot() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * INTERNAL: |
||
| 242 | * getDeleteDiff |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | 328 | public function getDeleteDiff() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * INTERNAL: |
||
| 257 | * getInsertDiff |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 328 | public function getInsertDiff() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * INTERNAL: Gets the association mapping of the collection. |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | 548 | public function getMapping() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Marks this collection as changed/dirty. |
||
| 282 | * |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | 146 | private function changed() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Gets a boolean flag indicating whether this collection is dirty which means |
||
| 304 | * its state needs to be synchronized with the database. |
||
| 305 | * |
||
| 306 | * @return boolean TRUE if the collection is dirty, FALSE otherwise. |
||
| 307 | */ |
||
| 308 | 787 | public function isDirty() |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Sets a boolean flag, indicating whether this collection is dirty. |
||
| 315 | * |
||
| 316 | * @param boolean $dirty Whether the collection should be marked dirty or not. |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | 778 | public function setDirty($dirty) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Sets the initialized flag of the collection, forcing it into that state. |
||
| 327 | * |
||
| 328 | * @param boolean $bool |
||
| 329 | * |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | 528 | public function setInitialized($bool) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | 16 | public function remove($key) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | 25 | public function removeElement($element) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * {@inheritdoc} |
||
| 403 | */ |
||
| 404 | 28 | public function containsKey($key) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * {@inheritdoc} |
||
| 418 | */ |
||
| 419 | 33 | public function contains($element) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * {@inheritdoc} |
||
| 432 | */ |
||
| 433 | 86 | public function get($key) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * {@inheritdoc} |
||
| 451 | */ |
||
| 452 | 724 | public function count() |
|
| 462 | |||
| 463 | /** |
||
| 464 | * {@inheritdoc} |
||
| 465 | */ |
||
| 466 | 4 | public function set($key, $value) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * {@inheritdoc} |
||
| 479 | */ |
||
| 480 | 110 | public function add($value) |
|
| 492 | |||
| 493 | /* ArrayAccess implementation */ |
||
| 494 | |||
| 495 | /** |
||
| 496 | * {@inheritdoc} |
||
| 497 | */ |
||
| 498 | 17 | public function offsetExists($offset) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * {@inheritdoc} |
||
| 505 | */ |
||
| 506 | 62 | public function offsetGet($offset) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * {@inheritdoc} |
||
| 513 | */ |
||
| 514 | 94 | public function offsetSet($offset, $value) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * {@inheritdoc} |
||
| 525 | */ |
||
| 526 | 6 | public function offsetUnset($offset) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * {@inheritdoc} |
||
| 533 | */ |
||
| 534 | 779 | public function isEmpty() |
|
| 538 | |||
| 539 | /** |
||
| 540 | * {@inheritdoc} |
||
| 541 | */ |
||
| 542 | 20 | public function clear() |
|
| 543 | { |
||
| 544 | 20 | if ($this->initialized && $this->isEmpty()) { |
|
| 545 | 1 | $this->collection->clear(); |
|
| 546 | |||
| 547 | 1 | return; |
|
| 548 | } |
||
| 549 | |||
| 550 | 20 | $uow = $this->em->getUnitOfWork(); |
|
| 551 | |||
| 552 | 20 | if ($this->association['type'] & ClassMetadata::TO_MANY && |
|
| 553 | 20 | $this->association['orphanRemoval'] && |
|
| 554 | 20 | $this->owner) { |
|
| 555 | // we need to initialize here, as orphan removal acts like implicit cascadeRemove, |
||
| 556 | // hence for event listeners we need the objects in memory. |
||
| 557 | 6 | $this->initialize(); |
|
| 558 | |||
| 559 | 6 | foreach ($this->collection as $element) { |
|
| 560 | 6 | $uow->scheduleOrphanRemoval($element); |
|
| 561 | } |
||
| 562 | } |
||
| 563 | |||
| 564 | 20 | $this->collection->clear(); |
|
| 565 | |||
| 566 | 20 | $this->initialized = true; // direct call, {@link initialize()} is too expensive |
|
| 567 | |||
| 568 | 20 | if ($this->association['isOwningSide'] && $this->owner) { |
|
| 569 | 14 | $this->changed(); |
|
| 570 | |||
| 571 | 14 | $uow->scheduleCollectionDeletion($this); |
|
| 572 | |||
| 573 | 14 | $this->takeSnapshot(); |
|
| 574 | } |
||
| 575 | 20 | } |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Called by PHP when this collection is serialized. Ensures that only the |
||
| 579 | * elements are properly serialized. |
||
| 580 | * |
||
| 581 | * Internal note: Tried to implement Serializable first but that did not work well |
||
| 582 | * with circular references. This solution seems simpler and works well. |
||
| 583 | * |
||
| 584 | * @return array |
||
| 585 | */ |
||
| 586 | 2 | public function __sleep() |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Extracts a slice of $length elements starting at position $offset from the Collection. |
||
| 593 | * |
||
| 594 | * If $length is null it returns all elements from $offset to the end of the Collection. |
||
| 595 | * Keys have to be preserved by this method. Calling this method will only return the |
||
| 596 | * selected slice and NOT change the elements contained in the collection slice is called on. |
||
| 597 | * |
||
| 598 | * @param int $offset |
||
| 599 | * @param int|null $length |
||
| 600 | * |
||
| 601 | * @return array |
||
| 602 | */ |
||
| 603 | 15 | public function slice($offset, $length = null) |
|
| 613 | |||
| 614 | /** |
||
| 615 | * Cleans up internal state of cloned persistent collection. |
||
| 616 | * |
||
| 617 | * The following problems have to be prevented: |
||
| 618 | * 1. Added entities are added to old PC |
||
| 619 | * 2. New collection is not dirty, if reused on other entity nothing |
||
| 620 | * changes. |
||
| 621 | * 3. Snapshot leads to invalid diffs being generated. |
||
| 622 | * 4. Lazy loading grabs entities from old owner object. |
||
| 623 | * 5. New collection is connected to old owner and leads to duplicate keys. |
||
| 624 | * |
||
| 625 | * @return void |
||
| 626 | */ |
||
| 627 | 11 | public function __clone() |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Selects all elements from a selectable that match the expression and |
||
| 643 | * return a new collection containing these elements. |
||
| 644 | * |
||
| 645 | * @param \Doctrine\Common\Collections\Criteria $criteria |
||
| 646 | * |
||
| 647 | * @return Collection |
||
| 648 | * |
||
| 649 | * @throws \RuntimeException |
||
| 650 | */ |
||
| 651 | 15 | public function matching(Criteria $criteria) |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Retrieves the wrapped Collection instance. |
||
| 684 | * |
||
| 685 | * @return \Doctrine\Common\Collections\Collection |
||
| 686 | */ |
||
| 687 | 780 | public function unwrap() |
|
| 691 | |||
| 692 | /** |
||
| 693 | * {@inheritdoc} |
||
| 694 | */ |
||
| 695 | 140 | protected function doInitialize() |
|
| 717 | } |
||
| 718 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.