Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 = []; |
||
| 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 | 890 | 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 | 884 | public function setOwner($entity, array $assoc) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * INTERNAL: |
||
| 133 | * Gets the collection owner. |
||
| 134 | * |
||
| 135 | * @return object |
||
| 136 | */ |
||
| 137 | 526 | 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 | 158 | View Code Duplication | public function hydrateAdd($element) |
| 167 | |||
| 168 | /** |
||
| 169 | * Sets the owner on the inverse side of the association, when the property was not set yet |
||
| 170 | * |
||
| 171 | * @param mixed $element |
||
| 172 | */ |
||
| 173 | 114 | private function completeBidirectionalAssociationLink($element) : void |
|
| 189 | |||
| 190 | /** |
||
| 191 | * INTERNAL: |
||
| 192 | * Sets a keyed element in the collection during hydration. |
||
| 193 | * |
||
| 194 | * @param mixed $key The key to set. |
||
| 195 | * @param mixed $element The element to set. |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | 39 | View Code Duplication | public function hydrateSet($key, $element) |
| 207 | |||
| 208 | /** |
||
| 209 | * Initializes the collection by loading its contents from the database |
||
| 210 | * if the collection is not yet initialized. |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | 767 | public function initialize() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * INTERNAL: |
||
| 227 | * Tells this collection to take a snapshot of its current state. |
||
| 228 | * |
||
| 229 | * @return void |
||
| 230 | */ |
||
| 231 | 588 | public function takeSnapshot() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * INTERNAL: |
||
| 239 | * Returns the last snapshot of the elements in the collection. |
||
| 240 | * |
||
| 241 | * @return array The last snapshot of the elements. |
||
| 242 | */ |
||
| 243 | 24 | public function getSnapshot() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * INTERNAL: |
||
| 250 | * getDeleteDiff |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | 328 | View Code Duplication | public function getDeleteDiff() |
| 262 | |||
| 263 | /** |
||
| 264 | * INTERNAL: |
||
| 265 | * getInsertDiff |
||
| 266 | * |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | 328 | View Code Duplication | public function getInsertDiff() |
| 277 | |||
| 278 | /** |
||
| 279 | * INTERNAL: Gets the association mapping of the collection. |
||
| 280 | * |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | 554 | public function getMapping() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Marks this collection as changed/dirty. |
||
| 290 | * |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | 154 | private function changed() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Gets a boolean flag indicating whether this collection is dirty which means |
||
| 312 | * its state needs to be synchronized with the database. |
||
| 313 | * |
||
| 314 | * @return boolean TRUE if the collection is dirty, FALSE otherwise. |
||
| 315 | */ |
||
| 316 | 802 | public function isDirty() |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Sets a boolean flag, indicating whether this collection is dirty. |
||
| 323 | * |
||
| 324 | * @param boolean $dirty Whether the collection should be marked dirty or not. |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | 790 | public function setDirty($dirty) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Sets the initialized flag of the collection, forcing it into that state. |
||
| 335 | * |
||
| 336 | * @param boolean $bool |
||
| 337 | * |
||
| 338 | * @return void |
||
| 339 | */ |
||
| 340 | 538 | public function setInitialized($bool) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritdoc} |
||
| 347 | */ |
||
| 348 | 16 | View Code Duplication | public function remove($key) |
| 371 | |||
| 372 | /** |
||
| 373 | * {@inheritdoc} |
||
| 374 | */ |
||
| 375 | 27 | public function removeElement($element) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * {@inheritdoc} |
||
| 407 | */ |
||
| 408 | 28 | View Code Duplication | public function containsKey($key) |
| 419 | |||
| 420 | /** |
||
| 421 | * {@inheritdoc} |
||
| 422 | */ |
||
| 423 | 34 | View Code Duplication | public function contains($element) |
| 433 | |||
| 434 | /** |
||
| 435 | * {@inheritdoc} |
||
| 436 | */ |
||
| 437 | 86 | public function get($key) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * {@inheritdoc} |
||
| 455 | */ |
||
| 456 | 735 | public function count() |
|
| 466 | |||
| 467 | /** |
||
| 468 | * {@inheritdoc} |
||
| 469 | */ |
||
| 470 | 4 | View Code Duplication | public function set($key, $value) |
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritdoc} |
||
| 483 | */ |
||
| 484 | 118 | View Code Duplication | public function add($value) |
| 496 | |||
| 497 | /* ArrayAccess implementation */ |
||
| 498 | |||
| 499 | /** |
||
| 500 | * {@inheritdoc} |
||
| 501 | */ |
||
| 502 | 17 | public function offsetExists($offset) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * {@inheritdoc} |
||
| 509 | */ |
||
| 510 | 62 | public function offsetGet($offset) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * {@inheritdoc} |
||
| 517 | */ |
||
| 518 | 96 | public function offsetSet($offset, $value) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * {@inheritdoc} |
||
| 530 | */ |
||
| 531 | 6 | public function offsetUnset($offset) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * {@inheritdoc} |
||
| 538 | */ |
||
| 539 | 791 | public function isEmpty() |
|
| 543 | |||
| 544 | /** |
||
| 545 | * {@inheritdoc} |
||
| 546 | */ |
||
| 547 | 21 | public function clear() |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Called by PHP when this collection is serialized. Ensures that only the |
||
| 584 | * elements are properly serialized. |
||
| 585 | * |
||
| 586 | * Internal note: Tried to implement Serializable first but that did not work well |
||
| 587 | * with circular references. This solution seems simpler and works well. |
||
| 588 | * |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | 2 | public function __sleep() |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Extracts a slice of $length elements starting at position $offset from the Collection. |
||
| 598 | * |
||
| 599 | * If $length is null it returns all elements from $offset to the end of the Collection. |
||
| 600 | * Keys have to be preserved by this method. Calling this method will only return the |
||
| 601 | * selected slice and NOT change the elements contained in the collection slice is called on. |
||
| 602 | * |
||
| 603 | * @param int $offset |
||
| 604 | * @param int|null $length |
||
| 605 | * |
||
| 606 | * @return array |
||
| 607 | */ |
||
| 608 | 15 | View Code Duplication | public function slice($offset, $length = null) |
| 618 | |||
| 619 | /** |
||
| 620 | * Cleans up internal state of cloned persistent collection. |
||
| 621 | * |
||
| 622 | * The following problems have to be prevented: |
||
| 623 | * 1. Added entities are added to old PC |
||
| 624 | * 2. New collection is not dirty, if reused on other entity nothing |
||
| 625 | * changes. |
||
| 626 | * 3. Snapshot leads to invalid diffs being generated. |
||
| 627 | * 4. Lazy loading grabs entities from old owner object. |
||
| 628 | * 5. New collection is connected to old owner and leads to duplicate keys. |
||
| 629 | * |
||
| 630 | * @return void |
||
| 631 | */ |
||
| 632 | 11 | public function __clone() |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Selects all elements from a selectable that match the expression and |
||
| 648 | * return a new collection containing these elements. |
||
| 649 | * |
||
| 650 | * @param \Doctrine\Common\Collections\Criteria $criteria |
||
| 651 | * |
||
| 652 | * @return Collection |
||
| 653 | * |
||
| 654 | * @throws \RuntimeException |
||
| 655 | */ |
||
| 656 | 15 | public function matching(Criteria $criteria) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Retrieves the wrapped Collection instance. |
||
| 689 | * |
||
| 690 | * @return \Doctrine\Common\Collections\Collection |
||
| 691 | */ |
||
| 692 | 795 | public function unwrap() |
|
| 696 | |||
| 697 | /** |
||
| 698 | * {@inheritdoc} |
||
| 699 | */ |
||
| 700 | 147 | protected function doInitialize() |
|
| 717 | |||
| 718 | /** |
||
| 719 | * @param object[] $newObjects |
||
| 720 | * |
||
| 721 | * Note: the only reason why this entire looping/complexity is performed via `spl_object_hash` |
||
| 722 | * is because we want to prevent using `array_udiff()`, which is likely to cause very |
||
| 723 | * high overhead (complexity of O(n^2)). `array_diff_key()` performs the operation in |
||
| 724 | * core, which is faster than using a callback for comparisons |
||
| 725 | */ |
||
| 726 | 17 | private function restoreNewObjectsInDirtyCollection(array $newObjects) : void |
|
| 740 | } |
||
| 741 |
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.