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 | 889 | 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 | 883 | public function setOwner($entity, array $assoc) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * INTERNAL: |
||
| 133 | * Gets the collection owner. |
||
| 134 | * |
||
| 135 | * @return object |
||
| 136 | */ |
||
| 137 | 525 | 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 | 157 | 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 | 39 | 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 | 766 | 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 | 587 | 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 | 24 | public function getSnapshot() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * INTERNAL: |
||
| 242 | * getDeleteDiff |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | 328 | View Code Duplication | public function getDeleteDiff() |
| 247 | { |
||
| 248 | 328 | return array_udiff_assoc( |
|
| 249 | 328 | $this->snapshot, |
|
| 250 | 328 | $this->collection->toArray(), |
|
| 251 | function($a, $b) { return $a === $b ? 0 : 1; } |
||
| 252 | ); |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * INTERNAL: |
||
| 257 | * getInsertDiff |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 328 | View Code Duplication | public function getInsertDiff() |
| 262 | { |
||
| 263 | 328 | return array_udiff_assoc( |
|
| 264 | 328 | $this->collection->toArray(), |
|
| 265 | 328 | $this->snapshot, |
|
| 266 | function($a, $b) { return $a === $b ? 0 : 1; } |
||
| 267 | ); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * INTERNAL: Gets the association mapping of the collection. |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | 553 | public function getMapping() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Marks this collection as changed/dirty. |
||
| 282 | * |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | 153 | 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 | 801 | 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 | 789 | 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 | 537 | public function setInitialized($bool) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | 16 | View Code Duplication | public function remove($key) |
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritdoc} |
||
| 366 | */ |
||
| 367 | 26 | public function removeElement($element) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * {@inheritdoc} |
||
| 399 | */ |
||
| 400 | 28 | View Code Duplication | public function containsKey($key) |
| 411 | |||
| 412 | /** |
||
| 413 | * {@inheritdoc} |
||
| 414 | */ |
||
| 415 | 34 | View Code Duplication | public function contains($element) |
| 425 | |||
| 426 | /** |
||
| 427 | * {@inheritdoc} |
||
| 428 | */ |
||
| 429 | 86 | public function get($key) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * {@inheritdoc} |
||
| 447 | */ |
||
| 448 | 734 | public function count() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * {@inheritdoc} |
||
| 461 | */ |
||
| 462 | 4 | View Code Duplication | public function set($key, $value) |
| 472 | |||
| 473 | /** |
||
| 474 | * {@inheritdoc} |
||
| 475 | */ |
||
| 476 | 117 | View Code Duplication | public function add($value) |
| 488 | |||
| 489 | /* ArrayAccess implementation */ |
||
| 490 | |||
| 491 | /** |
||
| 492 | * {@inheritdoc} |
||
| 493 | */ |
||
| 494 | 17 | public function offsetExists($offset) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * {@inheritdoc} |
||
| 501 | */ |
||
| 502 | 62 | public function offsetGet($offset) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * {@inheritdoc} |
||
| 509 | */ |
||
| 510 | 96 | public function offsetSet($offset, $value) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * {@inheritdoc} |
||
| 522 | */ |
||
| 523 | 6 | public function offsetUnset($offset) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * {@inheritdoc} |
||
| 530 | */ |
||
| 531 | 790 | public function isEmpty() |
|
| 535 | |||
| 536 | /** |
||
| 537 | * {@inheritdoc} |
||
| 538 | */ |
||
| 539 | 21 | public function clear() |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Called by PHP when this collection is serialized. Ensures that only the |
||
| 576 | * elements are properly serialized. |
||
| 577 | * |
||
| 578 | * Internal note: Tried to implement Serializable first but that did not work well |
||
| 579 | * with circular references. This solution seems simpler and works well. |
||
| 580 | * |
||
| 581 | * @return array |
||
| 582 | */ |
||
| 583 | 2 | public function __sleep() |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Extracts a slice of $length elements starting at position $offset from the Collection. |
||
| 590 | * |
||
| 591 | * If $length is null it returns all elements from $offset to the end of the Collection. |
||
| 592 | * Keys have to be preserved by this method. Calling this method will only return the |
||
| 593 | * selected slice and NOT change the elements contained in the collection slice is called on. |
||
| 594 | * |
||
| 595 | * @param int $offset |
||
| 596 | * @param int|null $length |
||
| 597 | * |
||
| 598 | * @return array |
||
| 599 | */ |
||
| 600 | 15 | View Code Duplication | public function slice($offset, $length = null) |
| 610 | |||
| 611 | /** |
||
| 612 | * Cleans up internal state of cloned persistent collection. |
||
| 613 | * |
||
| 614 | * The following problems have to be prevented: |
||
| 615 | * 1. Added entities are added to old PC |
||
| 616 | * 2. New collection is not dirty, if reused on other entity nothing |
||
| 617 | * changes. |
||
| 618 | * 3. Snapshot leads to invalid diffs being generated. |
||
| 619 | * 4. Lazy loading grabs entities from old owner object. |
||
| 620 | * 5. New collection is connected to old owner and leads to duplicate keys. |
||
| 621 | * |
||
| 622 | * @return void |
||
| 623 | */ |
||
| 624 | 11 | public function __clone() |
|
| 637 | |||
| 638 | /** |
||
| 639 | * Selects all elements from a selectable that match the expression and |
||
| 640 | * return a new collection containing these elements. |
||
| 641 | * |
||
| 642 | * @param \Doctrine\Common\Collections\Criteria $criteria |
||
| 643 | * |
||
| 644 | * @return Collection |
||
| 645 | * |
||
| 646 | * @throws \RuntimeException |
||
| 647 | */ |
||
| 648 | 15 | public function matching(Criteria $criteria) |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Retrieves the wrapped Collection instance. |
||
| 681 | * |
||
| 682 | * @return \Doctrine\Common\Collections\Collection |
||
| 683 | */ |
||
| 684 | 794 | public function unwrap() |
|
| 688 | |||
| 689 | /** |
||
| 690 | * {@inheritdoc} |
||
| 691 | */ |
||
| 692 | 146 | protected function doInitialize() |
|
| 709 | |||
| 710 | /** |
||
| 711 | * @param object[] $newObjects |
||
| 712 | * |
||
| 713 | * Note: the only reason why this entire looping/complexity is performed via `spl_object_hash` |
||
| 714 | * is because we want to prevent using `array_udiff()`, which is likely to cause very |
||
| 715 | * high overhead (complexity of O(n^2)). `array_diff_key()` performs the operation in |
||
| 716 | * core, which is faster than using a callback for comparisons |
||
| 717 | */ |
||
| 718 | 17 | private function restoreNewObjectsInDirtyCollection(array $newObjects) : void |
|
| 732 | } |
||
| 733 |
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.