Complex classes like DocumentPersister 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 DocumentPersister, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 59 | class DocumentPersister |
||
| 60 | { |
||
| 61 | /** |
||
| 62 | * The PersistenceBuilder instance. |
||
| 63 | * |
||
| 64 | * @var PersistenceBuilder |
||
| 65 | */ |
||
| 66 | private $pb; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The DocumentManager instance. |
||
| 70 | * |
||
| 71 | * @var DocumentManager |
||
| 72 | */ |
||
| 73 | private $dm; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The UnitOfWork instance. |
||
| 77 | * |
||
| 78 | * @var UnitOfWork |
||
| 79 | */ |
||
| 80 | private $uow; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The ClassMetadata instance for the document type being persisted. |
||
| 84 | * |
||
| 85 | * @var ClassMetadata |
||
| 86 | */ |
||
| 87 | private $class; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The MongoCollection instance for this document. |
||
| 91 | * |
||
| 92 | * @var Collection |
||
| 93 | */ |
||
| 94 | private $collection; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Array of queued inserts for the persister to insert. |
||
| 98 | * |
||
| 99 | * @var array |
||
| 100 | */ |
||
| 101 | private $queuedInserts = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Array of queued inserts for the persister to insert. |
||
| 105 | * |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | private $queuedUpserts = []; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The CriteriaMerger instance. |
||
| 112 | * |
||
| 113 | * @var CriteriaMerger |
||
| 114 | */ |
||
| 115 | private $cm; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The CollectionPersister instance. |
||
| 119 | * |
||
| 120 | * @var CollectionPersister |
||
| 121 | */ |
||
| 122 | private $cp; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The HydratorFactory instance. |
||
| 126 | * |
||
| 127 | * @var HydratorFactory |
||
| 128 | */ |
||
| 129 | private $hydratorFactory; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Initializes this instance. |
||
| 133 | * |
||
| 134 | */ |
||
| 135 | 1074 | public function __construct( |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | public function getInserts() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param object $document |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function isQueuedForInsert($document) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Adds a document to the queued insertions. |
||
| 172 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 173 | * |
||
| 174 | * @param object $document The document to queue for insertion. |
||
| 175 | */ |
||
| 176 | 477 | public function addInsert($document) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | public function getUpserts() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param object $document |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function isQueuedForUpsert($document) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds a document to the queued upserts. |
||
| 200 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 201 | * |
||
| 202 | * @param object $document The document to queue for insertion. |
||
| 203 | */ |
||
| 204 | 83 | public function addUpsert($document) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 211 | * |
||
| 212 | * @return ClassMetadata |
||
| 213 | */ |
||
| 214 | public function getClassMetadata() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Executes all queued document insertions. |
||
| 221 | * |
||
| 222 | * Queued documents without an ID will inserted in a batch and queued |
||
| 223 | * documents with an ID will be upserted individually. |
||
| 224 | * |
||
| 225 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 226 | * |
||
| 227 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 228 | */ |
||
| 229 | 477 | public function executeInserts(array $options = []) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Executes all queued document upserts. |
||
| 280 | * |
||
| 281 | * Queued documents with an ID are upserted individually. |
||
| 282 | * |
||
| 283 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 284 | * |
||
| 285 | * @param array $options Options for batchInsert() and update() driver methods |
||
| 286 | */ |
||
| 287 | 83 | public function executeUpserts(array $options = []) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Executes a single upsert in {@link executeUpserts} |
||
| 308 | * |
||
| 309 | * @param object $document |
||
| 310 | * @param array $options |
||
| 311 | */ |
||
| 312 | 83 | private function executeUpsert($document, array $options) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Updates the already persisted document if it has any new changesets. |
||
| 381 | * |
||
| 382 | * @param object $document |
||
| 383 | * @param array $options Array of options to be used with update() |
||
| 384 | * @throws LockException |
||
| 385 | */ |
||
| 386 | 196 | public function update($document, array $options = []) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Removes document from mongo |
||
| 447 | * |
||
| 448 | * @param mixed $document |
||
| 449 | * @param array $options Array of options to be used with remove() |
||
| 450 | * @throws LockException |
||
| 451 | */ |
||
| 452 | 32 | public function delete($document, array $options = []) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Refreshes a managed document. |
||
| 471 | * |
||
| 472 | * @param object $document The document to refresh. |
||
| 473 | */ |
||
| 474 | 20 | public function refresh($document) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Finds a document by a set of criteria. |
||
| 484 | * |
||
| 485 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 486 | * be used to match an _id value. |
||
| 487 | * |
||
| 488 | * @param mixed $criteria Query criteria |
||
| 489 | * @param object $document Document to load the data into. If not specified, a new document is created. |
||
| 490 | * @param array $hints Hints for document creation |
||
| 491 | * @param int $lockMode |
||
| 492 | * @param array $sort Sort array for Cursor::sort() |
||
| 493 | * @throws LockException |
||
| 494 | * @return object|null The loaded and managed document instance or null if no document was found |
||
| 495 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 496 | */ |
||
| 497 | 342 | public function load($criteria, $document = null, array $hints = [], $lockMode = 0, ?array $sort = null) |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Finds documents by a set of criteria. |
||
| 526 | * |
||
| 527 | * @param array $criteria Query criteria |
||
| 528 | * @param array $sort Sort array for Cursor::sort() |
||
| 529 | * @param int|null $limit Limit for Cursor::limit() |
||
| 530 | * @param int|null $skip Skip for Cursor::skip() |
||
| 531 | * @return Iterator |
||
| 532 | */ |
||
| 533 | 22 | public function loadAll(array $criteria = [], ?array $sort = null, $limit = null, $skip = null) |
|
| 557 | |||
| 558 | /** |
||
| 559 | * @param object $document |
||
| 560 | * |
||
| 561 | * @return array |
||
| 562 | * @throws MongoDBException |
||
| 563 | */ |
||
| 564 | 268 | private function getShardKeyQuery($document) |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 600 | * |
||
| 601 | */ |
||
| 602 | 22 | private function wrapCursor(Cursor $baseCursor): Iterator |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Checks whether the given managed document exists in the database. |
||
| 609 | * |
||
| 610 | * @param object $document |
||
| 611 | * @return bool TRUE if the document exists in the database, FALSE otherwise. |
||
| 612 | */ |
||
| 613 | 3 | public function exists($document) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 621 | * |
||
| 622 | * @param object $document |
||
| 623 | * @param int $lockMode |
||
| 624 | */ |
||
| 625 | 5 | public function lock($document, $lockMode) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Releases any lock that exists on this document. |
||
| 636 | * |
||
| 637 | * @param object $document |
||
| 638 | */ |
||
| 639 | 1 | public function unlock($document) |
|
| 647 | |||
| 648 | /** |
||
| 649 | * Creates or fills a single document object from an query result. |
||
| 650 | * |
||
| 651 | * @param object $result The query result. |
||
| 652 | * @param object $document The document object to fill, if any. |
||
| 653 | * @param array $hints Hints for document creation. |
||
| 654 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 655 | */ |
||
| 656 | 341 | private function createDocument($result, $document = null, array $hints = []) |
|
| 670 | |||
| 671 | /** |
||
| 672 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 673 | * |
||
| 674 | */ |
||
| 675 | 164 | public function loadCollection(PersistentCollectionInterface $collection) |
|
| 696 | |||
| 697 | 109 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection) |
|
| 726 | |||
| 727 | 61 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection) |
|
| 800 | |||
| 801 | 17 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection) |
|
| 809 | |||
| 810 | /** |
||
| 811 | * |
||
| 812 | * @return Query |
||
| 813 | */ |
||
| 814 | 17 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection) |
|
| 853 | |||
| 854 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection) |
|
| 867 | |||
| 868 | /** |
||
| 869 | * |
||
| 870 | * @return \Iterator |
||
| 871 | */ |
||
| 872 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection) |
|
| 893 | |||
| 894 | /** |
||
| 895 | * Prepare a projection array by converting keys, which are PHP property |
||
| 896 | * names, to MongoDB field names. |
||
| 897 | * |
||
| 898 | * @param array $fields |
||
| 899 | * @return array |
||
| 900 | */ |
||
| 901 | 14 | public function prepareProjection(array $fields) |
|
| 911 | |||
| 912 | /** |
||
| 913 | * @param string $sort |
||
| 914 | * @return int |
||
| 915 | */ |
||
| 916 | 25 | private function getSortDirection($sort) |
|
| 928 | |||
| 929 | /** |
||
| 930 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 931 | * names and changing direction strings to int. |
||
| 932 | * |
||
| 933 | * @param array $fields |
||
| 934 | * @return array |
||
| 935 | */ |
||
| 936 | 142 | public function prepareSort(array $fields) |
|
| 946 | |||
| 947 | /** |
||
| 948 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 949 | * |
||
| 950 | * @param string $fieldName |
||
| 951 | * @return string |
||
| 952 | */ |
||
| 953 | 433 | public function prepareFieldName($fieldName) |
|
| 959 | |||
| 960 | /** |
||
| 961 | * Adds discriminator criteria to an already-prepared query. |
||
| 962 | * |
||
| 963 | * This method should be used once for query criteria and not be used for |
||
| 964 | * nested expressions. It should be called before |
||
| 965 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 966 | * |
||
| 967 | * @param array $preparedQuery |
||
| 968 | * @return array |
||
| 969 | */ |
||
| 970 | 493 | public function addDiscriminatorToPreparedQuery(array $preparedQuery) |
|
| 986 | |||
| 987 | /** |
||
| 988 | * Adds filter criteria to an already-prepared query. |
||
| 989 | * |
||
| 990 | * This method should be used once for query criteria and not be used for |
||
| 991 | * nested expressions. It should be called after |
||
| 992 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 993 | * |
||
| 994 | * @param array $preparedQuery |
||
| 995 | * @return array |
||
| 996 | */ |
||
| 997 | 494 | public function addFilterToPreparedQuery(array $preparedQuery) |
|
| 1012 | |||
| 1013 | /** |
||
| 1014 | * Prepares the query criteria or new document object. |
||
| 1015 | * |
||
| 1016 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 1017 | * |
||
| 1018 | * @param array $query |
||
| 1019 | * @param bool $isNewObj |
||
| 1020 | * @return array |
||
| 1021 | */ |
||
| 1022 | 526 | public function prepareQueryOrNewObj(array $query, $isNewObj = false) |
|
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Prepares a query value and converts the PHP value to the database value |
||
| 1053 | * if it is an identifier. |
||
| 1054 | * |
||
| 1055 | * It also handles converting $fieldName to the database name if they are different. |
||
| 1056 | * |
||
| 1057 | * @param string $fieldName |
||
| 1058 | * @param mixed $value |
||
| 1059 | * @param ClassMetadata $class Defaults to $this->class |
||
| 1060 | * @param bool $prepareValue Whether or not to prepare the value |
||
| 1061 | * @param bool $inNewObj Whether or not newObj is being prepared |
||
| 1062 | * @return array An array of tuples containing prepared field names and values |
||
| 1063 | */ |
||
| 1064 | 877 | private function prepareQueryElement($fieldName, $value = null, $class = null, $prepareValue = true, $inNewObj = false) |
|
| 1065 | { |
||
| 1066 | 877 | $class = $class ?? $this->class; |
|
| 1067 | |||
| 1068 | // @todo Consider inlining calls to ClassMetadata methods |
||
| 1069 | |||
| 1070 | // Process all non-identifier fields by translating field names |
||
| 1071 | 877 | if ($class->hasField($fieldName) && ! $class->isIdentifier($fieldName)) { |
|
| 1072 | 247 | $mapping = $class->fieldMappings[$fieldName]; |
|
| 1073 | 247 | $fieldName = $mapping['name']; |
|
| 1074 | |||
| 1075 | 247 | if (! $prepareValue) { |
|
| 1076 | 52 | return [[$fieldName, $value]]; |
|
| 1077 | } |
||
| 1078 | |||
| 1079 | // Prepare mapped, embedded objects |
||
| 1080 | 205 | if (! empty($mapping['embedded']) && is_object($value) && |
|
| 1081 | 205 | ! $this->dm->getMetadataFactory()->isTransient(get_class($value))) { |
|
| 1082 | 3 | return [[$fieldName, $this->pb->prepareEmbeddedDocumentValue($mapping, $value)]]; |
|
| 1083 | } |
||
| 1084 | |||
| 1085 | 203 | if (! empty($mapping['reference']) && is_object($value) && ! ($value instanceof ObjectId)) { |
|
| 1086 | try { |
||
| 1087 | 14 | return $this->prepareReference($fieldName, $value, $mapping, $inNewObj); |
|
| 1088 | 1 | } catch (MappingException $e) { |
|
| 1089 | // do nothing in case passed object is not mapped document |
||
| 1090 | } |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | // No further preparation unless we're dealing with a simple reference |
||
| 1094 | // We can't have expressions in empty() with PHP < 5.5, so store it in a variable |
||
| 1095 | 190 | $arrayValue = (array) $value; |
|
| 1096 | 190 | if (empty($mapping['reference']) || $mapping['storeAs'] !== ClassMetadata::REFERENCE_STORE_AS_ID || empty($arrayValue)) { |
|
| 1097 | 126 | return [[$fieldName, $value]]; |
|
| 1098 | } |
||
| 1099 | |||
| 1100 | // Additional preparation for one or more simple reference values |
||
| 1101 | 91 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 1102 | |||
| 1103 | 91 | if (! is_array($value)) { |
|
| 1104 | 87 | return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]]; |
|
| 1105 | } |
||
| 1106 | |||
| 1107 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1108 | 6 | if (! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
|
| 1109 | 3 | return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]]; |
|
| 1110 | } |
||
| 1111 | |||
| 1112 | 6 | return [[$fieldName, $this->prepareQueryExpression($value, $targetClass)]]; |
|
| 1113 | } |
||
| 1114 | |||
| 1115 | // Process identifier fields |
||
| 1116 | 790 | if (($class->hasField($fieldName) && $class->isIdentifier($fieldName)) || $fieldName === '_id') { |
|
| 1117 | 335 | $fieldName = '_id'; |
|
| 1118 | |||
| 1119 | 335 | if (! $prepareValue) { |
|
| 1120 | 42 | return [[$fieldName, $value]]; |
|
| 1121 | } |
||
| 1122 | |||
| 1123 | 296 | if (! is_array($value)) { |
|
| 1124 | 270 | return [[$fieldName, $class->getDatabaseIdentifierValue($value)]]; |
|
| 1125 | } |
||
| 1126 | |||
| 1127 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1128 | 62 | if (! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
|
| 1129 | 6 | return [[$fieldName, $class->getDatabaseIdentifierValue($value)]]; |
|
| 1130 | } |
||
| 1131 | |||
| 1132 | 57 | return [[$fieldName, $this->prepareQueryExpression($value, $class)]]; |
|
| 1133 | } |
||
| 1134 | |||
| 1135 | // No processing for unmapped, non-identifier, non-dotted field names |
||
| 1136 | 553 | if (strpos($fieldName, '.') === false) { |
|
| 1137 | 414 | return [[$fieldName, $value]]; |
|
| 1138 | } |
||
| 1139 | |||
| 1140 | /* Process "fieldName.objectProperty" queries (on arrays or objects). |
||
| 1141 | * |
||
| 1142 | * We can limit parsing here, since at most three segments are |
||
| 1143 | * significant: "fieldName.objectProperty" with an optional index or key |
||
| 1144 | * for collections stored as either BSON arrays or objects. |
||
| 1145 | */ |
||
| 1146 | 152 | $e = explode('.', $fieldName, 4); |
|
| 1147 | |||
| 1148 | // No further processing for unmapped fields |
||
| 1149 | 152 | if (! isset($class->fieldMappings[$e[0]])) { |
|
| 1150 | 6 | return [[$fieldName, $value]]; |
|
| 1151 | } |
||
| 1152 | |||
| 1153 | 147 | $mapping = $class->fieldMappings[$e[0]]; |
|
| 1154 | 147 | $e[0] = $mapping['name']; |
|
| 1155 | |||
| 1156 | // Hash and raw fields will not be prepared beyond the field name |
||
| 1157 | 147 | if ($mapping['type'] === Type::HASH || $mapping['type'] === Type::RAW) { |
|
| 1158 | 1 | $fieldName = implode('.', $e); |
|
| 1159 | |||
| 1160 | 1 | return [[$fieldName, $value]]; |
|
| 1161 | } |
||
| 1162 | |||
| 1163 | 146 | if ($mapping['type'] === 'many' && CollectionHelper::isHash($mapping['strategy']) |
|
| 1164 | 146 | && isset($e[2])) { |
|
| 1165 | 1 | $objectProperty = $e[2]; |
|
| 1166 | 1 | $objectPropertyPrefix = $e[1] . '.'; |
|
| 1167 | 1 | $nextObjectProperty = implode('.', array_slice($e, 3)); |
|
| 1168 | 145 | } elseif ($e[1] !== '$') { |
|
| 1169 | 144 | $fieldName = $e[0] . '.' . $e[1]; |
|
| 1170 | 144 | $objectProperty = $e[1]; |
|
| 1171 | 144 | $objectPropertyPrefix = ''; |
|
| 1172 | 144 | $nextObjectProperty = implode('.', array_slice($e, 2)); |
|
| 1173 | 1 | } elseif (isset($e[2])) { |
|
| 1174 | 1 | $fieldName = $e[0] . '.' . $e[1] . '.' . $e[2]; |
|
| 1175 | 1 | $objectProperty = $e[2]; |
|
| 1176 | 1 | $objectPropertyPrefix = $e[1] . '.'; |
|
| 1177 | 1 | $nextObjectProperty = implode('.', array_slice($e, 3)); |
|
| 1178 | } else { |
||
| 1179 | 1 | $fieldName = $e[0] . '.' . $e[1]; |
|
| 1180 | |||
| 1181 | 1 | return [[$fieldName, $value]]; |
|
| 1182 | } |
||
| 1183 | |||
| 1184 | // No further processing for fields without a targetDocument mapping |
||
| 1185 | 146 | if (! isset($mapping['targetDocument'])) { |
|
| 1186 | 3 | if ($nextObjectProperty) { |
|
| 1187 | $fieldName .= '.' . $nextObjectProperty; |
||
| 1188 | } |
||
| 1189 | |||
| 1190 | 3 | return [[$fieldName, $value]]; |
|
| 1191 | } |
||
| 1192 | |||
| 1193 | 143 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 1194 | |||
| 1195 | // No further processing for unmapped targetDocument fields |
||
| 1196 | 143 | if (! $targetClass->hasField($objectProperty)) { |
|
| 1197 | 25 | if ($nextObjectProperty) { |
|
| 1198 | $fieldName .= '.' . $nextObjectProperty; |
||
| 1199 | } |
||
| 1200 | |||
| 1201 | 25 | return [[$fieldName, $value]]; |
|
| 1202 | } |
||
| 1203 | |||
| 1204 | 123 | $targetMapping = $targetClass->getFieldMapping($objectProperty); |
|
| 1205 | 123 | $objectPropertyIsId = $targetClass->isIdentifier($objectProperty); |
|
| 1206 | |||
| 1207 | // Prepare DBRef identifiers or the mapped field's property path |
||
| 1208 | 123 | $fieldName = ($objectPropertyIsId && ! empty($mapping['reference']) && $mapping['storeAs'] !== ClassMetadata::REFERENCE_STORE_AS_ID) |
|
| 1209 | 105 | ? ClassMetadata::getReferenceFieldName($mapping['storeAs'], $e[0]) |
|
| 1210 | 123 | : $e[0] . '.' . $objectPropertyPrefix . $targetMapping['name']; |
|
| 1211 | |||
| 1212 | // Process targetDocument identifier fields |
||
| 1213 | 123 | if ($objectPropertyIsId) { |
|
| 1214 | 106 | if (! $prepareValue) { |
|
| 1215 | 7 | return [[$fieldName, $value]]; |
|
| 1216 | } |
||
| 1217 | |||
| 1218 | 99 | if (! is_array($value)) { |
|
| 1219 | 85 | return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]]; |
|
| 1220 | } |
||
| 1221 | |||
| 1222 | // Objects without operators or with DBRef fields can be converted immediately |
||
| 1223 | 16 | if (! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) { |
|
| 1224 | 6 | return [[$fieldName, $targetClass->getDatabaseIdentifierValue($value)]]; |
|
| 1225 | } |
||
| 1226 | |||
| 1227 | 16 | return [[$fieldName, $this->prepareQueryExpression($value, $targetClass)]]; |
|
| 1228 | } |
||
| 1229 | |||
| 1230 | /* The property path may include a third field segment, excluding the |
||
| 1231 | * collection item pointer. If present, this next object property must |
||
| 1232 | * be processed recursively. |
||
| 1233 | */ |
||
| 1234 | 17 | if ($nextObjectProperty) { |
|
| 1235 | // Respect the targetDocument's class metadata when recursing |
||
| 1236 | 14 | $nextTargetClass = isset($targetMapping['targetDocument']) |
|
| 1237 | 8 | ? $this->dm->getClassMetadata($targetMapping['targetDocument']) |
|
| 1238 | 14 | : null; |
|
| 1239 | |||
| 1240 | 14 | $fieldNames = $this->prepareQueryElement($nextObjectProperty, $value, $nextTargetClass, $prepareValue); |
|
| 1241 | |||
| 1242 | return array_map(function ($preparedTuple) use ($fieldName) { |
||
| 1243 | 14 | list($key, $value) = $preparedTuple; |
|
| 1244 | |||
| 1245 | 14 | return [$fieldName . '.' . $key, $value]; |
|
| 1246 | 14 | }, $fieldNames); |
|
| 1247 | } |
||
| 1248 | |||
| 1249 | 5 | return [[$fieldName, $value]]; |
|
| 1250 | } |
||
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Prepares a query expression. |
||
| 1254 | * |
||
| 1255 | * @param array|object $expression |
||
| 1256 | * @param ClassMetadata $class |
||
| 1257 | * @return array |
||
| 1258 | */ |
||
| 1259 | 79 | private function prepareQueryExpression($expression, $class) |
|
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Checks whether the value has DBRef fields. |
||
| 1289 | * |
||
| 1290 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1291 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1292 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1293 | * $elemMatch criteria for matching a DBRef. |
||
| 1294 | * |
||
| 1295 | * @param mixed $value |
||
| 1296 | * @return bool |
||
| 1297 | */ |
||
| 1298 | 80 | private function hasDBRefFields($value) |
|
| 1316 | |||
| 1317 | /** |
||
| 1318 | * Checks whether the value has query operators. |
||
| 1319 | * |
||
| 1320 | * @param mixed $value |
||
| 1321 | * @return bool |
||
| 1322 | */ |
||
| 1323 | 84 | private function hasQueryOperators($value) |
|
| 1341 | |||
| 1342 | /** |
||
| 1343 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1344 | * |
||
| 1345 | * @return array |
||
| 1346 | */ |
||
| 1347 | 27 | private function getClassDiscriminatorValues(ClassMetadata $metadata) |
|
| 1366 | |||
| 1367 | 547 | private function handleCollections($document, $options) |
|
| 1390 | |||
| 1391 | /** |
||
| 1392 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
| 1393 | * Also, shard key field should be present in actual document data. |
||
| 1394 | * |
||
| 1395 | * @param object $document |
||
| 1396 | * @param string $shardKeyField |
||
| 1397 | * @param array $actualDocumentData |
||
| 1398 | * |
||
| 1399 | * @throws MongoDBException |
||
| 1400 | */ |
||
| 1401 | 4 | private function guardMissingShardKey($document, $shardKeyField, $actualDocumentData) |
|
| 1417 | |||
| 1418 | /** |
||
| 1419 | * Get shard key aware query for single document. |
||
| 1420 | * |
||
| 1421 | * @param object $document |
||
| 1422 | * |
||
| 1423 | * @return array |
||
| 1424 | */ |
||
| 1425 | 264 | private function getQueryForDocument($document) |
|
| 1435 | |||
| 1436 | /** |
||
| 1437 | * @param array $options |
||
| 1438 | * |
||
| 1439 | * @return array |
||
| 1440 | */ |
||
| 1441 | 548 | private function getWriteOptions(array $options = []) |
|
| 1451 | |||
| 1452 | /** |
||
| 1453 | * @param string $fieldName |
||
| 1454 | * @param mixed $value |
||
| 1455 | * @param array $mapping |
||
| 1456 | * @param bool $inNewObj |
||
| 1457 | * @return array |
||
| 1458 | */ |
||
| 1459 | 15 | private function prepareReference($fieldName, $value, array $mapping, $inNewObj) |
|
| 1499 | } |
||
| 1500 |
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.