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 |
||
| 33 | class PersistentCollection implements BaseCollection |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * A snapshot of the collection at the moment it was fetched from the database. |
||
| 37 | * This is used to create a diff of the collection at commit time. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | private $snapshot = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Collection's owning entity |
||
| 45 | * |
||
| 46 | * @var object |
||
| 47 | */ |
||
| 48 | private $owner; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $mapping; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Whether the collection is dirty and needs to be synchronized with the database |
||
| 57 | * when the UnitOfWork that manages its persistent state commits. |
||
| 58 | * |
||
| 59 | * @var boolean |
||
| 60 | */ |
||
| 61 | private $isDirty = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Whether the collection has already been initialized. |
||
| 65 | * |
||
| 66 | * @var boolean |
||
| 67 | */ |
||
| 68 | private $initialized = true; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The wrapped Collection instance. |
||
| 72 | * |
||
| 73 | * @var BaseCollection |
||
| 74 | */ |
||
| 75 | private $coll; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The DocumentManager that manages the persistence of the collection. |
||
| 79 | * |
||
| 80 | * @var DocumentManager |
||
| 81 | */ |
||
| 82 | private $dm; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The UnitOfWork that manages the persistence of the collection. |
||
| 86 | * |
||
| 87 | * @var UnitOfWork |
||
| 88 | */ |
||
| 89 | private $uow; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The raw mongo data that will be used to initialize this collection. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | private $mongoData = array(); |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Any hints to account for during reconstitution/lookup of the documents. |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | private $hints = array(); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var ClassMetadata |
||
| 107 | */ |
||
| 108 | private $typeClass; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param BaseCollection $coll |
||
| 112 | * @param DocumentManager $dm |
||
| 113 | * @param UnitOfWork $uow |
||
| 114 | */ |
||
| 115 | 372 | public function __construct(BaseCollection $coll, DocumentManager $dm, UnitOfWork $uow) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Sets the document manager and unit of work (used during merge operations). |
||
| 124 | * |
||
| 125 | * @param DocumentManager $dm |
||
| 126 | */ |
||
| 127 | public function setDocumentManager(DocumentManager $dm) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Sets the array of raw mongo data that will be used to initialize this collection. |
||
| 135 | * |
||
| 136 | * @param array $mongoData |
||
| 137 | */ |
||
| 138 | 158 | public function setMongoData(array $mongoData) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Gets the array of raw mongo data that will be used to initialize this collection. |
||
| 145 | * |
||
| 146 | * @return array $mongoData |
||
| 147 | */ |
||
| 148 | 137 | public function getMongoData() |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Set hints to account for during reconstitution/lookup of the documents. |
||
| 155 | * |
||
| 156 | * @param array $hints |
||
| 157 | */ |
||
| 158 | 241 | public function setHints(array $hints) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Get hints to account for during reconstitution/lookup of the documents. |
||
| 165 | * |
||
| 166 | * @return array $hints |
||
| 167 | */ |
||
| 168 | 79 | public function getHints() |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Initializes the collection by loading its contents from the database |
||
| 175 | * if the collection is not yet initialized. |
||
| 176 | */ |
||
| 177 | 345 | public function initialize() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Marks this collection as changed/dirty. |
||
| 214 | */ |
||
| 215 | 164 | private function changed() |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Gets a boolean flag indicating whether this collection is dirty which means |
||
| 234 | * its state needs to be synchronized with the database. |
||
| 235 | * |
||
| 236 | * @return boolean TRUE if the collection is dirty, FALSE otherwise. |
||
| 237 | */ |
||
| 238 | 364 | public function isDirty() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Sets a boolean flag, indicating whether this collection is dirty. |
||
| 245 | * |
||
| 246 | * @param boolean $dirty Whether the collection should be marked dirty or not. |
||
| 247 | */ |
||
| 248 | 357 | public function setDirty($dirty) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * INTERNAL: |
||
| 255 | * Sets the collection's owning entity together with the AssociationMapping that |
||
| 256 | * describes the association between the owner and the elements of the collection. |
||
| 257 | * |
||
| 258 | * @param object $document |
||
| 259 | * @param array $mapping |
||
| 260 | */ |
||
| 261 | 371 | public function setOwner($document, array $mapping) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * INTERNAL: |
||
| 273 | * Tells this collection to take a snapshot of its current state reindexing |
||
| 274 | * itself numerically if using save strategy that is enforcing BSON array. |
||
| 275 | * Reindexing is safe as snapshot is taken only after synchronizing collection |
||
| 276 | * with database or clearing it. |
||
| 277 | */ |
||
| 278 | 241 | public function takeSnapshot() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * INTERNAL: |
||
| 293 | * Clears the internal snapshot information and sets isDirty to true if the collection |
||
| 294 | * has elements. |
||
| 295 | */ |
||
| 296 | 22 | public function clearSnapshot() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * INTERNAL: |
||
| 304 | * Returns the last snapshot of the elements in the collection. |
||
| 305 | * |
||
| 306 | * @return array The last snapshot of the elements. |
||
| 307 | */ |
||
| 308 | public function getSnapshot() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * INTERNAL: |
||
| 315 | * getDeleteDiff |
||
| 316 | * |
||
| 317 | * @return array |
||
| 318 | */ |
||
| 319 | 79 | View Code Duplication | public function getDeleteDiff() |
| 327 | |||
| 328 | /** |
||
| 329 | * INTERNAL: get objects that were removed, unlike getDeleteDiff this doesn't care about indices. |
||
| 330 | * |
||
| 331 | * @return array |
||
| 332 | */ |
||
| 333 | public function getDeletedDocuments() |
||
| 334 | 79 | { |
|
| 335 | $compare = function ($a, $b) { |
||
| 336 | 79 | $compareA = is_object($a) ? spl_object_hash($a) : $a; |
|
| 337 | 79 | $compareb = is_object($b) ? spl_object_hash($b) : $b; |
|
| 338 | 79 | return $compareA === $compareb ? 0 : ($compareA > $compareb ? 1 : -1); |
|
| 339 | }; |
||
| 340 | 79 | ||
| 341 | return array_udiff( |
||
| 342 | $this->snapshot, |
||
| 343 | $this->coll->toArray(), |
||
| 344 | $compare |
||
| 345 | ); |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | 364 | * INTERNAL: |
|
| 350 | * getInsertDiff |
||
| 351 | 364 | * |
|
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | View Code Duplication | public function getInsertDiff() |
|
| 355 | { |
||
| 356 | return array_udiff_assoc( |
||
| 357 | 244 | $this->coll->toArray(), |
|
| 358 | $this->snapshot, |
||
| 359 | 244 | function ($a, $b) { return $a === $b ? 0 : 1; } |
|
| 360 | ); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * INTERNAL: |
||
| 365 | * Gets the collection owner. |
||
| 366 | 2 | * |
|
| 367 | * @return object |
||
| 368 | 2 | */ |
|
| 369 | 1 | public function getOwner() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @return array |
||
| 376 | */ |
||
| 377 | public function getMapping() |
||
| 378 | { |
||
| 379 | return $this->mapping; |
||
| 380 | 242 | } |
|
| 381 | |||
| 382 | 242 | /** |
|
| 383 | 242 | * @return ClassMetadata |
|
| 384 | * @throws MongoDBException |
||
| 385 | */ |
||
| 386 | public function getTypeClass() |
||
| 387 | { |
||
| 388 | if (empty($this->typeClass)) { |
||
| 389 | throw new MongoDBException('Specifying targetDocument is required for the ClassMetadata to be obtained.'); |
||
| 390 | 15 | } |
|
| 391 | |||
| 392 | 15 | return $this->typeClass; |
|
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | 12 | * Sets the initialized flag of the collection, forcing it into that state. |
|
| 397 | * |
||
| 398 | 12 | * @param boolean $bool |
|
| 399 | 12 | */ |
|
| 400 | public function setInitialized($bool) |
||
| 401 | { |
||
| 402 | $this->initialized = $bool; |
||
| 403 | 1 | } |
|
| 404 | |||
| 405 | 1 | /** |
|
| 406 | 1 | * Checks whether this collection has been initialized. |
|
| 407 | * |
||
| 408 | * @return boolean |
||
| 409 | */ |
||
| 410 | public function isInitialized() |
||
| 411 | { |
||
| 412 | 20 | return $this->initialized; |
|
| 413 | } |
||
| 414 | 20 | ||
| 415 | 20 | /** {@inheritdoc} */ |
|
| 416 | public function first() |
||
| 417 | 20 | { |
|
| 418 | $this->initialize(); |
||
| 419 | return $this->coll->first(); |
||
| 420 | } |
||
| 421 | 20 | ||
| 422 | /** {@inheritdoc} */ |
||
| 423 | 20 | public function last() |
|
| 424 | 12 | { |
|
| 425 | 12 | $this->initialize(); |
|
| 426 | return $this->coll->last(); |
||
| 427 | 20 | } |
|
| 428 | |||
| 429 | /** |
||
| 430 | * {@inheritdoc} |
||
| 431 | */ |
||
| 432 | View Code Duplication | public function remove($key) |
|
| 445 | 8 | ||
| 446 | 8 | /** |
|
| 447 | * {@inheritdoc} |
||
| 448 | 10 | */ |
|
| 449 | View Code Duplication | public function removeElement($element) |
|
| 450 | { |
||
| 451 | $this->initialize(); |
||
| 452 | $removed = $this->coll->removeElement($element); |
||
| 453 | |||
| 462 | |||
| 463 | 2 | /** |
|
| 464 | * {@inheritdoc} |
||
| 465 | 2 | */ |
|
| 466 | 2 | public function containsKey($key) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * {@inheritdoc} |
||
| 474 | */ |
||
| 475 | public function contains($element) |
||
| 480 | |||
| 481 | 1 | /** |
|
| 482 | * {@inheritdoc} |
||
| 483 | 1 | */ |
|
| 484 | 1 | public function exists(\Closure $p) |
|
| 489 | |||
| 490 | 87 | /** |
|
| 491 | * {@inheritdoc} |
||
| 492 | 87 | */ |
|
| 493 | 87 | public function indexOf($element) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * {@inheritdoc} |
||
| 501 | */ |
||
| 502 | public function get($key) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * {@inheritdoc} |
||
| 510 | */ |
||
| 511 | public function getKeys() |
||
| 516 | |||
| 517 | 345 | /** |
|
| 518 | * {@inheritdoc} |
||
| 519 | 345 | */ |
|
| 520 | public function getValues() |
||
| 525 | 10 | ||
| 526 | 10 | /** |
|
| 527 | 10 | * {@inheritdoc} |
|
| 528 | */ |
||
| 529 | 345 | public function count() |
|
| 543 | 2 | ||
| 544 | 2 | /** |
|
| 545 | * {@inheritdoc} |
||
| 546 | 26 | */ |
|
| 547 | 26 | public function set($key, $value) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * {@inheritdoc} |
||
| 561 | */ |
||
| 562 | public function add($value) |
||
| 580 | |||
| 581 | 338 | /** |
|
| 582 | * {@inheritdoc} |
||
| 583 | */ |
||
| 584 | public function isEmpty() |
||
| 588 | |||
| 589 | 298 | /** |
|
| 590 | 298 | * {@inheritdoc} |
|
| 591 | */ |
||
| 592 | public function getIterator() |
||
| 597 | |||
| 598 | 198 | /** |
|
| 599 | 198 | * {@inheritdoc} |
|
| 600 | */ |
||
| 601 | public function map(\Closure $func) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * {@inheritdoc} |
||
| 609 | */ |
||
| 610 | public function filter(\Closure $p) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * {@inheritdoc} |
||
| 618 | */ |
||
| 619 | public function forAll(\Closure $p) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * {@inheritdoc} |
||
| 627 | */ |
||
| 628 | public function partition(\Closure $p) |
||
| 633 | |||
| 634 | 15 | /** |
|
| 635 | 15 | * {@inheritdoc} |
|
| 636 | */ |
||
| 637 | public function toArray() |
||
| 642 | |||
| 643 | 27 | /** |
|
| 644 | * {@inheritdoc} |
||
| 645 | */ |
||
| 646 | public function clear() |
||
| 675 | |||
| 676 | 1 | /** |
|
| 677 | 1 | * {@inheritdoc} |
|
| 678 | */ |
||
| 679 | public function slice($offset, $length = null) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Called by PHP when this collection is serialized. Ensures that only the |
||
| 687 | 1 | * elements are properly serialized. |
|
| 688 | * |
||
| 689 | 1 | * @internal Tried to implement Serializable first but that did not work well |
|
| 690 | * with circular references. This solution seems simpler and works well. |
||
| 691 | */ |
||
| 692 | public function __sleep() |
||
| 696 | |||
| 697 | 1 | /* ArrayAccess implementation */ |
|
| 698 | |||
| 699 | 1 | /** |
|
| 700 | * @see containsKey() |
||
| 701 | */ |
||
| 702 | public function offsetExists($offset) |
||
| 706 | |||
| 707 | 75 | /** |
|
| 708 | * @see get() |
||
| 709 | */ |
||
| 710 | public function offsetGet($offset) |
||
| 714 | 38 | ||
| 715 | /** |
||
| 716 | 38 | * @see add() |
|
| 717 | 37 | * @see set() |
|
| 718 | */ |
||
| 719 | public function offsetSet($offset, $value) |
||
| 727 | |||
| 728 | 17 | /** |
|
| 729 | * @see remove() |
||
| 730 | */ |
||
| 731 | public function offsetUnset($offset) |
||
| 735 | |||
| 736 | public function key() |
||
| 740 | |||
| 741 | 1 | /** |
|
| 742 | * Gets the element of the collection at the current iterator position. |
||
| 743 | */ |
||
| 744 | public function current() |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Moves the internal iterator position to the next element. |
||
| 751 | */ |
||
| 752 | public function next() |
||
| 756 | |||
| 757 | 364 | /** |
|
| 758 | * Retrieves the wrapped Collection instance. |
||
| 759 | */ |
||
| 760 | public function unwrap() |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Cleanup internal state of cloned persistent collection. |
||
| 767 | * |
||
| 768 | * The following problems have to be prevented: |
||
| 769 | * 1. Added documents are added to old PersistentCollection |
||
| 770 | * 2. New collection is not dirty, if reused on other document nothing |
||
| 771 | 8 | * changes. |
|
| 772 | * 3. Snapshot leads to invalid diffs being generated. |
||
| 773 | 8 | * 4. Lazy loading grabs entities from old owner object. |
|
| 774 | 8 | * 5. New collection is connected to old owner and leads to duplicate keys. |
|
| 775 | 8 | */ |
|
| 776 | public function __clone() |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Returns whether or not this collection has orphan removal enabled. |
||
| 792 | * |
||
| 793 | 163 | * Embedded documents are automatically considered as "orphan removal enabled" because they might have references |
|
| 794 | * that require to trigger cascade remove operations. |
||
| 795 | 163 | * |
|
| 796 | * @return boolean |
||
| 797 | */ |
||
| 798 | private function isOrphanRemovalEnabled() |
||
| 814 | } |
||
| 815 |
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.