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 | * @param BaseCollection $coll |
||
| 107 | * @param DocumentManager $dm |
||
| 108 | * @param UnitOfWork $uow |
||
| 109 | */ |
||
| 110 | 384 | public function __construct(BaseCollection $coll, DocumentManager $dm, UnitOfWork $uow) |
|
| 111 | { |
||
| 112 | 384 | $this->coll = $coll; |
|
| 113 | 384 | $this->dm = $dm; |
|
| 114 | 384 | $this->uow = $uow; |
|
| 115 | 384 | } |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Sets the document manager and unit of work (used during merge operations). |
||
| 119 | * |
||
| 120 | * @param DocumentManager $dm |
||
| 121 | */ |
||
| 122 | 2 | public function setDocumentManager(DocumentManager $dm) |
|
| 123 | { |
||
| 124 | 2 | $this->dm = $dm; |
|
| 125 | 2 | $this->uow = $dm->getUnitOfWork(); |
|
| 126 | 2 | } |
|
| 127 | |||
| 128 | /** |
||
| 129 | * Sets the array of raw mongo data that will be used to initialize this collection. |
||
| 130 | * |
||
| 131 | * @param array $mongoData |
||
| 132 | */ |
||
| 133 | 159 | public function setMongoData(array $mongoData) |
|
| 134 | { |
||
| 135 | 159 | $this->mongoData = $mongoData; |
|
| 136 | 159 | } |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Gets the array of raw mongo data that will be used to initialize this collection. |
||
| 140 | * |
||
| 141 | * @return array $mongoData |
||
| 142 | */ |
||
| 143 | 146 | public function getMongoData() |
|
| 144 | { |
||
| 145 | 146 | return $this->mongoData; |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set hints to account for during reconstitution/lookup of the documents. |
||
| 150 | * |
||
| 151 | * @param array $hints |
||
| 152 | */ |
||
| 153 | 242 | public function setHints(array $hints) |
|
| 154 | { |
||
| 155 | 242 | $this->hints = $hints; |
|
| 156 | 242 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get hints to account for during reconstitution/lookup of the documents. |
||
| 160 | * |
||
| 161 | * @return array $hints |
||
| 162 | */ |
||
| 163 | 72 | public function getHints() |
|
| 164 | { |
||
| 165 | 72 | return $this->hints; |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Initializes the collection by loading its contents from the database |
||
| 170 | * if the collection is not yet initialized. |
||
| 171 | */ |
||
| 172 | 349 | public function initialize() |
|
| 173 | { |
||
| 174 | 349 | if ($this->initialized || ! $this->mapping) { |
|
|
|
|||
| 175 | 342 | return; |
|
| 176 | } |
||
| 177 | |||
| 178 | 158 | $newObjects = array(); |
|
| 179 | |||
| 180 | 158 | if ($this->isDirty) { |
|
| 181 | // Remember any NEW objects added through add() |
||
| 182 | 21 | $newObjects = $this->coll->toArray(); |
|
| 183 | 21 | } |
|
| 184 | |||
| 185 | 158 | $this->initialized = true; |
|
| 186 | |||
| 187 | 158 | $this->coll->clear(); |
|
| 188 | 158 | $this->uow->loadCollection($this); |
|
| 189 | 158 | $this->takeSnapshot(); |
|
| 190 | |||
| 191 | 158 | $this->mongoData = array(); |
|
| 192 | |||
| 193 | // Reattach any NEW objects added through add() |
||
| 194 | 158 | if ($newObjects) { |
|
| 195 | 21 | foreach ($newObjects as $key => $obj) { |
|
| 196 | 21 | if (CollectionHelper::isHash($this->mapping['strategy'])) { |
|
| 197 | $this->coll->set($key, $obj); |
||
| 198 | } else { |
||
| 199 | 21 | $this->coll->add($obj); |
|
| 200 | } |
||
| 201 | 21 | } |
|
| 202 | |||
| 203 | 21 | $this->isDirty = true; |
|
| 204 | 21 | } |
|
| 205 | 158 | } |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Marks this collection as changed/dirty. |
||
| 209 | */ |
||
| 210 | 179 | private function changed() |
|
| 211 | { |
||
| 212 | 179 | if ($this->isDirty) { |
|
| 213 | 117 | return; |
|
| 214 | } |
||
| 215 | |||
| 216 | 179 | $this->isDirty = true; |
|
| 217 | |||
| 218 | 179 | if ($this->dm && |
|
| 219 | 179 | $this->mapping !== null && |
|
| 220 | 179 | $this->mapping['isOwningSide'] && |
|
| 221 | 179 | $this->owner && |
|
| 222 | 179 | $this->dm->getClassMetadata(get_class($this->owner))->isChangeTrackingNotify()) { |
|
| 223 | 1 | $this->uow->scheduleForDirtyCheck($this->owner); |
|
| 224 | 1 | } |
|
| 225 | 179 | } |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Gets a boolean flag indicating whether this collection is dirty which means |
||
| 229 | * its state needs to be synchronized with the database. |
||
| 230 | * |
||
| 231 | * @return boolean TRUE if the collection is dirty, FALSE otherwise. |
||
| 232 | */ |
||
| 233 | 366 | public function isDirty() |
|
| 234 | { |
||
| 235 | 366 | return $this->isDirty; |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Sets a boolean flag, indicating whether this collection is dirty. |
||
| 240 | * |
||
| 241 | * @param boolean $dirty Whether the collection should be marked dirty or not. |
||
| 242 | */ |
||
| 243 | 359 | public function setDirty($dirty) |
|
| 244 | { |
||
| 245 | 359 | $this->isDirty = $dirty; |
|
| 246 | 359 | } |
|
| 247 | |||
| 248 | /** |
||
| 249 | * INTERNAL: |
||
| 250 | * Sets the collection's owning entity together with the AssociationMapping that |
||
| 251 | * describes the association between the owner and the elements of the collection. |
||
| 252 | * |
||
| 253 | * @param object $document |
||
| 254 | * @param array $mapping |
||
| 255 | */ |
||
| 256 | 377 | public function setOwner($document, array $mapping) |
|
| 257 | { |
||
| 258 | 377 | $this->owner = $document; |
|
| 259 | 377 | $this->mapping = $mapping; |
|
| 260 | 377 | } |
|
| 261 | |||
| 262 | /** |
||
| 263 | * INTERNAL: |
||
| 264 | * Tells this collection to take a snapshot of its current state reindexing |
||
| 265 | * itself numerically if using save strategy that is enforcing BSON array. |
||
| 266 | * Reindexing is safe as snapshot is taken only after synchronizing collection |
||
| 267 | * with database or clearing it. |
||
| 268 | */ |
||
| 269 | 247 | public function takeSnapshot() |
|
| 270 | { |
||
| 271 | 247 | if (CollectionHelper::isList($this->mapping['strategy'])) { |
|
| 272 | 233 | $array = $this->coll->toArray(); |
|
| 273 | 233 | $this->coll->clear(); |
|
| 274 | 233 | foreach ($array as $document) { |
|
| 275 | 208 | $this->coll->add($document); |
|
| 276 | 233 | } |
|
| 277 | 233 | } |
|
| 278 | 247 | $this->snapshot = $this->coll->toArray(); |
|
| 279 | 247 | $this->isDirty = false; |
|
| 280 | 247 | } |
|
| 281 | |||
| 282 | /** |
||
| 283 | * INTERNAL: |
||
| 284 | * Clears the internal snapshot information and sets isDirty to true if the collection |
||
| 285 | * has elements. |
||
| 286 | */ |
||
| 287 | 22 | public function clearSnapshot() |
|
| 288 | { |
||
| 289 | 22 | $this->snapshot = array(); |
|
| 290 | 22 | $this->isDirty = $this->coll->count() ? true : false; |
|
| 291 | 22 | } |
|
| 292 | |||
| 293 | /** |
||
| 294 | * INTERNAL: |
||
| 295 | * Returns the last snapshot of the elements in the collection. |
||
| 296 | * |
||
| 297 | * @return array The last snapshot of the elements. |
||
| 298 | */ |
||
| 299 | public function getSnapshot() |
||
| 300 | { |
||
| 301 | return $this->snapshot; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * INTERNAL: |
||
| 306 | * getDeleteDiff |
||
| 307 | * |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | 83 | View Code Duplication | public function getDeleteDiff() |
| 311 | { |
||
| 312 | 83 | return array_udiff_assoc( |
|
| 313 | 83 | $this->snapshot, |
|
| 314 | 83 | $this->coll->toArray(), |
|
| 315 | function ($a, $b) { return $a === $b ? 0 : 1; } |
||
| 316 | 83 | ); |
|
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * INTERNAL: get objects that were removed, unlike getDeleteDiff this doesn't care about indices. |
||
| 321 | * |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | 137 | public function getDeletedDocuments() |
|
| 325 | { |
||
| 326 | $compare = function ($a, $b) { |
||
| 327 | 94 | $compareA = is_object($a) ? spl_object_hash($a) : $a; |
|
| 328 | 94 | $compareb = is_object($b) ? spl_object_hash($b) : $b; |
|
| 329 | 94 | return $compareA === $compareb ? 0 : ($compareA > $compareb ? 1 : -1); |
|
| 330 | 137 | }; |
|
| 331 | |||
| 332 | 137 | return array_udiff( |
|
| 333 | 137 | $this->snapshot, |
|
| 334 | 137 | $this->coll->toArray(), |
|
| 335 | $compare |
||
| 336 | 137 | ); |
|
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * INTERNAL: |
||
| 341 | * getInsertDiff |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | 83 | View Code Duplication | public function getInsertDiff() |
| 346 | { |
||
| 347 | 83 | return array_udiff_assoc( |
|
| 348 | 83 | $this->coll->toArray(), |
|
| 349 | 83 | $this->snapshot, |
|
| 350 | function ($a, $b) { return $a === $b ? 0 : 1; } |
||
| 351 | 83 | ); |
|
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * INTERNAL: |
||
| 356 | * Gets the collection owner. |
||
| 357 | * |
||
| 358 | * @return object |
||
| 359 | */ |
||
| 360 | 366 | public function getOwner() |
|
| 361 | { |
||
| 362 | 366 | return $this->owner; |
|
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | 245 | public function getMapping() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * @return ClassMetadata |
||
| 375 | * @throws MongoDBException |
||
| 376 | */ |
||
| 377 | 5 | public function getTypeClass() |
|
| 378 | { |
||
| 379 | 5 | switch (true) { |
|
| 380 | 5 | case ($this->dm === null): |
|
| 381 | 1 | throw new MongoDBException('No DocumentManager is associated with this PersistentCollection, please set one using setDocumentManager method.'); |
|
| 382 | 4 | case (empty($this->mapping)): |
|
| 383 | 1 | throw new MongoDBException('No mapping is associated with this PersistentCollection, please set one using setOwner method.'); |
|
| 384 | 3 | case (empty($this->mapping['targetDocument'])): |
|
| 385 | 1 | throw new MongoDBException('Specifying targetDocument is required for the ClassMetadata to be obtained.'); |
|
| 386 | 2 | default: |
|
| 387 | 2 | return $this->dm->getClassMetadata($this->mapping['targetDocument']); |
|
| 388 | 2 | } |
|
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Sets the initialized flag of the collection, forcing it into that state. |
||
| 393 | * |
||
| 394 | * @param boolean $bool |
||
| 395 | */ |
||
| 396 | 243 | public function setInitialized($bool) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Checks whether this collection has been initialized. |
||
| 403 | * |
||
| 404 | * @return boolean |
||
| 405 | */ |
||
| 406 | 15 | public function isInitialized() |
|
| 410 | |||
| 411 | /** {@inheritdoc} */ |
||
| 412 | 12 | public function first() |
|
| 417 | |||
| 418 | /** {@inheritdoc} */ |
||
| 419 | 1 | public function last() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * {@inheritdoc} |
||
| 427 | */ |
||
| 428 | 20 | View Code Duplication | public function remove($key) |
| 441 | |||
| 442 | /** |
||
| 443 | * {@inheritdoc} |
||
| 444 | */ |
||
| 445 | 13 | View Code Duplication | public function removeElement($element) |
| 458 | |||
| 459 | /** |
||
| 460 | * {@inheritdoc} |
||
| 461 | */ |
||
| 462 | 1 | public function containsKey($key) |
|
| 467 | |||
| 468 | /** |
||
| 469 | * {@inheritdoc} |
||
| 470 | */ |
||
| 471 | 2 | public function contains($element) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * {@inheritdoc} |
||
| 479 | */ |
||
| 480 | public function exists(\Closure $p) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * {@inheritdoc} |
||
| 488 | */ |
||
| 489 | 2 | public function indexOf($element) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * {@inheritdoc} |
||
| 497 | */ |
||
| 498 | 87 | public function get($key) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * {@inheritdoc} |
||
| 506 | */ |
||
| 507 | public function getKeys() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * {@inheritdoc} |
||
| 515 | */ |
||
| 516 | public function getValues() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * {@inheritdoc} |
||
| 524 | */ |
||
| 525 | 347 | public function count() |
|
| 539 | |||
| 540 | /** |
||
| 541 | * {@inheritdoc} |
||
| 542 | */ |
||
| 543 | 35 | public function set($key, $value) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * {@inheritdoc} |
||
| 557 | */ |
||
| 558 | 153 | public function add($value) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * {@inheritdoc} |
||
| 579 | */ |
||
| 580 | 340 | public function isEmpty() |
|
| 584 | |||
| 585 | /** |
||
| 586 | * {@inheritdoc} |
||
| 587 | */ |
||
| 588 | 297 | public function getIterator() |
|
| 593 | |||
| 594 | /** |
||
| 595 | * {@inheritdoc} |
||
| 596 | */ |
||
| 597 | 197 | public function map(\Closure $func) |
|
| 602 | |||
| 603 | /** |
||
| 604 | * {@inheritdoc} |
||
| 605 | */ |
||
| 606 | public function filter(\Closure $p) |
||
| 611 | |||
| 612 | /** |
||
| 613 | * {@inheritdoc} |
||
| 614 | */ |
||
| 615 | public function forAll(\Closure $p) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * {@inheritdoc} |
||
| 623 | */ |
||
| 624 | public function partition(\Closure $p) |
||
| 629 | |||
| 630 | /** |
||
| 631 | * {@inheritdoc} |
||
| 632 | */ |
||
| 633 | 23 | public function toArray() |
|
| 638 | |||
| 639 | /** |
||
| 640 | * {@inheritdoc} |
||
| 641 | */ |
||
| 642 | 27 | public function clear() |
|
| 671 | |||
| 672 | /** |
||
| 673 | * {@inheritdoc} |
||
| 674 | */ |
||
| 675 | 1 | public function slice($offset, $length = null) |
|
| 680 | |||
| 681 | /** |
||
| 682 | * Called by PHP when this collection is serialized. Ensures that only the |
||
| 683 | * elements are properly serialized. |
||
| 684 | * |
||
| 685 | * @internal Tried to implement Serializable first but that did not work well |
||
| 686 | * with circular references. This solution seems simpler and works well. |
||
| 687 | */ |
||
| 688 | 4 | public function __sleep() |
|
| 692 | |||
| 693 | /* ArrayAccess implementation */ |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @see containsKey() |
||
| 697 | */ |
||
| 698 | 1 | public function offsetExists($offset) |
|
| 702 | |||
| 703 | /** |
||
| 704 | * @see get() |
||
| 705 | */ |
||
| 706 | 75 | public function offsetGet($offset) |
|
| 710 | |||
| 711 | /** |
||
| 712 | * @see add() |
||
| 713 | * @see set() |
||
| 714 | */ |
||
| 715 | 39 | public function offsetSet($offset, $value) |
|
| 723 | |||
| 724 | /** |
||
| 725 | * @see remove() |
||
| 726 | */ |
||
| 727 | 17 | public function offsetUnset($offset) |
|
| 731 | |||
| 732 | public function key() |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Gets the element of the collection at the current iterator position. |
||
| 739 | */ |
||
| 740 | 1 | public function current() |
|
| 744 | |||
| 745 | /** |
||
| 746 | * Moves the internal iterator position to the next element. |
||
| 747 | */ |
||
| 748 | public function next() |
||
| 752 | |||
| 753 | /** |
||
| 754 | * Retrieves the wrapped Collection instance. |
||
| 755 | */ |
||
| 756 | 366 | public function unwrap() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Cleanup internal state of cloned persistent collection. |
||
| 763 | * |
||
| 764 | * The following problems have to be prevented: |
||
| 765 | * 1. Added documents are added to old PersistentCollection |
||
| 766 | * 2. New collection is not dirty, if reused on other document nothing |
||
| 767 | * changes. |
||
| 768 | * 3. Snapshot leads to invalid diffs being generated. |
||
| 769 | * 4. Lazy loading grabs entities from old owner object. |
||
| 770 | * 5. New collection is connected to old owner and leads to duplicate keys. |
||
| 771 | */ |
||
| 772 | 8 | public function __clone() |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Returns whether or not this collection has orphan removal enabled. |
||
| 788 | * |
||
| 789 | * Embedded documents are automatically considered as "orphan removal enabled" because they might have references |
||
| 790 | * that require to trigger cascade remove operations. |
||
| 791 | * |
||
| 792 | * @return boolean |
||
| 793 | */ |
||
| 794 | 173 | private function isOrphanRemovalEnabled() |
|
| 810 | } |
||
| 811 |
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.