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 |
||
| 60 | class DocumentPersister |
||
| 61 | { |
||
| 62 | /** @var PersistenceBuilder */ |
||
| 63 | private $pb; |
||
| 64 | |||
| 65 | /** @var DocumentManager */ |
||
| 66 | private $dm; |
||
| 67 | |||
| 68 | /** @var UnitOfWork */ |
||
| 69 | private $uow; |
||
| 70 | |||
| 71 | /** @var ClassMetadata */ |
||
| 72 | private $class; |
||
| 73 | |||
| 74 | /** @var Collection */ |
||
| 75 | private $collection; |
||
| 76 | |||
| 77 | /** @var Bucket|null */ |
||
| 78 | private $bucket; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Array of queued inserts for the persister to insert. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $queuedInserts = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Array of queued inserts for the persister to insert. |
||
| 89 | * |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | private $queuedUpserts = []; |
||
| 93 | |||
| 94 | /** @var CriteriaMerger */ |
||
| 95 | private $cm; |
||
| 96 | |||
| 97 | /** @var CollectionPersister */ |
||
| 98 | private $cp; |
||
| 99 | |||
| 100 | /** @var HydratorFactory */ |
||
| 101 | private $hydratorFactory; |
||
| 102 | |||
| 103 | 1121 | public function __construct( |
|
| 104 | PersistenceBuilder $pb, |
||
| 105 | DocumentManager $dm, |
||
| 106 | UnitOfWork $uow, |
||
| 107 | HydratorFactory $hydratorFactory, |
||
| 108 | ClassMetadata $class, |
||
| 109 | ?CriteriaMerger $cm = null |
||
| 110 | ) { |
||
| 111 | 1121 | $this->pb = $pb; |
|
| 112 | 1121 | $this->dm = $dm; |
|
| 113 | 1121 | $this->cm = $cm ?: new CriteriaMerger(); |
|
| 114 | 1121 | $this->uow = $uow; |
|
| 115 | 1121 | $this->hydratorFactory = $hydratorFactory; |
|
| 116 | 1121 | $this->class = $class; |
|
| 117 | 1121 | $this->collection = $dm->getDocumentCollection($class->name); |
|
| 118 | 1121 | $this->cp = $this->uow->getCollectionPersister(); |
|
| 119 | |||
| 120 | 1121 | if (! $class->isFile) { |
|
| 121 | 1113 | return; |
|
| 122 | } |
||
| 123 | |||
| 124 | 10 | $this->bucket = $dm->getDocumentBucket($class->name); |
|
| 125 | 10 | } |
|
| 126 | |||
| 127 | public function getInserts(): array |
||
| 128 | { |
||
| 129 | return $this->queuedInserts; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function isQueuedForInsert(object $document): bool |
||
| 133 | { |
||
| 134 | return isset($this->queuedInserts[spl_object_hash($document)]); |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Adds a document to the queued insertions. |
||
| 139 | * The document remains queued until {@link executeInserts} is invoked. |
||
| 140 | */ |
||
| 141 | 511 | public function addInsert(object $document): void |
|
| 142 | { |
||
| 143 | 511 | $this->queuedInserts[spl_object_hash($document)] = $document; |
|
| 144 | 511 | } |
|
| 145 | |||
| 146 | public function getUpserts(): array |
||
| 147 | { |
||
| 148 | return $this->queuedUpserts; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function isQueuedForUpsert(object $document): bool |
||
| 152 | { |
||
| 153 | return isset($this->queuedUpserts[spl_object_hash($document)]); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Adds a document to the queued upserts. |
||
| 158 | * The document remains queued until {@link executeUpserts} is invoked. |
||
| 159 | */ |
||
| 160 | 85 | public function addUpsert(object $document): void |
|
| 161 | { |
||
| 162 | 85 | $this->queuedUpserts[spl_object_hash($document)] = $document; |
|
| 163 | 85 | } |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Gets the ClassMetadata instance of the document class this persister is used for. |
||
| 167 | */ |
||
| 168 | public function getClassMetadata(): ClassMetadata |
||
| 169 | { |
||
| 170 | return $this->class; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Executes all queued document insertions. |
||
| 175 | * |
||
| 176 | * Queued documents without an ID will inserted in a batch and queued |
||
| 177 | * documents with an ID will be upserted individually. |
||
| 178 | * |
||
| 179 | * If no inserts are queued, invoking this method is a NOOP. |
||
| 180 | * |
||
| 181 | * @throws DriverException |
||
| 182 | */ |
||
| 183 | 511 | public function executeInserts(array $options = []): void |
|
| 184 | { |
||
| 185 | 511 | if (! $this->queuedInserts) { |
|
|
|
|||
| 186 | return; |
||
| 187 | } |
||
| 188 | |||
| 189 | 511 | $inserts = []; |
|
| 190 | 511 | $options = $this->getWriteOptions($options); |
|
| 191 | 511 | foreach ($this->queuedInserts as $oid => $document) { |
|
| 192 | 511 | $data = $this->pb->prepareInsertData($document); |
|
| 193 | |||
| 194 | // Set the initial version for each insert |
||
| 195 | 510 | if ($this->class->isVersioned) { |
|
| 196 | 38 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 197 | 38 | $nextVersion = null; |
|
| 198 | 38 | if ($versionMapping['type'] === 'int') { |
|
| 199 | 36 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 200 | 36 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 201 | 2 | } elseif ($versionMapping['type'] === 'date') { |
|
| 202 | 2 | $nextVersionDateTime = new \DateTime(); |
|
| 203 | 2 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 204 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 205 | } |
||
| 206 | 38 | $data[$versionMapping['name']] = $nextVersion; |
|
| 207 | } |
||
| 208 | |||
| 209 | 510 | $inserts[] = $data; |
|
| 210 | } |
||
| 211 | |||
| 212 | 510 | if ($inserts) { |
|
| 213 | try { |
||
| 214 | 510 | $this->collection->insertMany($inserts, $options); |
|
| 215 | 6 | } catch (DriverException $e) { |
|
| 216 | 6 | $this->queuedInserts = []; |
|
| 217 | 6 | throw $e; |
|
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /* All collections except for ones using addToSet have already been |
||
| 222 | * saved. We have left these to be handled separately to avoid checking |
||
| 223 | * collection for uniqueness on PHP side. |
||
| 224 | */ |
||
| 225 | 510 | foreach ($this->queuedInserts as $document) { |
|
| 226 | 510 | $this->handleCollections($document, $options); |
|
| 227 | } |
||
| 228 | |||
| 229 | 510 | $this->queuedInserts = []; |
|
| 230 | 510 | } |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Executes all queued document upserts. |
||
| 234 | * |
||
| 235 | * Queued documents with an ID are upserted individually. |
||
| 236 | * |
||
| 237 | * If no upserts are queued, invoking this method is a NOOP. |
||
| 238 | */ |
||
| 239 | 85 | public function executeUpserts(array $options = []): void |
|
| 240 | { |
||
| 241 | 85 | if (! $this->queuedUpserts) { |
|
| 242 | return; |
||
| 243 | } |
||
| 244 | |||
| 245 | 85 | $options = $this->getWriteOptions($options); |
|
| 246 | 85 | foreach ($this->queuedUpserts as $oid => $document) { |
|
| 247 | try { |
||
| 248 | 85 | $this->executeUpsert($document, $options); |
|
| 249 | 85 | $this->handleCollections($document, $options); |
|
| 250 | 85 | unset($this->queuedUpserts[$oid]); |
|
| 251 | } catch (WriteException $e) { |
||
| 252 | unset($this->queuedUpserts[$oid]); |
||
| 253 | 85 | throw $e; |
|
| 254 | } |
||
| 255 | } |
||
| 256 | 85 | } |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Executes a single upsert in {@link executeUpserts} |
||
| 260 | */ |
||
| 261 | 85 | private function executeUpsert(object $document, array $options): void |
|
| 262 | { |
||
| 263 | 85 | $options['upsert'] = true; |
|
| 264 | 85 | $criteria = $this->getQueryForDocument($document); |
|
| 265 | |||
| 266 | 85 | $data = $this->pb->prepareUpsertData($document); |
|
| 267 | |||
| 268 | // Set the initial version for each upsert |
||
| 269 | 85 | if ($this->class->isVersioned) { |
|
| 270 | 3 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 271 | 3 | $nextVersion = null; |
|
| 272 | 3 | if ($versionMapping['type'] === 'int') { |
|
| 273 | 2 | $nextVersion = max(1, (int) $this->class->reflFields[$this->class->versionField]->getValue($document)); |
|
| 274 | 2 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 275 | 1 | } elseif ($versionMapping['type'] === 'date') { |
|
| 276 | 1 | $nextVersionDateTime = new \DateTime(); |
|
| 277 | 1 | $nextVersion = Type::convertPHPToDatabaseValue($nextVersionDateTime); |
|
| 278 | 1 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersionDateTime); |
|
| 279 | } |
||
| 280 | 3 | $data['$set'][$versionMapping['name']] = $nextVersion; |
|
| 281 | } |
||
| 282 | |||
| 283 | 85 | foreach (array_keys($criteria) as $field) { |
|
| 284 | 85 | unset($data['$set'][$field]); |
|
| 285 | 85 | unset($data['$inc'][$field]); |
|
| 286 | 85 | unset($data['$setOnInsert'][$field]); |
|
| 287 | } |
||
| 288 | |||
| 289 | // Do not send empty update operators |
||
| 290 | 85 | foreach (['$set', '$inc', '$setOnInsert'] as $operator) { |
|
| 291 | 85 | if (! empty($data[$operator])) { |
|
| 292 | 70 | continue; |
|
| 293 | } |
||
| 294 | |||
| 295 | 85 | unset($data[$operator]); |
|
| 296 | } |
||
| 297 | |||
| 298 | /* If there are no modifiers remaining, we're upserting a document with |
||
| 299 | * an identifier as its only field. Since a document with the identifier |
||
| 300 | * may already exist, the desired behavior is "insert if not exists" and |
||
| 301 | * NOOP otherwise. MongoDB 2.6+ does not allow empty modifiers, so $set |
||
| 302 | * the identifier to the same value in our criteria. |
||
| 303 | * |
||
| 304 | * This will fail for versions before MongoDB 2.6, which require an |
||
| 305 | * empty $set modifier. The best we can do (without attempting to check |
||
| 306 | * server versions in advance) is attempt the 2.6+ behavior and retry |
||
| 307 | * after the relevant exception. |
||
| 308 | * |
||
| 309 | * See: https://jira.mongodb.org/browse/SERVER-12266 |
||
| 310 | */ |
||
| 311 | 85 | if (empty($data)) { |
|
| 312 | 16 | $retry = true; |
|
| 313 | 16 | $data = ['$set' => ['_id' => $criteria['_id']]]; |
|
| 314 | } |
||
| 315 | |||
| 316 | try { |
||
| 317 | 85 | $this->collection->updateOne($criteria, $data, $options); |
|
| 318 | 85 | return; |
|
| 319 | } catch (WriteException $e) { |
||
| 320 | if (empty($retry) || strpos($e->getMessage(), 'Mod on _id not allowed') === false) { |
||
| 321 | throw $e; |
||
| 322 | } |
||
| 323 | } |
||
| 324 | |||
| 325 | $this->collection->updateOne($criteria, ['$set' => new \stdClass()], $options); |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Updates the already persisted document if it has any new changesets. |
||
| 330 | * |
||
| 331 | * @throws LockException |
||
| 332 | */ |
||
| 333 | 219 | public function update(object $document, array $options = []): void |
|
| 334 | { |
||
| 335 | 219 | $update = $this->pb->prepareUpdateData($document); |
|
| 336 | |||
| 337 | 219 | $query = $this->getQueryForDocument($document); |
|
| 338 | |||
| 339 | 217 | foreach (array_keys($query) as $field) { |
|
| 340 | 217 | unset($update['$set'][$field]); |
|
| 341 | } |
||
| 342 | |||
| 343 | 217 | if (empty($update['$set'])) { |
|
| 344 | 94 | unset($update['$set']); |
|
| 345 | } |
||
| 346 | |||
| 347 | // Include versioning logic to set the new version value in the database |
||
| 348 | // and to ensure the version has not changed since this document object instance |
||
| 349 | // was fetched from the database |
||
| 350 | 217 | $nextVersion = null; |
|
| 351 | 217 | if ($this->class->isVersioned) { |
|
| 352 | 31 | $versionMapping = $this->class->fieldMappings[$this->class->versionField]; |
|
| 353 | 31 | $currentVersion = $this->class->reflFields[$this->class->versionField]->getValue($document); |
|
| 354 | 31 | if ($versionMapping['type'] === 'int') { |
|
| 355 | 28 | $nextVersion = $currentVersion + 1; |
|
| 356 | 28 | $update['$inc'][$versionMapping['name']] = 1; |
|
| 357 | 28 | $query[$versionMapping['name']] = $currentVersion; |
|
| 358 | 3 | } elseif ($versionMapping['type'] === 'date') { |
|
| 359 | 3 | $nextVersion = new \DateTime(); |
|
| 360 | 3 | $update['$set'][$versionMapping['name']] = Type::convertPHPToDatabaseValue($nextVersion); |
|
| 361 | 3 | $query[$versionMapping['name']] = Type::convertPHPToDatabaseValue($currentVersion); |
|
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | 217 | if (! empty($update)) { |
|
| 366 | // Include locking logic so that if the document object in memory is currently |
||
| 367 | // locked then it will remove it, otherwise it ensures the document is not locked. |
||
| 368 | 149 | if ($this->class->isLockable) { |
|
| 369 | 11 | $isLocked = $this->class->reflFields[$this->class->lockField]->getValue($document); |
|
| 370 | 11 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 371 | 11 | if ($isLocked) { |
|
| 372 | 2 | $update['$unset'] = [$lockMapping['name'] => true]; |
|
| 373 | } else { |
||
| 374 | 9 | $query[$lockMapping['name']] = ['$exists' => false]; |
|
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | 149 | $options = $this->getWriteOptions($options); |
|
| 379 | |||
| 380 | 149 | $result = $this->collection->updateOne($query, $update, $options); |
|
| 381 | |||
| 382 | 149 | if (($this->class->isVersioned || $this->class->isLockable) && $result->getModifiedCount() !== 1) { |
|
| 383 | 5 | throw LockException::lockFailed($document); |
|
| 384 | 145 | } elseif ($this->class->isVersioned) { |
|
| 385 | 27 | $this->class->reflFields[$this->class->versionField]->setValue($document, $nextVersion); |
|
| 386 | } |
||
| 387 | } |
||
| 388 | |||
| 389 | 213 | $this->handleCollections($document, $options); |
|
| 390 | 213 | } |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Removes document from mongo |
||
| 394 | * |
||
| 395 | * @throws LockException |
||
| 396 | */ |
||
| 397 | 36 | public function delete(object $document, array $options = []): void |
|
| 398 | { |
||
| 399 | 36 | if ($this->bucket instanceof Bucket) { |
|
| 400 | 1 | $documentIdentifier = $this->uow->getDocumentIdentifier($document); |
|
| 401 | 1 | $databaseIdentifier = $this->class->getDatabaseIdentifierValue($documentIdentifier); |
|
| 402 | |||
| 403 | 1 | $this->bucket->delete($databaseIdentifier); |
|
| 404 | |||
| 405 | 1 | return; |
|
| 406 | } |
||
| 407 | |||
| 408 | 35 | $query = $this->getQueryForDocument($document); |
|
| 409 | |||
| 410 | 35 | if ($this->class->isLockable) { |
|
| 411 | 2 | $query[$this->class->lockField] = ['$exists' => false]; |
|
| 412 | } |
||
| 413 | |||
| 414 | 35 | $options = $this->getWriteOptions($options); |
|
| 415 | |||
| 416 | 35 | $result = $this->collection->deleteOne($query, $options); |
|
| 417 | |||
| 418 | 35 | if (($this->class->isVersioned || $this->class->isLockable) && ! $result->getDeletedCount()) { |
|
| 419 | 2 | throw LockException::lockFailed($document); |
|
| 420 | } |
||
| 421 | 33 | } |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Refreshes a managed document. |
||
| 425 | */ |
||
| 426 | 23 | public function refresh(object $document): void |
|
| 427 | { |
||
| 428 | 23 | $query = $this->getQueryForDocument($document); |
|
| 429 | 23 | $data = $this->collection->findOne($query); |
|
| 430 | 23 | $data = $this->hydratorFactory->hydrate($document, $data); |
|
| 431 | 23 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 432 | 23 | } |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Finds a document by a set of criteria. |
||
| 436 | * |
||
| 437 | * If a scalar or MongoDB\BSON\ObjectId is provided for $criteria, it will |
||
| 438 | * be used to match an _id value. |
||
| 439 | * |
||
| 440 | * @param mixed $criteria Query criteria |
||
| 441 | * @throws LockException |
||
| 442 | * @todo Check identity map? loadById method? Try to guess whether $criteria is the id? |
||
| 443 | */ |
||
| 444 | 372 | public function load($criteria, ?object $document = null, array $hints = [], int $lockMode = 0, ?array $sort = null): ?object |
|
| 445 | { |
||
| 446 | // TODO: remove this |
||
| 447 | 372 | if ($criteria === null || is_scalar($criteria) || $criteria instanceof ObjectId) { |
|
| 448 | $criteria = ['_id' => $criteria]; |
||
| 449 | } |
||
| 450 | |||
| 451 | 372 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 452 | 372 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 453 | 372 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 454 | |||
| 455 | 372 | $options = []; |
|
| 456 | 372 | if ($sort !== null) { |
|
| 457 | 95 | $options['sort'] = $this->prepareSort($sort); |
|
| 458 | } |
||
| 459 | 372 | $result = $this->collection->findOne($criteria, $options); |
|
| 460 | |||
| 461 | 372 | if ($this->class->isLockable) { |
|
| 462 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 463 | 1 | if (isset($result[$lockMapping['name']]) && $result[$lockMapping['name']] === LockMode::PESSIMISTIC_WRITE) { |
|
| 464 | 1 | throw LockException::lockFailed($document); |
|
| 465 | } |
||
| 466 | } |
||
| 467 | |||
| 468 | 371 | return $this->createDocument($result, $document, $hints); |
|
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Finds documents by a set of criteria. |
||
| 473 | */ |
||
| 474 | 22 | public function loadAll(array $criteria = [], ?array $sort = null, ?int $limit = null, ?int $skip = null): Iterator |
|
| 475 | { |
||
| 476 | 22 | $criteria = $this->prepareQueryOrNewObj($criteria); |
|
| 477 | 22 | $criteria = $this->addDiscriminatorToPreparedQuery($criteria); |
|
| 478 | 22 | $criteria = $this->addFilterToPreparedQuery($criteria); |
|
| 479 | |||
| 480 | 22 | $options = []; |
|
| 481 | 22 | if ($sort !== null) { |
|
| 482 | 11 | $options['sort'] = $this->prepareSort($sort); |
|
| 483 | } |
||
| 484 | |||
| 485 | 22 | if ($limit !== null) { |
|
| 486 | 10 | $options['limit'] = $limit; |
|
| 487 | } |
||
| 488 | |||
| 489 | 22 | if ($skip !== null) { |
|
| 490 | 1 | $options['skip'] = $skip; |
|
| 491 | } |
||
| 492 | |||
| 493 | 22 | $baseCursor = $this->collection->find($criteria, $options); |
|
| 494 | 22 | $cursor = $this->wrapCursor($baseCursor); |
|
| 495 | |||
| 496 | 22 | return $cursor; |
|
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * @throws MongoDBException |
||
| 501 | */ |
||
| 502 | 299 | private function getShardKeyQuery(object $document): array |
|
| 503 | { |
||
| 504 | 299 | if (! $this->class->isSharded()) { |
|
| 505 | 289 | return []; |
|
| 506 | } |
||
| 507 | |||
| 508 | 10 | $shardKey = $this->class->getShardKey(); |
|
| 509 | 10 | $keys = array_keys($shardKey['keys']); |
|
| 510 | 10 | $data = $this->uow->getDocumentActualData($document); |
|
| 511 | |||
| 512 | 10 | $shardKeyQueryPart = []; |
|
| 513 | 10 | foreach ($keys as $key) { |
|
| 514 | 10 | $mapping = $this->class->getFieldMappingByDbFieldName($key); |
|
| 515 | 10 | $this->guardMissingShardKey($document, $key, $data); |
|
| 516 | |||
| 517 | 8 | if (isset($mapping['association']) && $mapping['association'] === ClassMetadata::REFERENCE_ONE) { |
|
| 518 | 1 | $reference = $this->prepareReference( |
|
| 519 | 1 | $key, |
|
| 520 | 1 | $data[$mapping['fieldName']], |
|
| 521 | 1 | $mapping, |
|
| 522 | 1 | false |
|
| 523 | ); |
||
| 524 | 1 | foreach ($reference as $keyValue) { |
|
| 525 | 1 | $shardKeyQueryPart[$keyValue[0]] = $keyValue[1]; |
|
| 526 | } |
||
| 527 | } else { |
||
| 528 | 7 | $value = Type::getType($mapping['type'])->convertToDatabaseValue($data[$mapping['fieldName']]); |
|
| 529 | 8 | $shardKeyQueryPart[$key] = $value; |
|
| 530 | } |
||
| 531 | } |
||
| 532 | |||
| 533 | 8 | return $shardKeyQueryPart; |
|
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Wraps the supplied base cursor in the corresponding ODM class. |
||
| 538 | */ |
||
| 539 | 22 | private function wrapCursor(Cursor $baseCursor): Iterator |
|
| 540 | { |
||
| 541 | 22 | return new CachingIterator(new HydratingIterator($baseCursor, $this->dm->getUnitOfWork(), $this->class)); |
|
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Checks whether the given managed document exists in the database. |
||
| 546 | */ |
||
| 547 | 3 | public function exists(object $document): bool |
|
| 548 | { |
||
| 549 | 3 | $id = $this->class->getIdentifierObject($document); |
|
| 550 | 3 | return (bool) $this->collection->findOne(['_id' => $id], ['_id']); |
|
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Locks document by storing the lock mode on the mapped lock field. |
||
| 555 | */ |
||
| 556 | 5 | public function lock(object $document, int $lockMode): void |
|
| 557 | { |
||
| 558 | 5 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 559 | 5 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 560 | 5 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 561 | 5 | $this->collection->updateOne($criteria, ['$set' => [$lockMapping['name'] => $lockMode]]); |
|
| 562 | 5 | $this->class->reflFields[$this->class->lockField]->setValue($document, $lockMode); |
|
| 563 | 5 | } |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Releases any lock that exists on this document. |
||
| 567 | * |
||
| 568 | */ |
||
| 569 | 1 | public function unlock(object $document): void |
|
| 570 | { |
||
| 571 | 1 | $id = $this->uow->getDocumentIdentifier($document); |
|
| 572 | 1 | $criteria = ['_id' => $this->class->getDatabaseIdentifierValue($id)]; |
|
| 573 | 1 | $lockMapping = $this->class->fieldMappings[$this->class->lockField]; |
|
| 574 | 1 | $this->collection->updateOne($criteria, ['$unset' => [$lockMapping['name'] => true]]); |
|
| 575 | 1 | $this->class->reflFields[$this->class->lockField]->setValue($document, null); |
|
| 576 | 1 | } |
|
| 577 | |||
| 578 | /** |
||
| 579 | * Creates or fills a single document object from an query result. |
||
| 580 | * |
||
| 581 | * @param object $result The query result. |
||
| 582 | * @param object $document The document object to fill, if any. |
||
| 583 | * @param array $hints Hints for document creation. |
||
| 584 | * @return object The filled and managed document object or NULL, if the query result is empty. |
||
| 585 | */ |
||
| 586 | 371 | private function createDocument($result, ?object $document = null, array $hints = []): ?object |
|
| 587 | { |
||
| 588 | 371 | if ($result === null) { |
|
| 589 | 116 | return null; |
|
| 590 | } |
||
| 591 | |||
| 592 | 327 | if ($document !== null) { |
|
| 593 | 28 | $hints[Query::HINT_REFRESH] = true; |
|
| 594 | 28 | $id = $this->class->getPHPIdentifierValue($result['_id']); |
|
| 595 | 28 | $this->uow->registerManaged($document, $id, $result); |
|
| 596 | } |
||
| 597 | |||
| 598 | 327 | return $this->uow->getOrCreateDocument($this->class->name, $result, $hints, $document); |
|
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Loads a PersistentCollection data. Used in the initialize() method. |
||
| 603 | */ |
||
| 604 | 184 | public function loadCollection(PersistentCollectionInterface $collection): void |
|
| 605 | { |
||
| 606 | 184 | $mapping = $collection->getMapping(); |
|
| 607 | 184 | switch ($mapping['association']) { |
|
| 608 | case ClassMetadata::EMBED_MANY: |
||
| 609 | 129 | $this->loadEmbedManyCollection($collection); |
|
| 610 | 129 | break; |
|
| 611 | |||
| 612 | case ClassMetadata::REFERENCE_MANY: |
||
| 613 | 78 | if (isset($mapping['repositoryMethod']) && $mapping['repositoryMethod']) { |
|
| 614 | 5 | $this->loadReferenceManyWithRepositoryMethod($collection); |
|
| 615 | } else { |
||
| 616 | 74 | if ($mapping['isOwningSide']) { |
|
| 617 | 62 | $this->loadReferenceManyCollectionOwningSide($collection); |
|
| 618 | } else { |
||
| 619 | 17 | $this->loadReferenceManyCollectionInverseSide($collection); |
|
| 620 | } |
||
| 621 | } |
||
| 622 | 78 | break; |
|
| 623 | } |
||
| 624 | 184 | } |
|
| 625 | |||
| 626 | 129 | private function loadEmbedManyCollection(PersistentCollectionInterface $collection): void |
|
| 627 | { |
||
| 628 | 129 | $embeddedDocuments = $collection->getMongoData(); |
|
| 629 | 129 | $mapping = $collection->getMapping(); |
|
| 630 | 129 | $owner = $collection->getOwner(); |
|
| 631 | 129 | if (! $embeddedDocuments) { |
|
| 632 | 75 | return; |
|
| 633 | } |
||
| 634 | |||
| 635 | 100 | foreach ($embeddedDocuments as $key => $embeddedDocument) { |
|
| 636 | 100 | $className = $this->uow->getClassNameForAssociation($mapping, $embeddedDocument); |
|
| 637 | 100 | $embeddedMetadata = $this->dm->getClassMetadata($className); |
|
| 638 | 100 | $embeddedDocumentObject = $embeddedMetadata->newInstance(); |
|
| 639 | |||
| 640 | 100 | $this->uow->setParentAssociation($embeddedDocumentObject, $mapping, $owner, $mapping['name'] . '.' . $key); |
|
| 641 | |||
| 642 | 100 | $data = $this->hydratorFactory->hydrate($embeddedDocumentObject, $embeddedDocument, $collection->getHints()); |
|
| 643 | 100 | $id = $data[$embeddedMetadata->identifier] ?? null; |
|
| 644 | |||
| 645 | 100 | if (empty($collection->getHints()[Query::HINT_READ_ONLY])) { |
|
| 646 | 99 | $this->uow->registerManaged($embeddedDocumentObject, $id, $data); |
|
| 647 | } |
||
| 648 | 100 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 649 | 25 | $collection->set($key, $embeddedDocumentObject); |
|
| 650 | } else { |
||
| 651 | 100 | $collection->add($embeddedDocumentObject); |
|
| 652 | } |
||
| 653 | } |
||
| 654 | 100 | } |
|
| 655 | |||
| 656 | 62 | private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection): void |
|
| 657 | { |
||
| 658 | 62 | $hints = $collection->getHints(); |
|
| 659 | 62 | $mapping = $collection->getMapping(); |
|
| 660 | 62 | $groupedIds = []; |
|
| 661 | |||
| 662 | 62 | $sorted = isset($mapping['sort']) && $mapping['sort']; |
|
| 663 | |||
| 664 | 62 | foreach ($collection->getMongoData() as $key => $reference) { |
|
| 665 | 56 | $className = $this->uow->getClassNameForAssociation($mapping, $reference); |
|
| 666 | 56 | $identifier = ClassMetadata::getReferenceId($reference, $mapping['storeAs']); |
|
| 667 | 56 | $id = $this->dm->getClassMetadata($className)->getPHPIdentifierValue($identifier); |
|
| 668 | |||
| 669 | // create a reference to the class and id |
||
| 670 | 56 | $reference = $this->dm->getReference($className, $id); |
|
| 671 | |||
| 672 | // no custom sort so add the references right now in the order they are embedded |
||
| 673 | 56 | if (! $sorted) { |
|
| 674 | 55 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 675 | 2 | $collection->set($key, $reference); |
|
| 676 | } else { |
||
| 677 | 53 | $collection->add($reference); |
|
| 678 | } |
||
| 679 | } |
||
| 680 | |||
| 681 | // only query for the referenced object if it is not already initialized or the collection is sorted |
||
| 682 | 56 | if (! (($reference instanceof Proxy && ! $reference->__isInitialized__)) && ! $sorted) { |
|
| 683 | 22 | continue; |
|
| 684 | } |
||
| 685 | |||
| 686 | 41 | $groupedIds[$className][] = $identifier; |
|
| 687 | } |
||
| 688 | 62 | foreach ($groupedIds as $className => $ids) { |
|
| 689 | 41 | $class = $this->dm->getClassMetadata($className); |
|
| 690 | 41 | $mongoCollection = $this->dm->getDocumentCollection($className); |
|
| 691 | 41 | $criteria = $this->cm->merge( |
|
| 692 | 41 | ['_id' => ['$in' => array_values($ids)]], |
|
| 693 | 41 | $this->dm->getFilterCollection()->getFilterCriteria($class), |
|
| 694 | 41 | $mapping['criteria'] ?? [] |
|
| 695 | ); |
||
| 696 | 41 | $criteria = $this->uow->getDocumentPersister($className)->prepareQueryOrNewObj($criteria); |
|
| 697 | |||
| 698 | 41 | $options = []; |
|
| 699 | 41 | if (isset($mapping['sort'])) { |
|
| 700 | 41 | $options['sort'] = $this->prepareSort($mapping['sort']); |
|
| 701 | } |
||
| 702 | 41 | if (isset($mapping['limit'])) { |
|
| 703 | $options['limit'] = $mapping['limit']; |
||
| 704 | } |
||
| 705 | 41 | if (isset($mapping['skip'])) { |
|
| 706 | $options['skip'] = $mapping['skip']; |
||
| 707 | } |
||
| 708 | 41 | if (! empty($hints[Query::HINT_READ_PREFERENCE])) { |
|
| 709 | $options['readPreference'] = $hints[Query::HINT_READ_PREFERENCE]; |
||
| 710 | } |
||
| 711 | |||
| 712 | 41 | $cursor = $mongoCollection->find($criteria, $options); |
|
| 713 | 41 | $documents = $cursor->toArray(); |
|
| 714 | 41 | foreach ($documents as $documentData) { |
|
| 715 | 40 | $document = $this->uow->getById($documentData['_id'], $class); |
|
| 716 | 40 | if ($document instanceof Proxy && ! $document->__isInitialized()) { |
|
| 717 | 40 | $data = $this->hydratorFactory->hydrate($document, $documentData); |
|
| 718 | 40 | $this->uow->setOriginalDocumentData($document, $data); |
|
| 719 | 40 | $document->__isInitialized__ = true; |
|
| 720 | } |
||
| 721 | 40 | if (! $sorted) { |
|
| 722 | 39 | continue; |
|
| 723 | } |
||
| 724 | |||
| 725 | 41 | $collection->add($document); |
|
| 726 | } |
||
| 727 | } |
||
| 728 | 62 | } |
|
| 729 | |||
| 730 | 17 | private function loadReferenceManyCollectionInverseSide(PersistentCollectionInterface $collection): void |
|
| 731 | { |
||
| 732 | 17 | $query = $this->createReferenceManyInverseSideQuery($collection); |
|
| 733 | 17 | $documents = $query->execute()->toArray(); |
|
| 734 | 17 | foreach ($documents as $key => $document) { |
|
| 735 | 16 | $collection->add($document); |
|
| 736 | } |
||
| 737 | 17 | } |
|
| 738 | |||
| 739 | 17 | public function createReferenceManyInverseSideQuery(PersistentCollectionInterface $collection): Query |
|
| 740 | { |
||
| 741 | 17 | $hints = $collection->getHints(); |
|
| 742 | 17 | $mapping = $collection->getMapping(); |
|
| 743 | 17 | $owner = $collection->getOwner(); |
|
| 744 | 17 | $ownerClass = $this->dm->getClassMetadata(get_class($owner)); |
|
| 745 | 17 | $targetClass = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 746 | 17 | $mappedByMapping = $targetClass->fieldMappings[$mapping['mappedBy']] ?? []; |
|
| 747 | 17 | $mappedByFieldName = ClassMetadata::getReferenceFieldName($mappedByMapping['storeAs'] ?? ClassMetadata::REFERENCE_STORE_AS_DB_REF, $mapping['mappedBy']); |
|
| 748 | |||
| 749 | 17 | $criteria = $this->cm->merge( |
|
| 750 | 17 | [$mappedByFieldName => $ownerClass->getIdentifierObject($owner)], |
|
| 751 | 17 | $this->dm->getFilterCollection()->getFilterCriteria($targetClass), |
|
| 752 | 17 | $mapping['criteria'] ?? [] |
|
| 753 | ); |
||
| 754 | 17 | $criteria = $this->uow->getDocumentPersister($mapping['targetDocument'])->prepareQueryOrNewObj($criteria); |
|
| 755 | 17 | $qb = $this->dm->createQueryBuilder($mapping['targetDocument']) |
|
| 756 | 17 | ->setQueryArray($criteria); |
|
| 757 | |||
| 758 | 17 | if (isset($mapping['sort'])) { |
|
| 759 | 17 | $qb->sort($mapping['sort']); |
|
| 760 | } |
||
| 761 | 17 | if (isset($mapping['limit'])) { |
|
| 762 | 2 | $qb->limit($mapping['limit']); |
|
| 763 | } |
||
| 764 | 17 | if (isset($mapping['skip'])) { |
|
| 765 | $qb->skip($mapping['skip']); |
||
| 766 | } |
||
| 767 | |||
| 768 | 17 | if (! empty($hints[Query::HINT_READ_PREFERENCE])) { |
|
| 769 | $qb->setReadPreference($hints[Query::HINT_READ_PREFERENCE]); |
||
| 770 | } |
||
| 771 | |||
| 772 | 17 | foreach ($mapping['prime'] as $field) { |
|
| 773 | 4 | $qb->field($field)->prime(true); |
|
| 774 | } |
||
| 775 | |||
| 776 | 17 | return $qb->getQuery(); |
|
| 777 | } |
||
| 778 | |||
| 779 | 5 | private function loadReferenceManyWithRepositoryMethod(PersistentCollectionInterface $collection): void |
|
| 780 | { |
||
| 781 | 5 | $cursor = $this->createReferenceManyWithRepositoryMethodCursor($collection); |
|
| 782 | 5 | $mapping = $collection->getMapping(); |
|
| 783 | 5 | $documents = $cursor->toArray(); |
|
| 784 | 5 | foreach ($documents as $key => $obj) { |
|
| 785 | 5 | if (CollectionHelper::isHash($mapping['strategy'])) { |
|
| 786 | 1 | $collection->set($key, $obj); |
|
| 787 | } else { |
||
| 788 | 5 | $collection->add($obj); |
|
| 789 | } |
||
| 790 | } |
||
| 791 | 5 | } |
|
| 792 | |||
| 793 | 5 | public function createReferenceManyWithRepositoryMethodCursor(PersistentCollectionInterface $collection): Iterator |
|
| 794 | { |
||
| 795 | 5 | $mapping = $collection->getMapping(); |
|
| 796 | 5 | $repositoryMethod = $mapping['repositoryMethod']; |
|
| 797 | 5 | $cursor = $this->dm->getRepository($mapping['targetDocument']) |
|
| 798 | 5 | ->$repositoryMethod($collection->getOwner()); |
|
| 799 | |||
| 800 | 5 | if (! $cursor instanceof Iterator) { |
|
| 801 | throw new \BadMethodCallException(sprintf('Expected repository method %s to return an iterable object', $repositoryMethod)); |
||
| 802 | } |
||
| 803 | |||
| 804 | 5 | if (! empty($mapping['prime'])) { |
|
| 805 | 1 | $referencePrimer = new ReferencePrimer($this->dm, $this->dm->getUnitOfWork()); |
|
| 806 | 1 | $primers = array_combine($mapping['prime'], array_fill(0, count($mapping['prime']), true)); |
|
| 807 | 1 | $class = $this->dm->getClassMetadata($mapping['targetDocument']); |
|
| 808 | |||
| 809 | 1 | $cursor = new PrimingIterator($cursor, $class, $referencePrimer, $primers, $collection->getHints()); |
|
| 810 | } |
||
| 811 | |||
| 812 | 5 | return $cursor; |
|
| 813 | } |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Prepare a projection array by converting keys, which are PHP property |
||
| 817 | * names, to MongoDB field names. |
||
| 818 | */ |
||
| 819 | 14 | public function prepareProjection(array $fields): array |
|
| 820 | { |
||
| 821 | 14 | $preparedFields = []; |
|
| 822 | |||
| 823 | 14 | foreach ($fields as $key => $value) { |
|
| 824 | 14 | $preparedFields[$this->prepareFieldName($key)] = $value; |
|
| 825 | } |
||
| 826 | |||
| 827 | 14 | return $preparedFields; |
|
| 828 | } |
||
| 829 | |||
| 830 | /** |
||
| 831 | * @param int|string|null $sort |
||
| 832 | * |
||
| 833 | * @return int|string|null |
||
| 834 | */ |
||
| 835 | 25 | private function getSortDirection($sort) |
|
| 836 | { |
||
| 837 | 25 | switch (strtolower((string) $sort)) { |
|
| 838 | 25 | case 'desc': |
|
| 839 | 15 | return -1; |
|
| 840 | |||
| 841 | 22 | case 'asc': |
|
| 842 | 13 | return 1; |
|
| 843 | } |
||
| 844 | |||
| 845 | 12 | return $sort; |
|
| 846 | } |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Prepare a sort specification array by converting keys to MongoDB field |
||
| 850 | * names and changing direction strings to int. |
||
| 851 | */ |
||
| 852 | 146 | public function prepareSort(array $fields): array |
|
| 853 | { |
||
| 854 | 146 | $sortFields = []; |
|
| 855 | |||
| 856 | 146 | foreach ($fields as $key => $value) { |
|
| 857 | 25 | $sortFields[$this->prepareFieldName($key)] = $this->getSortDirection($value); |
|
| 858 | } |
||
| 859 | |||
| 860 | 146 | return $sortFields; |
|
| 861 | } |
||
| 862 | |||
| 863 | /** |
||
| 864 | * Prepare a mongodb field name and convert the PHP property names to MongoDB field names. |
||
| 865 | */ |
||
| 866 | 436 | public function prepareFieldName(string $fieldName): string |
|
| 872 | |||
| 873 | /** |
||
| 874 | * Adds discriminator criteria to an already-prepared query. |
||
| 875 | * |
||
| 876 | * This method should be used once for query criteria and not be used for |
||
| 877 | * nested expressions. It should be called before |
||
| 878 | * {@link DocumentPerister::addFilterToPreparedQuery()}. |
||
| 879 | */ |
||
| 880 | 527 | public function addDiscriminatorToPreparedQuery(array $preparedQuery): array |
|
| 896 | |||
| 897 | /** |
||
| 898 | * Adds filter criteria to an already-prepared query. |
||
| 899 | * |
||
| 900 | * This method should be used once for query criteria and not be used for |
||
| 901 | * nested expressions. It should be called after |
||
| 902 | * {@link DocumentPerister::addDiscriminatorToPreparedQuery()}. |
||
| 903 | */ |
||
| 904 | 528 | public function addFilterToPreparedQuery(array $preparedQuery): array |
|
| 905 | { |
||
| 906 | /* If filter criteria exists for this class, prepare it and merge |
||
| 907 | * over the existing query. |
||
| 908 | * |
||
| 909 | * @todo Consider recursive merging in case the filter criteria and |
||
| 910 | * prepared query both contain top-level $and/$or operators. |
||
| 911 | */ |
||
| 912 | 528 | $filterCriteria = $this->dm->getFilterCollection()->getFilterCriteria($this->class); |
|
| 913 | 528 | if ($filterCriteria) { |
|
| 914 | 18 | $preparedQuery = $this->cm->merge($preparedQuery, $this->prepareQueryOrNewObj($filterCriteria)); |
|
| 915 | } |
||
| 916 | |||
| 917 | 528 | return $preparedQuery; |
|
| 918 | } |
||
| 919 | |||
| 920 | /** |
||
| 921 | * Prepares the query criteria or new document object. |
||
| 922 | * |
||
| 923 | * PHP field names and types will be converted to those used by MongoDB. |
||
| 924 | */ |
||
| 925 | 560 | public function prepareQueryOrNewObj(array $query, bool $isNewObj = false): array |
|
| 926 | { |
||
| 927 | 560 | $preparedQuery = []; |
|
| 928 | |||
| 929 | 560 | foreach ($query as $key => $value) { |
|
| 953 | |||
| 954 | /** |
||
| 955 | * Prepares a query value and converts the PHP value to the database value |
||
| 956 | * if it is an identifier. |
||
| 957 | * |
||
| 958 | * It also handles converting $fieldName to the database name if they are different. |
||
| 959 | * |
||
| 960 | * @param mixed $value |
||
| 961 | */ |
||
| 962 | 914 | private function prepareQueryElement(string $fieldName, $value = null, ?ClassMetadata $class = null, bool $prepareValue = true, bool $inNewObj = false): array |
|
| 1157 | |||
| 1158 | /** |
||
| 1159 | * Prepares a query expression. |
||
| 1160 | * |
||
| 1161 | * @param array|object $expression |
||
| 1162 | */ |
||
| 1163 | 80 | private function prepareQueryExpression($expression, ClassMetadata $class): array |
|
| 1190 | |||
| 1191 | /** |
||
| 1192 | * Checks whether the value has DBRef fields. |
||
| 1193 | * |
||
| 1194 | * This method doesn't check if the the value is a complete DBRef object, |
||
| 1195 | * although it should return true for a DBRef. Rather, we're checking that |
||
| 1196 | * the value has one or more fields for a DBref. In practice, this could be |
||
| 1197 | * $elemMatch criteria for matching a DBRef. |
||
| 1198 | * |
||
| 1199 | * @param mixed $value |
||
| 1200 | */ |
||
| 1201 | 81 | private function hasDBRefFields($value): bool |
|
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Checks whether the value has query operators. |
||
| 1222 | * |
||
| 1223 | * @param mixed $value |
||
| 1224 | */ |
||
| 1225 | 85 | private function hasQueryOperators($value): bool |
|
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Gets the array of discriminator values for the given ClassMetadata |
||
| 1246 | */ |
||
| 1247 | 27 | private function getClassDiscriminatorValues(ClassMetadata $metadata): array |
|
| 1266 | |||
| 1267 | 583 | private function handleCollections(object $document, array $options): void |
|
| 1290 | |||
| 1291 | /** |
||
| 1292 | * If the document is new, ignore shard key field value, otherwise throw an exception. |
||
| 1293 | * Also, shard key field should be present in actual document data. |
||
| 1294 | * |
||
| 1295 | * @throws MongoDBException |
||
| 1296 | */ |
||
| 1297 | 10 | private function guardMissingShardKey(object $document, string $shardKeyField, array $actualDocumentData): void |
|
| 1298 | { |
||
| 1299 | 10 | $dcs = $this->uow->getDocumentChangeSet($document); |
|
| 1300 | 10 | $isUpdate = $this->uow->isScheduledForUpdate($document); |
|
| 1301 | |||
| 1302 | 10 | $fieldMapping = $this->class->getFieldMappingByDbFieldName($shardKeyField); |
|
| 1303 | 10 | $fieldName = $fieldMapping['fieldName']; |
|
| 1304 | |||
| 1305 | 10 | if ($isUpdate && isset($dcs[$fieldName]) && $dcs[$fieldName][0] !== $dcs[$fieldName][1]) { |
|
| 1306 | 2 | throw MongoDBException::shardKeyFieldCannotBeChanged($shardKeyField, $this->class->getName()); |
|
| 1307 | } |
||
| 1308 | |||
| 1309 | 8 | if (! isset($actualDocumentData[$fieldName])) { |
|
| 1310 | throw MongoDBException::shardKeyFieldMissing($shardKeyField, $this->class->getName()); |
||
| 1311 | } |
||
| 1312 | 8 | } |
|
| 1313 | |||
| 1314 | /** |
||
| 1315 | * Get shard key aware query for single document. |
||
| 1316 | */ |
||
| 1317 | 295 | private function getQueryForDocument(object $document): array |
|
| 1327 | |||
| 1328 | 584 | private function getWriteOptions(array $options = []): array |
|
| 1338 | |||
| 1339 | 15 | private function prepareReference(string $fieldName, $value, array $mapping, bool $inNewObj): array |
|
| 1379 | } |
||
| 1380 |
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.