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 UnitOfWork 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 UnitOfWork, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class UnitOfWork implements PropertyChangedListener |
||
| 46 | { |
||
| 47 | /** |
||
| 48 | * A document is in MANAGED state when its persistence is managed by a DocumentManager. |
||
| 49 | */ |
||
| 50 | const STATE_MANAGED = 1; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * A document is new if it has just been instantiated (i.e. using the "new" operator) |
||
| 54 | * and is not (yet) managed by a DocumentManager. |
||
| 55 | */ |
||
| 56 | const STATE_NEW = 2; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * A detached document is an instance with a persistent identity that is not |
||
| 60 | * (or no longer) associated with a DocumentManager (and a UnitOfWork). |
||
| 61 | */ |
||
| 62 | const STATE_DETACHED = 3; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * A removed document instance is an instance with a persistent identity, |
||
| 66 | * associated with a DocumentManager, whose persistent state has been |
||
| 67 | * deleted (or is scheduled for deletion). |
||
| 68 | */ |
||
| 69 | const STATE_REMOVED = 4; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The identity map holds references to all managed documents. |
||
| 73 | * |
||
| 74 | * Documents are grouped by their class name, and then indexed by the |
||
| 75 | * serialized string of their database identifier field or, if the class |
||
| 76 | * has no identifier, the SPL object hash. Serializing the identifier allows |
||
| 77 | * differentiation of values that may be equal (via type juggling) but not |
||
| 78 | * identical. |
||
| 79 | * |
||
| 80 | * Since all classes in a hierarchy must share the same identifier set, |
||
| 81 | * we always take the root class name of the hierarchy. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $identityMap = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Map of all identifiers of managed documents. |
||
| 89 | * Keys are object ids (spl_object_hash). |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | private $documentIdentifiers = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Map of the original document data of managed documents. |
||
| 97 | * Keys are object ids (spl_object_hash). This is used for calculating changesets |
||
| 98 | * at commit time. |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | * @internal Note that PHPs "copy-on-write" behavior helps a lot with memory usage. |
||
| 102 | * A value will only really be copied if the value in the document is modified |
||
| 103 | * by the user. |
||
| 104 | */ |
||
| 105 | private $originalDocumentData = array(); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Map of document changes. Keys are object ids (spl_object_hash). |
||
| 109 | * Filled at the beginning of a commit of the UnitOfWork and cleaned at the end. |
||
| 110 | * |
||
| 111 | * @var array |
||
| 112 | */ |
||
| 113 | private $documentChangeSets = array(); |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The (cached) states of any known documents. |
||
| 117 | * Keys are object ids (spl_object_hash). |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | private $documentStates = array(); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Map of documents that are scheduled for dirty checking at commit time. |
||
| 125 | * |
||
| 126 | * Documents are grouped by their class name, and then indexed by their SPL |
||
| 127 | * object hash. This is only used for documents with a change tracking |
||
| 128 | * policy of DEFERRED_EXPLICIT. |
||
| 129 | * |
||
| 130 | * @var array |
||
| 131 | * @todo rename: scheduledForSynchronization |
||
| 132 | */ |
||
| 133 | private $scheduledForDirtyCheck = array(); |
||
| 134 | |||
| 135 | /** |
||
| 136 | * A list of all pending document insertions. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | private $documentInsertions = array(); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * A list of all pending document updates. |
||
| 144 | * |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | private $documentUpdates = array(); |
||
| 148 | |||
| 149 | /** |
||
| 150 | * A list of all pending document upserts. |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | private $documentUpserts = array(); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * A list of all pending document deletions. |
||
| 158 | * |
||
| 159 | * @var array |
||
| 160 | */ |
||
| 161 | private $documentDeletions = array(); |
||
| 162 | |||
| 163 | /** |
||
| 164 | * All pending collection deletions. |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | private $collectionDeletions = array(); |
||
| 169 | |||
| 170 | /** |
||
| 171 | * All pending collection updates. |
||
| 172 | * |
||
| 173 | * @var array |
||
| 174 | */ |
||
| 175 | private $collectionUpdates = array(); |
||
| 176 | |||
| 177 | /** |
||
| 178 | * A list of documents related to collections scheduled for update or deletion |
||
| 179 | * |
||
| 180 | * @var array |
||
| 181 | */ |
||
| 182 | private $hasScheduledCollections = array(); |
||
| 183 | |||
| 184 | /** |
||
| 185 | * List of collections visited during changeset calculation on a commit-phase of a UnitOfWork. |
||
| 186 | * At the end of the UnitOfWork all these collections will make new snapshots |
||
| 187 | * of their data. |
||
| 188 | * |
||
| 189 | * @var array |
||
| 190 | */ |
||
| 191 | private $visitedCollections = array(); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * The DocumentManager that "owns" this UnitOfWork instance. |
||
| 195 | * |
||
| 196 | * @var DocumentManager |
||
| 197 | */ |
||
| 198 | private $dm; |
||
| 199 | |||
| 200 | /** |
||
| 201 | * The EventManager used for dispatching events. |
||
| 202 | * |
||
| 203 | * @var EventManager |
||
| 204 | */ |
||
| 205 | private $evm; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Additional documents that are scheduled for removal. |
||
| 209 | * |
||
| 210 | * @var array |
||
| 211 | */ |
||
| 212 | private $orphanRemovals = array(); |
||
| 213 | |||
| 214 | /** |
||
| 215 | * The HydratorFactory used for hydrating array Mongo documents to Doctrine object documents. |
||
| 216 | * |
||
| 217 | * @var HydratorFactory |
||
| 218 | */ |
||
| 219 | private $hydratorFactory; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * The document persister instances used to persist document instances. |
||
| 223 | * |
||
| 224 | * @var array |
||
| 225 | */ |
||
| 226 | private $persisters = array(); |
||
| 227 | |||
| 228 | /** |
||
| 229 | * The collection persister instance used to persist changes to collections. |
||
| 230 | * |
||
| 231 | * @var Persisters\CollectionPersister |
||
| 232 | */ |
||
| 233 | private $collectionPersister; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * The persistence builder instance used in DocumentPersisters. |
||
| 237 | * |
||
| 238 | * @var PersistenceBuilder |
||
| 239 | */ |
||
| 240 | private $persistenceBuilder; |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Array of parent associations between embedded documents. |
||
| 244 | * |
||
| 245 | * @var array |
||
| 246 | */ |
||
| 247 | private $parentAssociations = array(); |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @var LifecycleEventManager |
||
| 251 | */ |
||
| 252 | private $lifecycleEventManager; |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Array of embedded documents known to UnitOfWork. We need to hold them to prevent spl_object_hash |
||
| 256 | * collisions in case already managed object is lost due to GC (so now it won't). Embedded documents |
||
| 257 | * found during doDetach are removed from the registry, to empty it altogether clear() can be utilized. |
||
| 258 | * |
||
| 259 | * @var array |
||
| 260 | */ |
||
| 261 | private $embeddedDocumentsRegistry = array(); |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Initializes a new UnitOfWork instance, bound to the given DocumentManager. |
||
| 265 | * |
||
| 266 | * @param DocumentManager $dm |
||
| 267 | * @param EventManager $evm |
||
| 268 | * @param HydratorFactory $hydratorFactory |
||
| 269 | */ |
||
| 270 | 1044 | public function __construct(DocumentManager $dm, EventManager $evm, HydratorFactory $hydratorFactory) |
|
| 271 | { |
||
| 272 | 1044 | $this->dm = $dm; |
|
| 273 | 1044 | $this->evm = $evm; |
|
| 274 | 1044 | $this->hydratorFactory = $hydratorFactory; |
|
| 275 | 1044 | $this->lifecycleEventManager = new LifecycleEventManager($dm, $this, $evm); |
|
| 276 | 1044 | } |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Factory for returning new PersistenceBuilder instances used for preparing data into |
||
| 280 | * queries for insert persistence. |
||
| 281 | * |
||
| 282 | * @return PersistenceBuilder $pb |
||
| 283 | */ |
||
| 284 | 729 | public function getPersistenceBuilder() |
|
| 285 | { |
||
| 286 | 729 | if ( ! $this->persistenceBuilder) { |
|
| 287 | 729 | $this->persistenceBuilder = new PersistenceBuilder($this->dm, $this); |
|
| 288 | } |
||
| 289 | 729 | return $this->persistenceBuilder; |
|
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Sets the parent association for a given embedded document. |
||
| 294 | * |
||
| 295 | * @param object $document |
||
| 296 | * @param array $mapping |
||
| 297 | * @param object $parent |
||
| 298 | * @param string $propertyPath |
||
| 299 | */ |
||
| 300 | 199 | public function setParentAssociation($document, $mapping, $parent, $propertyPath) |
|
| 301 | { |
||
| 302 | 199 | $oid = spl_object_hash($document); |
|
| 303 | 199 | $this->embeddedDocumentsRegistry[$oid] = $document; |
|
| 304 | 199 | $this->parentAssociations[$oid] = array($mapping, $parent, $propertyPath); |
|
| 305 | 199 | } |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Gets the parent association for a given embedded document. |
||
| 309 | * |
||
| 310 | * <code> |
||
| 311 | * list($mapping, $parent, $propertyPath) = $this->getParentAssociation($embeddedDocument); |
||
| 312 | * </code> |
||
| 313 | * |
||
| 314 | * @param object $document |
||
| 315 | * @return array $association |
||
| 316 | */ |
||
| 317 | 229 | public function getParentAssociation($document) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Get the document persister instance for the given document name |
||
| 328 | * |
||
| 329 | * @param string $documentName |
||
| 330 | * @return Persisters\DocumentPersister |
||
| 331 | */ |
||
| 332 | 727 | public function getDocumentPersister($documentName) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Get the collection persister instance. |
||
| 344 | * |
||
| 345 | * @return \Doctrine\ODM\MongoDB\Persisters\CollectionPersister |
||
| 346 | */ |
||
| 347 | 727 | public function getCollectionPersister() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Set the document persister instance to use for the given document name |
||
| 358 | * |
||
| 359 | * @param string $documentName |
||
| 360 | * @param Persisters\DocumentPersister $persister |
||
| 361 | */ |
||
| 362 | 14 | public function setDocumentPersister($documentName, Persisters\DocumentPersister $persister) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Commits the UnitOfWork, executing all operations that have been postponed |
||
| 369 | * up to this point. The state of all managed documents will be synchronized with |
||
| 370 | * the database. |
||
| 371 | * |
||
| 372 | * The operations are executed in the following order: |
||
| 373 | * |
||
| 374 | * 1) All document insertions |
||
| 375 | * 2) All document updates |
||
| 376 | * 3) All document deletions |
||
| 377 | * |
||
| 378 | * @param object $document |
||
| 379 | * @param array $options Array of options to be used with batchInsert(), update() and remove() |
||
| 380 | */ |
||
| 381 | 604 | public function commit($document = null, array $options = array()) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Groups a list of scheduled documents by their class. |
||
| 462 | * |
||
| 463 | * @param array $documents Scheduled documents (e.g. $this->documentInsertions) |
||
| 464 | * @param bool $includeEmbedded |
||
| 465 | * @return array Tuples of ClassMetadata and a corresponding array of objects |
||
| 466 | */ |
||
| 467 | 599 | private function getClassesForCommitAction($documents, $includeEmbedded = false) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Compute changesets of all documents scheduled for insertion. |
||
| 499 | * |
||
| 500 | * Embedded documents will not be processed. |
||
| 501 | */ |
||
| 502 | 606 | View Code Duplication | private function computeScheduleInsertsChangeSets() |
| 511 | |||
| 512 | /** |
||
| 513 | * Compute changesets of all documents scheduled for upsert. |
||
| 514 | * |
||
| 515 | * Embedded documents will not be processed. |
||
| 516 | */ |
||
| 517 | 605 | View Code Duplication | private function computeScheduleUpsertsChangeSets() |
| 526 | |||
| 527 | /** |
||
| 528 | * Only flush the given document according to a ruleset that keeps the UoW consistent. |
||
| 529 | * |
||
| 530 | * 1. All documents scheduled for insertion and (orphan) removals are processed as well! |
||
| 531 | * 2. Proxies are skipped. |
||
| 532 | * 3. Only if document is properly managed. |
||
| 533 | * |
||
| 534 | * @param object $document |
||
| 535 | * @throws \InvalidArgumentException If the document is not STATE_MANAGED |
||
| 536 | * @return void |
||
| 537 | */ |
||
| 538 | 14 | private function computeSingleDocumentChangeSet($document) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Gets the changeset for a document. |
||
| 575 | * |
||
| 576 | * @param object $document |
||
| 577 | * @return array array('property' => array(0 => mixed|null, 1 => mixed|null)) |
||
| 578 | */ |
||
| 579 | 599 | public function getDocumentChangeSet($document) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * INTERNAL: |
||
| 590 | * Sets the changeset for a document. |
||
| 591 | * |
||
| 592 | * @param object $document |
||
| 593 | * @param array $changeset |
||
| 594 | */ |
||
| 595 | 1 | public function setDocumentChangeSet($document, $changeset) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Get a documents actual data, flattening all the objects to arrays. |
||
| 602 | * |
||
| 603 | * @param object $document |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | 606 | public function getDocumentActualData($document) |
|
| 607 | { |
||
| 608 | 606 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 609 | 606 | $actualData = array(); |
|
| 610 | 606 | foreach ($class->reflFields as $name => $refProp) { |
|
| 611 | 606 | $mapping = $class->fieldMappings[$name]; |
|
| 612 | // skip not saved fields |
||
| 613 | 606 | if (isset($mapping['notSaved']) && $mapping['notSaved'] === true) { |
|
| 614 | 51 | continue; |
|
| 615 | } |
||
| 616 | 606 | $value = $refProp->getValue($document); |
|
| 617 | 606 | if (isset($mapping['file']) && ! $value instanceof GridFSFile) { |
|
| 618 | 6 | $value = new GridFSFile($value); |
|
| 619 | 6 | $class->reflFields[$name]->setValue($document, $value); |
|
| 620 | 6 | $actualData[$name] = $value; |
|
| 621 | 606 | } elseif ((isset($mapping['association']) && $mapping['type'] === 'many') |
|
| 622 | 606 | && $value !== null && ! ($value instanceof PersistentCollectionInterface)) { |
|
| 623 | // If $actualData[$name] is not a Collection then use an ArrayCollection. |
||
| 624 | 392 | if ( ! $value instanceof Collection) { |
|
| 625 | 131 | $value = new ArrayCollection($value); |
|
| 626 | } |
||
| 627 | |||
| 628 | // Inject PersistentCollection |
||
| 629 | 392 | $coll = $this->dm->getConfiguration()->getPersistentCollectionFactory()->create($this->dm, $mapping, $value); |
|
| 630 | 392 | $coll->setOwner($document, $mapping); |
|
| 631 | 392 | $coll->setDirty( ! $value->isEmpty()); |
|
| 632 | 392 | $class->reflFields[$name]->setValue($document, $coll); |
|
| 633 | 392 | $actualData[$name] = $coll; |
|
| 634 | } else { |
||
| 635 | 606 | $actualData[$name] = $value; |
|
| 636 | } |
||
| 637 | } |
||
| 638 | 606 | return $actualData; |
|
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Computes the changes that happened to a single document. |
||
| 643 | * |
||
| 644 | * Modifies/populates the following properties: |
||
| 645 | * |
||
| 646 | * {@link originalDocumentData} |
||
| 647 | * If the document is NEW or MANAGED but not yet fully persisted (only has an id) |
||
| 648 | * then it was not fetched from the database and therefore we have no original |
||
| 649 | * document data yet. All of the current document data is stored as the original document data. |
||
| 650 | * |
||
| 651 | * {@link documentChangeSets} |
||
| 652 | * The changes detected on all properties of the document are stored there. |
||
| 653 | * A change is a tuple array where the first entry is the old value and the second |
||
| 654 | * entry is the new value of the property. Changesets are used by persisters |
||
| 655 | * to INSERT/UPDATE the persistent document state. |
||
| 656 | * |
||
| 657 | * {@link documentUpdates} |
||
| 658 | * If the document is already fully MANAGED (has been fetched from the database before) |
||
| 659 | * and any changes to its properties are detected, then a reference to the document is stored |
||
| 660 | * there to mark it for an update. |
||
| 661 | * |
||
| 662 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 663 | * @param object $document The document for which to compute the changes. |
||
| 664 | */ |
||
| 665 | 603 | public function computeChangeSet(ClassMetadata $class, $document) |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Used to do the common work of computeChangeSet and recomputeSingleDocumentChangeSet |
||
| 681 | * |
||
| 682 | * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $class |
||
| 683 | * @param object $document |
||
| 684 | * @param boolean $recompute |
||
| 685 | */ |
||
| 686 | 603 | private function computeOrRecomputeChangeSet(ClassMetadata $class, $document, $recompute = false) |
|
| 687 | { |
||
| 688 | 603 | $oid = spl_object_hash($document); |
|
| 689 | 603 | $actualData = $this->getDocumentActualData($document); |
|
| 690 | 603 | $isNewDocument = ! isset($this->originalDocumentData[$oid]); |
|
| 691 | 603 | if ($isNewDocument) { |
|
| 692 | // Document is either NEW or MANAGED but not yet fully persisted (only has an id). |
||
| 693 | // These result in an INSERT. |
||
| 694 | 603 | $this->originalDocumentData[$oid] = $actualData; |
|
| 695 | 603 | $changeSet = array(); |
|
| 696 | 603 | foreach ($actualData as $propName => $actualValue) { |
|
| 697 | /* At this PersistentCollection shouldn't be here, probably it |
||
| 698 | * was cloned and its ownership must be fixed |
||
| 699 | */ |
||
| 700 | 603 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 701 | $actualData[$propName] = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
||
| 702 | $actualValue = $actualData[$propName]; |
||
| 703 | } |
||
| 704 | // ignore inverse side of reference relationship |
||
| 705 | 603 | View Code Duplication | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
| 706 | 185 | continue; |
|
| 707 | } |
||
| 708 | 603 | $changeSet[$propName] = array(null, $actualValue); |
|
| 709 | } |
||
| 710 | 603 | $this->documentChangeSets[$oid] = $changeSet; |
|
| 711 | } else { |
||
| 712 | // Document is "fully" MANAGED: it was already fully persisted before |
||
| 713 | // and we have a copy of the original data |
||
| 714 | 290 | $originalData = $this->originalDocumentData[$oid]; |
|
| 715 | 290 | $isChangeTrackingNotify = $class->isChangeTrackingNotify(); |
|
| 716 | 290 | if ($isChangeTrackingNotify && ! $recompute && isset($this->documentChangeSets[$oid])) { |
|
| 717 | 2 | $changeSet = $this->documentChangeSets[$oid]; |
|
| 718 | } else { |
||
| 719 | 290 | $changeSet = array(); |
|
| 720 | } |
||
| 721 | |||
| 722 | 290 | foreach ($actualData as $propName => $actualValue) { |
|
| 723 | // skip not saved fields |
||
| 724 | 290 | if (isset($class->fieldMappings[$propName]['notSaved']) && $class->fieldMappings[$propName]['notSaved'] === true) { |
|
| 725 | continue; |
||
| 726 | } |
||
| 727 | |||
| 728 | 290 | $orgValue = isset($originalData[$propName]) ? $originalData[$propName] : null; |
|
| 729 | |||
| 730 | // skip if value has not changed |
||
| 731 | 290 | if ($orgValue === $actualValue) { |
|
| 732 | 289 | if ($actualValue instanceof PersistentCollectionInterface) { |
|
| 733 | 203 | if (! $actualValue->isDirty() && ! $this->isCollectionScheduledForDeletion($actualValue)) { |
|
| 734 | // consider dirty collections as changed as well |
||
| 735 | 203 | continue; |
|
| 736 | } |
||
| 737 | 289 | } elseif ( ! (isset($class->fieldMappings[$propName]['file']) && $actualValue->isDirty())) { |
|
| 738 | // but consider dirty GridFSFile instances as changed |
||
| 739 | 289 | continue; |
|
| 740 | } |
||
| 741 | } |
||
| 742 | |||
| 743 | // if relationship is a embed-one, schedule orphan removal to trigger cascade remove operations |
||
| 744 | 249 | if (isset($class->fieldMappings[$propName]['embedded']) && $class->fieldMappings[$propName]['type'] === 'one') { |
|
| 745 | 12 | if ($orgValue !== null) { |
|
| 746 | 8 | $this->scheduleOrphanRemoval($orgValue); |
|
| 747 | } |
||
| 748 | |||
| 749 | 12 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 750 | 12 | continue; |
|
| 751 | } |
||
| 752 | |||
| 753 | // if owning side of reference-one relationship |
||
| 754 | 243 | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['type'] === 'one' && $class->fieldMappings[$propName]['isOwningSide']) { |
|
| 755 | 13 | if ($orgValue !== null && $class->fieldMappings[$propName]['orphanRemoval']) { |
|
| 756 | 1 | $this->scheduleOrphanRemoval($orgValue); |
|
| 757 | } |
||
| 758 | |||
| 759 | 13 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 760 | 13 | continue; |
|
| 761 | } |
||
| 762 | |||
| 763 | 236 | if ($isChangeTrackingNotify) { |
|
| 764 | 3 | continue; |
|
| 765 | } |
||
| 766 | |||
| 767 | // ignore inverse side of reference relationship |
||
| 768 | 234 | View Code Duplication | if (isset($class->fieldMappings[$propName]['reference']) && $class->fieldMappings[$propName]['isInverseSide']) { |
| 769 | 6 | continue; |
|
| 770 | } |
||
| 771 | |||
| 772 | // Persistent collection was exchanged with the "originally" |
||
| 773 | // created one. This can only mean it was cloned and replaced |
||
| 774 | // on another document. |
||
| 775 | 232 | if ($actualValue instanceof PersistentCollectionInterface && $actualValue->getOwner() !== $document) { |
|
| 776 | 6 | $actualValue = $this->fixPersistentCollectionOwnership($actualValue, $document, $class, $propName); |
|
| 777 | } |
||
| 778 | |||
| 779 | // if embed-many or reference-many relationship |
||
| 780 | 232 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'many') { |
|
| 781 | 117 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 782 | /* If original collection was exchanged with a non-empty value |
||
| 783 | * and $set will be issued, there is no need to $unset it first |
||
| 784 | */ |
||
| 785 | 117 | if ($actualValue && $actualValue->isDirty() && CollectionHelper::usesSet($class->fieldMappings[$propName]['strategy'])) { |
|
| 786 | 28 | continue; |
|
| 787 | } |
||
| 788 | 97 | if ($orgValue !== $actualValue && $orgValue instanceof PersistentCollectionInterface) { |
|
| 789 | 18 | $this->scheduleCollectionDeletion($orgValue); |
|
| 790 | } |
||
| 791 | 97 | continue; |
|
| 792 | } |
||
| 793 | |||
| 794 | // skip equivalent date values |
||
| 795 | 152 | if (isset($class->fieldMappings[$propName]['type']) && $class->fieldMappings[$propName]['type'] === 'date') { |
|
| 796 | 37 | $dateType = Type::getType('date'); |
|
| 797 | 37 | $dbOrgValue = $dateType->convertToDatabaseValue($orgValue); |
|
| 798 | 37 | $dbActualValue = $dateType->convertToDatabaseValue($actualValue); |
|
| 799 | |||
| 800 | 37 | if ($dbOrgValue instanceof \MongoDate && $dbActualValue instanceof \MongoDate && $dbOrgValue == $dbActualValue) { |
|
| 801 | 30 | continue; |
|
| 802 | } |
||
| 803 | } |
||
| 804 | |||
| 805 | // regular field |
||
| 806 | 135 | $changeSet[$propName] = array($orgValue, $actualValue); |
|
| 807 | } |
||
| 808 | 290 | if ($changeSet) { |
|
| 809 | 238 | $this->documentChangeSets[$oid] = isset($this->documentChangeSets[$oid]) |
|
| 810 | 21 | ? $changeSet + $this->documentChangeSets[$oid] |
|
| 811 | 233 | : $changeSet; |
|
| 812 | |||
| 813 | 238 | $this->originalDocumentData[$oid] = $actualData; |
|
| 814 | 238 | $this->scheduleForUpdate($document); |
|
| 815 | } |
||
| 816 | } |
||
| 817 | |||
| 818 | // Look for changes in associations of the document |
||
| 819 | 603 | $associationMappings = array_filter( |
|
| 820 | 603 | $class->associationMappings, |
|
| 821 | function ($assoc) { return empty($assoc['notSaved']); } |
||
| 822 | ); |
||
| 823 | |||
| 824 | 603 | foreach ($associationMappings as $mapping) { |
|
| 825 | 462 | $value = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 826 | |||
| 827 | 462 | if ($value === null) { |
|
| 828 | 310 | continue; |
|
| 829 | } |
||
| 830 | |||
| 831 | 449 | $this->computeAssociationChanges($document, $mapping, $value); |
|
| 832 | |||
| 833 | 448 | if (isset($mapping['reference'])) { |
|
| 834 | 338 | continue; |
|
| 835 | } |
||
| 836 | |||
| 837 | 349 | $values = $mapping['type'] === ClassMetadata::ONE ? array($value) : $value->unwrap(); |
|
| 838 | |||
| 839 | 349 | foreach ($values as $obj) { |
|
| 840 | 184 | $oid2 = spl_object_hash($obj); |
|
| 841 | |||
| 842 | 184 | if (isset($this->documentChangeSets[$oid2])) { |
|
| 843 | 182 | if (empty($this->documentChangeSets[$oid][$mapping['fieldName']])) { |
|
| 844 | // instance of $value is the same as it was previously otherwise there would be |
||
| 845 | // change set already in place |
||
| 846 | 40 | $this->documentChangeSets[$oid][$mapping['fieldName']] = array($value, $value); |
|
| 847 | } |
||
| 848 | |||
| 849 | 182 | if ( ! $isNewDocument) { |
|
| 850 | 79 | $this->scheduleForUpdate($document); |
|
| 851 | } |
||
| 852 | |||
| 853 | 349 | break; |
|
| 854 | } |
||
| 855 | } |
||
| 856 | } |
||
| 857 | 602 | } |
|
| 858 | |||
| 859 | /** |
||
| 860 | * Computes all the changes that have been done to documents and collections |
||
| 861 | * since the last commit and stores these changes in the _documentChangeSet map |
||
| 862 | * temporarily for access by the persisters, until the UoW commit is finished. |
||
| 863 | */ |
||
| 864 | 601 | public function computeChangeSets() |
|
| 914 | |||
| 915 | /** |
||
| 916 | * Computes the changes of an association. |
||
| 917 | * |
||
| 918 | * @param object $parentDocument |
||
| 919 | * @param array $assoc |
||
| 920 | * @param mixed $value The value of the association. |
||
| 921 | * @throws \InvalidArgumentException |
||
| 922 | */ |
||
| 923 | 449 | private function computeAssociationChanges($parentDocument, array $assoc, $value) |
|
| 1024 | |||
| 1025 | /** |
||
| 1026 | * INTERNAL: |
||
| 1027 | * Computes the changeset of an individual document, independently of the |
||
| 1028 | * computeChangeSets() routine that is used at the beginning of a UnitOfWork#commit(). |
||
| 1029 | * |
||
| 1030 | * The passed document must be a managed document. If the document already has a change set |
||
| 1031 | * because this method is invoked during a commit cycle then the change sets are added. |
||
| 1032 | * whereby changes detected in this method prevail. |
||
| 1033 | * |
||
| 1034 | * @ignore |
||
| 1035 | * @param ClassMetadata $class The class descriptor of the document. |
||
| 1036 | * @param object $document The document for which to (re)calculate the change set. |
||
| 1037 | * @throws \InvalidArgumentException If the passed document is not MANAGED. |
||
| 1038 | */ |
||
| 1039 | 20 | public function recomputeSingleDocumentChangeSet(ClassMetadata $class, $document) |
|
| 1058 | |||
| 1059 | /** |
||
| 1060 | * @param ClassMetadata $class |
||
| 1061 | * @param object $document |
||
| 1062 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1063 | */ |
||
| 1064 | 631 | private function persistNew(ClassMetadata $class, $document) |
|
| 1107 | |||
| 1108 | /** |
||
| 1109 | * Executes all document insertions for documents of the specified type. |
||
| 1110 | * |
||
| 1111 | * @param ClassMetadata $class |
||
| 1112 | * @param array $documents Array of documents to insert |
||
| 1113 | * @param array $options Array of options to be used with batchInsert() |
||
| 1114 | */ |
||
| 1115 | 524 | View Code Duplication | private function executeInserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Executes all document upserts for documents of the specified type. |
||
| 1133 | * |
||
| 1134 | * @param ClassMetadata $class |
||
| 1135 | * @param array $documents Array of documents to upsert |
||
| 1136 | * @param array $options Array of options to be used with batchInsert() |
||
| 1137 | */ |
||
| 1138 | 86 | View Code Duplication | private function executeUpserts(ClassMetadata $class, array $documents, array $options = array()) |
| 1154 | |||
| 1155 | /** |
||
| 1156 | * Executes all document updates for documents of the specified type. |
||
| 1157 | * |
||
| 1158 | * @param Mapping\ClassMetadata $class |
||
| 1159 | * @param array $documents Array of documents to update |
||
| 1160 | * @param array $options Array of options to be used with update() |
||
| 1161 | */ |
||
| 1162 | 230 | private function executeUpdates(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Executes all document deletions for documents of the specified type. |
||
| 1182 | * |
||
| 1183 | * @param ClassMetadata $class |
||
| 1184 | * @param array $documents Array of documents to delete |
||
| 1185 | * @param array $options Array of options to be used with remove() |
||
| 1186 | */ |
||
| 1187 | 69 | private function executeDeletions(ClassMetadata $class, array $documents, array $options = array()) |
|
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Schedules a document for insertion into the database. |
||
| 1222 | * If the document already has an identifier, it will be added to the |
||
| 1223 | * identity map. |
||
| 1224 | * |
||
| 1225 | * @param ClassMetadata $class |
||
| 1226 | * @param object $document The document to schedule for insertion. |
||
| 1227 | * @throws \InvalidArgumentException |
||
| 1228 | */ |
||
| 1229 | 560 | public function scheduleForInsert(ClassMetadata $class, $document) |
|
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Schedules a document for upsert into the database and adds it to the |
||
| 1252 | * identity map |
||
| 1253 | * |
||
| 1254 | * @param ClassMetadata $class |
||
| 1255 | * @param object $document The document to schedule for upsert. |
||
| 1256 | * @throws \InvalidArgumentException |
||
| 1257 | */ |
||
| 1258 | 92 | public function scheduleForUpsert(ClassMetadata $class, $document) |
|
| 1279 | |||
| 1280 | /** |
||
| 1281 | * Checks whether a document is scheduled for insertion. |
||
| 1282 | * |
||
| 1283 | * @param object $document |
||
| 1284 | * @return boolean |
||
| 1285 | */ |
||
| 1286 | 105 | public function isScheduledForInsert($document) |
|
| 1290 | |||
| 1291 | /** |
||
| 1292 | * Checks whether a document is scheduled for upsert. |
||
| 1293 | * |
||
| 1294 | * @param object $document |
||
| 1295 | * @return boolean |
||
| 1296 | */ |
||
| 1297 | 5 | public function isScheduledForUpsert($document) |
|
| 1301 | |||
| 1302 | /** |
||
| 1303 | * Schedules a document for being updated. |
||
| 1304 | * |
||
| 1305 | * @param object $document The document to schedule for being updated. |
||
| 1306 | * @throws \InvalidArgumentException |
||
| 1307 | */ |
||
| 1308 | 239 | public function scheduleForUpdate($document) |
|
| 1325 | |||
| 1326 | /** |
||
| 1327 | * Checks whether a document is registered as dirty in the unit of work. |
||
| 1328 | * Note: Is not very useful currently as dirty documents are only registered |
||
| 1329 | * at commit time. |
||
| 1330 | * |
||
| 1331 | * @param object $document |
||
| 1332 | * @return boolean |
||
| 1333 | */ |
||
| 1334 | 16 | public function isScheduledForUpdate($document) |
|
| 1338 | |||
| 1339 | 1 | public function isScheduledForDirtyCheck($document) |
|
| 1344 | |||
| 1345 | /** |
||
| 1346 | * INTERNAL: |
||
| 1347 | * Schedules a document for deletion. |
||
| 1348 | * |
||
| 1349 | * @param object $document |
||
| 1350 | */ |
||
| 1351 | 74 | public function scheduleForDelete($document) |
|
| 1377 | |||
| 1378 | /** |
||
| 1379 | * Checks whether a document is registered as removed/deleted with the unit |
||
| 1380 | * of work. |
||
| 1381 | * |
||
| 1382 | * @param object $document |
||
| 1383 | * @return boolean |
||
| 1384 | */ |
||
| 1385 | 8 | public function isScheduledForDelete($document) |
|
| 1389 | |||
| 1390 | /** |
||
| 1391 | * Checks whether a document is scheduled for insertion, update or deletion. |
||
| 1392 | * |
||
| 1393 | * @param $document |
||
| 1394 | * @return boolean |
||
| 1395 | */ |
||
| 1396 | 246 | public function isDocumentScheduled($document) |
|
| 1404 | |||
| 1405 | /** |
||
| 1406 | * INTERNAL: |
||
| 1407 | * Registers a document in the identity map. |
||
| 1408 | * |
||
| 1409 | * Note that documents in a hierarchy are registered with the class name of |
||
| 1410 | * the root document. Identifiers are serialized before being used as array |
||
| 1411 | * keys to allow differentiation of equal, but not identical, values. |
||
| 1412 | * |
||
| 1413 | * @ignore |
||
| 1414 | * @param object $document The document to register. |
||
| 1415 | * @return boolean TRUE if the registration was successful, FALSE if the identity of |
||
| 1416 | * the document in question is already managed. |
||
| 1417 | */ |
||
| 1418 | 660 | public function addToIdentityMap($document) |
|
| 1436 | |||
| 1437 | /** |
||
| 1438 | * Gets the state of a document with regard to the current unit of work. |
||
| 1439 | * |
||
| 1440 | * @param object $document |
||
| 1441 | * @param int|null $assume The state to assume if the state is not yet known (not MANAGED or REMOVED). |
||
| 1442 | * This parameter can be set to improve performance of document state detection |
||
| 1443 | * by potentially avoiding a database lookup if the distinction between NEW and DETACHED |
||
| 1444 | * is either known or does not matter for the caller of the method. |
||
| 1445 | * @return int The document state. |
||
| 1446 | */ |
||
| 1447 | 634 | public function getDocumentState($document, $assume = null) |
|
| 1497 | |||
| 1498 | /** |
||
| 1499 | * INTERNAL: |
||
| 1500 | * Removes a document from the identity map. This effectively detaches the |
||
| 1501 | * document from the persistence management of Doctrine. |
||
| 1502 | * |
||
| 1503 | * @ignore |
||
| 1504 | * @param object $document |
||
| 1505 | * @throws \InvalidArgumentException |
||
| 1506 | * @return boolean |
||
| 1507 | */ |
||
| 1508 | 87 | public function removeFromIdentityMap($document) |
|
| 1528 | |||
| 1529 | /** |
||
| 1530 | * INTERNAL: |
||
| 1531 | * Gets a document in the identity map by its identifier hash. |
||
| 1532 | * |
||
| 1533 | * @ignore |
||
| 1534 | * @param mixed $id Document identifier |
||
| 1535 | * @param ClassMetadata $class Document class |
||
| 1536 | * @return object |
||
| 1537 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1538 | */ |
||
| 1539 | 34 | public function getById($id, ClassMetadata $class) |
|
| 1549 | |||
| 1550 | /** |
||
| 1551 | * INTERNAL: |
||
| 1552 | * Tries to get a document by its identifier hash. If no document is found |
||
| 1553 | * for the given hash, FALSE is returned. |
||
| 1554 | * |
||
| 1555 | * @ignore |
||
| 1556 | * @param mixed $id Document identifier |
||
| 1557 | * @param ClassMetadata $class Document class |
||
| 1558 | * @return mixed The found document or FALSE. |
||
| 1559 | * @throws InvalidArgumentException if the class does not have an identifier |
||
| 1560 | */ |
||
| 1561 | 307 | public function tryGetById($id, ClassMetadata $class) |
|
| 1572 | |||
| 1573 | /** |
||
| 1574 | * Schedules a document for dirty-checking at commit-time. |
||
| 1575 | * |
||
| 1576 | * @param object $document The document to schedule for dirty-checking. |
||
| 1577 | * @todo Rename: scheduleForSynchronization |
||
| 1578 | */ |
||
| 1579 | 3 | public function scheduleForDirtyCheck($document) |
|
| 1584 | |||
| 1585 | /** |
||
| 1586 | * Checks whether a document is registered in the identity map. |
||
| 1587 | * |
||
| 1588 | * @param object $document |
||
| 1589 | * @return boolean |
||
| 1590 | */ |
||
| 1591 | 85 | public function isInIdentityMap($document) |
|
| 1604 | |||
| 1605 | /** |
||
| 1606 | * @param object $document |
||
| 1607 | * @return string |
||
| 1608 | */ |
||
| 1609 | 660 | private function getIdForIdentityMap($document) |
|
| 1622 | |||
| 1623 | /** |
||
| 1624 | * INTERNAL: |
||
| 1625 | * Checks whether an identifier exists in the identity map. |
||
| 1626 | * |
||
| 1627 | * @ignore |
||
| 1628 | * @param string $id |
||
| 1629 | * @param string $rootClassName |
||
| 1630 | * @return boolean |
||
| 1631 | */ |
||
| 1632 | public function containsId($id, $rootClassName) |
||
| 1636 | |||
| 1637 | /** |
||
| 1638 | * Persists a document as part of the current unit of work. |
||
| 1639 | * |
||
| 1640 | * @param object $document The document to persist. |
||
| 1641 | * @throws MongoDBException If trying to persist MappedSuperclass. |
||
| 1642 | * @throws \InvalidArgumentException If there is something wrong with document's identifier. |
||
| 1643 | */ |
||
| 1644 | 628 | public function persist($document) |
|
| 1645 | { |
||
| 1646 | 628 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 1647 | 628 | if ($class->isMappedSuperclass || $class->isQueryResultDocument) { |
|
| 1648 | 1 | throw MongoDBException::cannotPersistMappedSuperclass($class->name); |
|
| 1649 | } |
||
| 1650 | 627 | $visited = array(); |
|
| 1651 | 627 | $this->doPersist($document, $visited); |
|
| 1652 | 623 | } |
|
| 1653 | |||
| 1654 | /** |
||
| 1655 | * Saves a document as part of the current unit of work. |
||
| 1656 | * This method is internally called during save() cascades as it tracks |
||
| 1657 | * the already visited documents to prevent infinite recursions. |
||
| 1658 | * |
||
| 1659 | * NOTE: This method always considers documents that are not yet known to |
||
| 1660 | * this UnitOfWork as NEW. |
||
| 1661 | * |
||
| 1662 | * @param object $document The document to persist. |
||
| 1663 | * @param array $visited The already visited documents. |
||
| 1664 | * @throws \InvalidArgumentException |
||
| 1665 | * @throws MongoDBException |
||
| 1666 | */ |
||
| 1667 | 627 | private function doPersist($document, array &$visited) |
|
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Deletes a document as part of the current unit of work. |
||
| 1710 | * |
||
| 1711 | * @param object $document The document to remove. |
||
| 1712 | */ |
||
| 1713 | 73 | public function remove($document) |
|
| 1718 | |||
| 1719 | /** |
||
| 1720 | * Deletes a document as part of the current unit of work. |
||
| 1721 | * |
||
| 1722 | * This method is internally called during delete() cascades as it tracks |
||
| 1723 | * the already visited documents to prevent infinite recursions. |
||
| 1724 | * |
||
| 1725 | * @param object $document The document to delete. |
||
| 1726 | * @param array $visited The map of the already visited documents. |
||
| 1727 | * @throws MongoDBException |
||
| 1728 | */ |
||
| 1729 | 73 | private function doRemove($document, array &$visited) |
|
| 1761 | |||
| 1762 | /** |
||
| 1763 | * Merges the state of the given detached document into this UnitOfWork. |
||
| 1764 | * |
||
| 1765 | * @param object $document |
||
| 1766 | * @return object The managed copy of the document. |
||
| 1767 | */ |
||
| 1768 | 15 | public function merge($document) |
|
| 1774 | |||
| 1775 | /** |
||
| 1776 | * Executes a merge operation on a document. |
||
| 1777 | * |
||
| 1778 | * @param object $document |
||
| 1779 | * @param array $visited |
||
| 1780 | * @param object|null $prevManagedCopy |
||
| 1781 | * @param array|null $assoc |
||
| 1782 | * |
||
| 1783 | * @return object The managed copy of the document. |
||
| 1784 | * |
||
| 1785 | * @throws InvalidArgumentException If the entity instance is NEW. |
||
| 1786 | * @throws LockException If the document uses optimistic locking through a |
||
| 1787 | * version attribute and the version check against the |
||
| 1788 | * managed copy fails. |
||
| 1789 | */ |
||
| 1790 | 15 | private function doMerge($document, array &$visited, $prevManagedCopy = null, $assoc = null) |
|
| 1964 | |||
| 1965 | /** |
||
| 1966 | * Detaches a document from the persistence management. It's persistence will |
||
| 1967 | * no longer be managed by Doctrine. |
||
| 1968 | * |
||
| 1969 | * @param object $document The document to detach. |
||
| 1970 | */ |
||
| 1971 | 11 | public function detach($document) |
|
| 1976 | |||
| 1977 | /** |
||
| 1978 | * Executes a detach operation on the given document. |
||
| 1979 | * |
||
| 1980 | * @param object $document |
||
| 1981 | * @param array $visited |
||
| 1982 | * @internal This method always considers documents with an assigned identifier as DETACHED. |
||
| 1983 | */ |
||
| 1984 | 16 | private function doDetach($document, array &$visited) |
|
| 2009 | |||
| 2010 | /** |
||
| 2011 | * Refreshes the state of the given document from the database, overwriting |
||
| 2012 | * any local, unpersisted changes. |
||
| 2013 | * |
||
| 2014 | * @param object $document The document to refresh. |
||
| 2015 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2016 | */ |
||
| 2017 | 21 | public function refresh($document) |
|
| 2022 | |||
| 2023 | /** |
||
| 2024 | * Executes a refresh operation on a document. |
||
| 2025 | * |
||
| 2026 | * @param object $document The document to refresh. |
||
| 2027 | * @param array $visited The already visited documents during cascades. |
||
| 2028 | * @throws \InvalidArgumentException If the document is not MANAGED. |
||
| 2029 | */ |
||
| 2030 | 21 | private function doRefresh($document, array &$visited) |
|
| 2052 | |||
| 2053 | /** |
||
| 2054 | * Cascades a refresh operation to associated documents. |
||
| 2055 | * |
||
| 2056 | * @param object $document |
||
| 2057 | * @param array $visited |
||
| 2058 | */ |
||
| 2059 | 20 | private function cascadeRefresh($document, array &$visited) |
|
| 2083 | |||
| 2084 | /** |
||
| 2085 | * Cascades a detach operation to associated documents. |
||
| 2086 | * |
||
| 2087 | * @param object $document |
||
| 2088 | * @param array $visited |
||
| 2089 | */ |
||
| 2090 | 16 | View Code Duplication | private function cascadeDetach($document, array &$visited) |
| 2091 | { |
||
| 2092 | 16 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 2093 | 16 | foreach ($class->fieldMappings as $mapping) { |
|
| 2094 | 16 | if ( ! $mapping['isCascadeDetach']) { |
|
| 2095 | 16 | continue; |
|
| 2096 | } |
||
| 2097 | 11 | $relatedDocuments = $class->reflFields[$mapping['fieldName']]->getValue($document); |
|
| 2098 | 11 | if (($relatedDocuments instanceof Collection || is_array($relatedDocuments))) { |
|
| 2099 | 11 | if ($relatedDocuments instanceof PersistentCollectionInterface) { |
|
| 2100 | // Unwrap so that foreach() does not initialize |
||
| 2101 | 8 | $relatedDocuments = $relatedDocuments->unwrap(); |
|
| 2102 | } |
||
| 2103 | 11 | foreach ($relatedDocuments as $relatedDocument) { |
|
| 2104 | 11 | $this->doDetach($relatedDocument, $visited); |
|
| 2105 | } |
||
| 2106 | 11 | } elseif ($relatedDocuments !== null) { |
|
| 2107 | 11 | $this->doDetach($relatedDocuments, $visited); |
|
| 2108 | } |
||
| 2109 | } |
||
| 2110 | 16 | } |
|
| 2111 | /** |
||
| 2112 | * Cascades a merge operation to associated documents. |
||
| 2113 | * |
||
| 2114 | * @param object $document |
||
| 2115 | * @param object $managedCopy |
||
| 2116 | * @param array $visited |
||
| 2117 | */ |
||
| 2118 | 15 | private function cascadeMerge($document, $managedCopy, array &$visited) |
|
| 2144 | |||
| 2145 | /** |
||
| 2146 | * Cascades the save operation to associated documents. |
||
| 2147 | * |
||
| 2148 | * @param object $document |
||
| 2149 | * @param array $visited |
||
| 2150 | */ |
||
| 2151 | 625 | private function cascadePersist($document, array &$visited) |
|
| 2198 | |||
| 2199 | /** |
||
| 2200 | * Cascades the delete operation to associated documents. |
||
| 2201 | * |
||
| 2202 | * @param object $document |
||
| 2203 | * @param array $visited |
||
| 2204 | */ |
||
| 2205 | 73 | View Code Duplication | private function cascadeRemove($document, array &$visited) |
| 2227 | |||
| 2228 | /** |
||
| 2229 | * Acquire a lock on the given document. |
||
| 2230 | * |
||
| 2231 | * @param object $document |
||
| 2232 | * @param int $lockMode |
||
| 2233 | * @param int $lockVersion |
||
| 2234 | * @throws LockException |
||
| 2235 | * @throws \InvalidArgumentException |
||
| 2236 | */ |
||
| 2237 | 9 | public function lock($document, $lockMode, $lockVersion = null) |
|
| 2261 | |||
| 2262 | /** |
||
| 2263 | * Releases a lock on the given document. |
||
| 2264 | * |
||
| 2265 | * @param object $document |
||
| 2266 | * @throws \InvalidArgumentException |
||
| 2267 | */ |
||
| 2268 | 1 | public function unlock($document) |
|
| 2276 | |||
| 2277 | /** |
||
| 2278 | * Clears the UnitOfWork. |
||
| 2279 | * |
||
| 2280 | * @param string|null $documentName if given, only documents of this type will get detached. |
||
| 2281 | */ |
||
| 2282 | 410 | public function clear($documentName = null) |
|
| 2316 | |||
| 2317 | /** |
||
| 2318 | * INTERNAL: |
||
| 2319 | * Schedules an embedded document for removal. The remove() operation will be |
||
| 2320 | * invoked on that document at the beginning of the next commit of this |
||
| 2321 | * UnitOfWork. |
||
| 2322 | * |
||
| 2323 | * @ignore |
||
| 2324 | * @param object $document |
||
| 2325 | */ |
||
| 2326 | 51 | public function scheduleOrphanRemoval($document) |
|
| 2330 | |||
| 2331 | /** |
||
| 2332 | * INTERNAL: |
||
| 2333 | * Unschedules an embedded or referenced object for removal. |
||
| 2334 | * |
||
| 2335 | * @ignore |
||
| 2336 | * @param object $document |
||
| 2337 | */ |
||
| 2338 | 112 | public function unscheduleOrphanRemoval($document) |
|
| 2345 | |||
| 2346 | /** |
||
| 2347 | * Fixes PersistentCollection state if it wasn't used exactly as we had in mind: |
||
| 2348 | * 1) sets owner if it was cloned |
||
| 2349 | * 2) clones collection, sets owner, updates document's property and, if necessary, updates originalData |
||
| 2350 | * 3) NOP if state is OK |
||
| 2351 | * Returned collection should be used from now on (only important with 2nd point) |
||
| 2352 | * |
||
| 2353 | * @param PersistentCollectionInterface $coll |
||
| 2354 | * @param object $document |
||
| 2355 | * @param ClassMetadata $class |
||
| 2356 | * @param string $propName |
||
| 2357 | * @return PersistentCollectionInterface |
||
| 2358 | */ |
||
| 2359 | 8 | private function fixPersistentCollectionOwnership(PersistentCollectionInterface $coll, $document, ClassMetadata $class, $propName) |
|
| 2379 | |||
| 2380 | /** |
||
| 2381 | * INTERNAL: |
||
| 2382 | * Schedules a complete collection for removal when this UnitOfWork commits. |
||
| 2383 | * |
||
| 2384 | * @param PersistentCollectionInterface $coll |
||
| 2385 | */ |
||
| 2386 | 42 | public function scheduleCollectionDeletion(PersistentCollectionInterface $coll) |
|
| 2395 | |||
| 2396 | /** |
||
| 2397 | * Checks whether a PersistentCollection is scheduled for deletion. |
||
| 2398 | * |
||
| 2399 | * @param PersistentCollectionInterface $coll |
||
| 2400 | * @return boolean |
||
| 2401 | */ |
||
| 2402 | 218 | public function isCollectionScheduledForDeletion(PersistentCollectionInterface $coll) |
|
| 2406 | |||
| 2407 | /** |
||
| 2408 | * INTERNAL: |
||
| 2409 | * Unschedules a collection from being deleted when this UnitOfWork commits. |
||
| 2410 | * |
||
| 2411 | * @param PersistentCollectionInterface $coll |
||
| 2412 | */ |
||
| 2413 | 222 | View Code Duplication | public function unscheduleCollectionDeletion(PersistentCollectionInterface $coll) |
| 2414 | { |
||
| 2415 | 222 | $oid = spl_object_hash($coll); |
|
| 2416 | 222 | if (isset($this->collectionDeletions[$oid])) { |
|
| 2417 | 12 | $topmostOwner = $this->getOwningDocument($coll->getOwner()); |
|
| 2418 | 12 | unset($this->collectionDeletions[$oid]); |
|
| 2419 | 12 | unset($this->hasScheduledCollections[spl_object_hash($topmostOwner)][$oid]); |
|
| 2420 | } |
||
| 2421 | 222 | } |
|
| 2422 | |||
| 2423 | /** |
||
| 2424 | * INTERNAL: |
||
| 2425 | * Schedules a collection for update when this UnitOfWork commits. |
||
| 2426 | * |
||
| 2427 | * @param PersistentCollectionInterface $coll |
||
| 2428 | */ |
||
| 2429 | 243 | public function scheduleCollectionUpdate(PersistentCollectionInterface $coll) |
|
| 2430 | { |
||
| 2431 | 243 | $mapping = $coll->getMapping(); |
|
| 2432 | 243 | if (CollectionHelper::usesSet($mapping['strategy'])) { |
|
| 2433 | /* There is no need to $unset collection if it will be $set later |
||
| 2434 | * This is NOP if collection is not scheduled for deletion |
||
| 2435 | */ |
||
| 2436 | 41 | $this->unscheduleCollectionDeletion($coll); |
|
| 2437 | } |
||
| 2438 | 243 | $oid = spl_object_hash($coll); |
|
| 2439 | 243 | if ( ! isset($this->collectionUpdates[$oid])) { |
|
| 2440 | 243 | $this->collectionUpdates[$oid] = $coll; |
|
| 2441 | 243 | $this->scheduleCollectionOwner($coll); |
|
| 2442 | } |
||
| 2443 | 243 | } |
|
| 2444 | |||
| 2445 | /** |
||
| 2446 | * INTERNAL: |
||
| 2447 | * Unschedules a collection from being updated when this UnitOfWork commits. |
||
| 2448 | * |
||
| 2449 | * @param PersistentCollectionInterface $coll |
||
| 2450 | */ |
||
| 2451 | 222 | View Code Duplication | public function unscheduleCollectionUpdate(PersistentCollectionInterface $coll) |
| 2452 | { |
||
| 2453 | 222 | $oid = spl_object_hash($coll); |
|
| 2454 | 222 | if (isset($this->collectionUpdates[$oid])) { |
|
| 2455 | 212 | $topmostOwner = $this->getOwningDocument($coll->getOwner()); |
|
| 2456 | 212 | unset($this->collectionUpdates[$oid]); |
|
| 2457 | 212 | unset($this->hasScheduledCollections[spl_object_hash($topmostOwner)][$oid]); |
|
| 2458 | } |
||
| 2459 | 222 | } |
|
| 2460 | |||
| 2461 | /** |
||
| 2462 | * Checks whether a PersistentCollection is scheduled for update. |
||
| 2463 | * |
||
| 2464 | * @param PersistentCollectionInterface $coll |
||
| 2465 | * @return boolean |
||
| 2466 | */ |
||
| 2467 | 131 | public function isCollectionScheduledForUpdate(PersistentCollectionInterface $coll) |
|
| 2471 | |||
| 2472 | /** |
||
| 2473 | * INTERNAL: |
||
| 2474 | * Gets PersistentCollections that have been visited during computing change |
||
| 2475 | * set of $document |
||
| 2476 | * |
||
| 2477 | * @param object $document |
||
| 2478 | * @return PersistentCollectionInterface[] |
||
| 2479 | */ |
||
| 2480 | 584 | public function getVisitedCollections($document) |
|
| 2487 | |||
| 2488 | /** |
||
| 2489 | * INTERNAL: |
||
| 2490 | * Gets PersistentCollections that are scheduled to update and related to $document |
||
| 2491 | * |
||
| 2492 | * @param object $document |
||
| 2493 | * @return array |
||
| 2494 | */ |
||
| 2495 | 584 | public function getScheduledCollections($document) |
|
| 2502 | |||
| 2503 | /** |
||
| 2504 | * Checks whether the document is related to a PersistentCollection |
||
| 2505 | * scheduled for update or deletion. |
||
| 2506 | * |
||
| 2507 | * @param object $document |
||
| 2508 | * @return boolean |
||
| 2509 | */ |
||
| 2510 | 52 | public function hasScheduledCollections($document) |
|
| 2514 | |||
| 2515 | /** |
||
| 2516 | * Marks the PersistentCollection's top-level owner as having a relation to |
||
| 2517 | * a collection scheduled for update or deletion. |
||
| 2518 | * |
||
| 2519 | * If the owner is not scheduled for any lifecycle action, it will be |
||
| 2520 | * scheduled for update to ensure that versioning takes place if necessary. |
||
| 2521 | * |
||
| 2522 | * If the collection is nested within atomic collection, it is immediately |
||
| 2523 | * unscheduled and atomic one is scheduled for update instead. This makes |
||
| 2524 | * calculating update data way easier. |
||
| 2525 | * |
||
| 2526 | * @param PersistentCollectionInterface $coll |
||
| 2527 | */ |
||
| 2528 | 245 | private function scheduleCollectionOwner(PersistentCollectionInterface $coll) |
|
| 2529 | { |
||
| 2530 | 245 | $document = $this->getOwningDocument($coll->getOwner()); |
|
| 2531 | 245 | $this->hasScheduledCollections[spl_object_hash($document)][spl_object_hash($coll)] = $coll; |
|
| 2532 | |||
| 2533 | 245 | if ($document !== $coll->getOwner()) { |
|
| 2534 | 25 | $parent = $coll->getOwner(); |
|
| 2535 | 25 | while (null !== ($parentAssoc = $this->getParentAssociation($parent))) { |
|
| 2536 | 25 | list($mapping, $parent, ) = $parentAssoc; |
|
| 2537 | } |
||
| 2538 | 25 | if (CollectionHelper::isAtomic($mapping['strategy'])) { |
|
| 2539 | 8 | $class = $this->dm->getClassMetadata(get_class($document)); |
|
| 2540 | 8 | $atomicCollection = $class->getFieldValue($document, $mapping['fieldName']); |
|
| 2541 | 8 | $this->scheduleCollectionUpdate($atomicCollection); |
|
| 2542 | 8 | $this->unscheduleCollectionDeletion($coll); |
|
| 2543 | 8 | $this->unscheduleCollectionUpdate($coll); |
|
| 2544 | } |
||
| 2545 | } |
||
| 2546 | |||
| 2547 | 245 | if ( ! $this->isDocumentScheduled($document)) { |
|
| 2548 | 49 | $this->scheduleForUpdate($document); |
|
| 2549 | } |
||
| 2550 | 245 | } |
|
| 2551 | |||
| 2552 | /** |
||
| 2553 | * Get the top-most owning document of a given document |
||
| 2554 | * |
||
| 2555 | * If a top-level document is provided, that same document will be returned. |
||
| 2556 | * For an embedded document, we will walk through parent associations until |
||
| 2557 | * we find a top-level document. |
||
| 2558 | * |
||
| 2559 | * @param object $document |
||
| 2560 | * @throws \UnexpectedValueException when a top-level document could not be found |
||
| 2561 | * @return object |
||
| 2562 | */ |
||
| 2563 | 247 | public function getOwningDocument($document) |
|
| 2579 | |||
| 2580 | /** |
||
| 2581 | * Gets the class name for an association (embed or reference) with respect |
||
| 2582 | * to any discriminator value. |
||
| 2583 | * |
||
| 2584 | * @param array $mapping Field mapping for the association |
||
| 2585 | * @param array|null $data Data for the embedded document or reference |
||
| 2586 | * @return string Class name. |
||
| 2587 | */ |
||
| 2588 | 221 | public function getClassNameForAssociation(array $mapping, $data) |
|
| 2621 | |||
| 2622 | /** |
||
| 2623 | * INTERNAL: |
||
| 2624 | * Creates a document. Used for reconstitution of documents during hydration. |
||
| 2625 | * |
||
| 2626 | * @ignore |
||
| 2627 | * @param string $className The name of the document class. |
||
| 2628 | * @param array $data The data for the document. |
||
| 2629 | * @param array $hints Any hints to account for during reconstitution/lookup of the document. |
||
| 2630 | * @param object $document The document to be hydrated into in case of creation |
||
| 2631 | * @return object The document instance. |
||
| 2632 | * @internal Highly performance-sensitive method. |
||
| 2633 | */ |
||
| 2634 | 412 | public function getOrCreateDocument($className, $data, &$hints = array(), $document = null) |
|
| 2635 | { |
||
| 2636 | 412 | $class = $this->dm->getClassMetadata($className); |
|
| 2637 | |||
| 2638 | // @TODO figure out how to remove this |
||
| 2639 | 412 | $discriminatorValue = null; |
|
| 2640 | 412 | View Code Duplication | if (isset($class->discriminatorField, $data[$class->discriminatorField])) { |
| 2641 | 19 | $discriminatorValue = $data[$class->discriminatorField]; |
|
| 2642 | } elseif (isset($class->defaultDiscriminatorValue)) { |
||
| 2643 | 2 | $discriminatorValue = $class->defaultDiscriminatorValue; |
|
| 2644 | } |
||
| 2645 | |||
| 2646 | 412 | if ($discriminatorValue !== null) { |
|
| 2647 | 20 | $className = isset($class->discriminatorMap[$discriminatorValue]) |
|
| 2648 | 18 | ? $class->discriminatorMap[$discriminatorValue] |
|
| 2649 | 20 | : $discriminatorValue; |
|
| 2650 | |||
| 2651 | 20 | $class = $this->dm->getClassMetadata($className); |
|
| 2652 | |||
| 2653 | 20 | unset($data[$class->discriminatorField]); |
|
| 2654 | } |
||
| 2655 | |||
| 2656 | 412 | if (! empty($hints[Query::HINT_READ_ONLY])) { |
|
| 2657 | 2 | $document = $class->newInstance(); |
|
| 2658 | 2 | $this->hydratorFactory->hydrate($document, $data, $hints); |
|
| 2659 | 2 | return $document; |
|
| 2660 | } |
||
| 2661 | |||
| 2662 | 411 | $isManagedObject = false; |
|
| 2663 | 411 | if (! $class->isQueryResultDocument) { |
|
| 2664 | 411 | $id = $class->getDatabaseIdentifierValue($data['_id']); |
|
| 2665 | 411 | $serializedId = serialize($id); |
|
| 2666 | 411 | $isManagedObject = isset($this->identityMap[$class->name][$serializedId]); |
|
| 2667 | } |
||
| 2668 | |||
| 2669 | 411 | if ($isManagedObject) { |
|
| 2670 | 104 | $document = $this->identityMap[$class->name][$serializedId]; |
|
| 2671 | 104 | $oid = spl_object_hash($document); |
|
| 2672 | 104 | if ($document instanceof Proxy && ! $document->__isInitialized__) { |
|
| 2673 | 12 | $document->__isInitialized__ = true; |
|
| 2674 | 12 | $overrideLocalValues = true; |
|
| 2675 | 12 | if ($document instanceof NotifyPropertyChanged) { |
|
| 2676 | 12 | $document->addPropertyChangedListener($this); |
|
| 2677 | } |
||
| 2678 | } else { |
||
| 2679 | 100 | $overrideLocalValues = ! empty($hints[Query::HINT_REFRESH]); |
|
| 2680 | } |
||
| 2681 | 104 | if ($overrideLocalValues) { |
|
| 2682 | 50 | $data = $this->hydratorFactory->hydrate($document, $data, $hints); |
|
| 2683 | 104 | $this->originalDocumentData[$oid] = $data; |
|
| 2684 | } |
||
| 2685 | } else { |
||
| 2686 | 373 | if ($document === null) { |
|
| 2687 | 373 | $document = $class->newInstance(); |
|
| 2688 | } |
||
| 2689 | |||
| 2690 | 373 | if (! $class->isQueryResultDocument) { |
|
| 2691 | 372 | $this->registerManaged($document, $id, $data); |
|
| 2692 | 372 | $oid = spl_object_hash($document); |
|
| 2693 | 372 | $this->documentStates[$oid] = self::STATE_MANAGED; |
|
| 2694 | 372 | $this->identityMap[$class->name][$serializedId] = $document; |
|
| 2695 | } |
||
| 2696 | |||
| 2697 | 373 | $data = $this->hydratorFactory->hydrate($document, $data, $hints); |
|
| 2698 | |||
| 2699 | 373 | if (! $class->isQueryResultDocument) { |
|
| 2700 | 372 | $this->originalDocumentData[$oid] = $data; |
|
| 2701 | } |
||
| 2702 | } |
||
| 2703 | |||
| 2704 | 411 | return $document; |
|
| 2705 | } |
||
| 2706 | |||
| 2707 | /** |
||
| 2708 | * Initializes (loads) an uninitialized persistent collection of a document. |
||
| 2709 | * |
||
| 2710 | * @param PersistentCollectionInterface $collection The collection to initialize. |
||
| 2711 | */ |
||
| 2712 | 168 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 2717 | |||
| 2718 | /** |
||
| 2719 | * Gets the identity map of the UnitOfWork. |
||
| 2720 | * |
||
| 2721 | * @return array |
||
| 2722 | */ |
||
| 2723 | public function getIdentityMap() |
||
| 2727 | |||
| 2728 | /** |
||
| 2729 | * Gets the original data of a document. The original data is the data that was |
||
| 2730 | * present at the time the document was reconstituted from the database. |
||
| 2731 | * |
||
| 2732 | * @param object $document |
||
| 2733 | * @return array |
||
| 2734 | */ |
||
| 2735 | 1 | public function getOriginalDocumentData($document) |
|
| 2743 | |||
| 2744 | /** |
||
| 2745 | * @ignore |
||
| 2746 | */ |
||
| 2747 | 54 | public function setOriginalDocumentData($document, array $data) |
|
| 2753 | |||
| 2754 | /** |
||
| 2755 | * INTERNAL: |
||
| 2756 | * Sets a property value of the original data array of a document. |
||
| 2757 | * |
||
| 2758 | * @ignore |
||
| 2759 | * @param string $oid |
||
| 2760 | * @param string $property |
||
| 2761 | * @param mixed $value |
||
| 2762 | */ |
||
| 2763 | 4 | public function setOriginalDocumentProperty($oid, $property, $value) |
|
| 2767 | |||
| 2768 | /** |
||
| 2769 | * Gets the identifier of a document. |
||
| 2770 | * |
||
| 2771 | * @param object $document |
||
| 2772 | * @return mixed The identifier value |
||
| 2773 | */ |
||
| 2774 | 424 | public function getDocumentIdentifier($document) |
|
| 2779 | |||
| 2780 | /** |
||
| 2781 | * Checks whether the UnitOfWork has any pending insertions. |
||
| 2782 | * |
||
| 2783 | * @return boolean TRUE if this UnitOfWork has pending insertions, FALSE otherwise. |
||
| 2784 | */ |
||
| 2785 | public function hasPendingInsertions() |
||
| 2789 | |||
| 2790 | /** |
||
| 2791 | * Calculates the size of the UnitOfWork. The size of the UnitOfWork is the |
||
| 2792 | * number of documents in the identity map. |
||
| 2793 | * |
||
| 2794 | * @return integer |
||
| 2795 | */ |
||
| 2796 | 2 | public function size() |
|
| 2804 | |||
| 2805 | /** |
||
| 2806 | * INTERNAL: |
||
| 2807 | * Registers a document as managed. |
||
| 2808 | * |
||
| 2809 | * TODO: This method assumes that $id is a valid PHP identifier for the |
||
| 2810 | * document class. If the class expects its database identifier to be a |
||
| 2811 | * MongoId, and an incompatible $id is registered (e.g. an integer), the |
||
| 2812 | * document identifiers map will become inconsistent with the identity map. |
||
| 2813 | * In the future, we may want to round-trip $id through a PHP and database |
||
| 2814 | * conversion and throw an exception if it's inconsistent. |
||
| 2815 | * |
||
| 2816 | * @param object $document The document. |
||
| 2817 | * @param array $id The identifier values. |
||
| 2818 | * @param array $data The original document data. |
||
| 2819 | */ |
||
| 2820 | 395 | public function registerManaged($document, $id, array $data) |
|
| 2835 | |||
| 2836 | /** |
||
| 2837 | * INTERNAL: |
||
| 2838 | * Clears the property changeset of the document with the given OID. |
||
| 2839 | * |
||
| 2840 | * @param string $oid The document's OID. |
||
| 2841 | */ |
||
| 2842 | 1 | public function clearDocumentChangeSet($oid) |
|
| 2846 | |||
| 2847 | /* PropertyChangedListener implementation */ |
||
| 2848 | |||
| 2849 | /** |
||
| 2850 | * Notifies this UnitOfWork of a property change in a document. |
||
| 2851 | * |
||
| 2852 | * @param object $document The document that owns the property. |
||
| 2853 | * @param string $propertyName The name of the property that changed. |
||
| 2854 | * @param mixed $oldValue The old value of the property. |
||
| 2855 | * @param mixed $newValue The new value of the property. |
||
| 2856 | */ |
||
| 2857 | 2 | public function propertyChanged($document, $propertyName, $oldValue, $newValue) |
|
| 2872 | |||
| 2873 | /** |
||
| 2874 | * Gets the currently scheduled document insertions in this UnitOfWork. |
||
| 2875 | * |
||
| 2876 | * @return array |
||
| 2877 | */ |
||
| 2878 | 5 | public function getScheduledDocumentInsertions() |
|
| 2882 | |||
| 2883 | /** |
||
| 2884 | * Gets the currently scheduled document upserts in this UnitOfWork. |
||
| 2885 | * |
||
| 2886 | * @return array |
||
| 2887 | */ |
||
| 2888 | 3 | public function getScheduledDocumentUpserts() |
|
| 2892 | |||
| 2893 | /** |
||
| 2894 | * Gets the currently scheduled document updates in this UnitOfWork. |
||
| 2895 | * |
||
| 2896 | * @return array |
||
| 2897 | */ |
||
| 2898 | 3 | public function getScheduledDocumentUpdates() |
|
| 2902 | |||
| 2903 | /** |
||
| 2904 | * Gets the currently scheduled document deletions in this UnitOfWork. |
||
| 2905 | * |
||
| 2906 | * @return array |
||
| 2907 | */ |
||
| 2908 | public function getScheduledDocumentDeletions() |
||
| 2912 | |||
| 2913 | /** |
||
| 2914 | * Get the currently scheduled complete collection deletions |
||
| 2915 | * |
||
| 2916 | * @return array |
||
| 2917 | */ |
||
| 2918 | public function getScheduledCollectionDeletions() |
||
| 2922 | |||
| 2923 | /** |
||
| 2924 | * Gets the currently scheduled collection inserts, updates and deletes. |
||
| 2925 | * |
||
| 2926 | * @return array |
||
| 2927 | */ |
||
| 2928 | public function getScheduledCollectionUpdates() |
||
| 2932 | |||
| 2933 | /** |
||
| 2934 | * Helper method to initialize a lazy loading proxy or persistent collection. |
||
| 2935 | * |
||
| 2936 | * @param object |
||
| 2937 | * @return void |
||
| 2938 | */ |
||
| 2939 | public function initializeObject($obj) |
||
| 2947 | |||
| 2948 | 1 | private function objToStr($obj) |
|
| 2952 | } |
||
| 2953 |
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.