Skip to content

Commit 875c998

Browse files
committed
Stan fixes
1 parent 7c84151 commit 875c998

3 files changed

Lines changed: 44 additions & 30 deletions

File tree

src/Database/Database.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3456,7 +3456,7 @@ public function createDocument(string $collection, Document $document): Document
34563456
* @param array<Document> $documents
34573457
* @param callable|null $onNext
34583458
* @param int $batchSize
3459-
* @return void
3459+
* @return int
34603460
* @throws AuthorizationException
34613461
* @throws StructureException
34623462
* @throws \Throwable
@@ -3473,7 +3473,7 @@ public function createDocuments(
34733473
}
34743474

34753475
if (empty($documents)) {
3476-
return;
3476+
return 0;
34773477
}
34783478

34793479
$batchSize = \min(Database::INSERT_BATCH_SIZE, \max(1, $batchSize));
@@ -3525,7 +3525,7 @@ public function createDocuments(
35253525
}
35263526

35273527
foreach (\array_chunk($documents, $batchSize) as $chunk) {
3528-
$batch = $this->withTransaction(function () use ($collection, $chunk, $onNext, &$modified) {
3528+
$batch = $this->withTransaction(function () use ($collection, $chunk) {
35293529
return $this->adapter->createDocuments($collection->getId(), $chunk);
35303530
});
35313531

@@ -4079,7 +4079,7 @@ public function updateDocument(string $collection, string $id, Document $documen
40794079
* @param array<Query> $queries
40804080
* @param callable|null $onNext
40814081
* @param int $batchSize
4082-
* @return void
4082+
* @return int
40834083
* @throws AuthorizationException
40844084
* @throws ConflictException
40854085
* @throws DuplicateException
@@ -4095,9 +4095,9 @@ public function updateDocuments(
40954095
array $queries = [],
40964096
?callable $onNext = null,
40974097
int $batchSize = self::INSERT_BATCH_SIZE
4098-
): void {
4098+
): int {
40994099
if ($updates->isEmpty()) {
4100-
return;
4100+
return 0;
41014101
}
41024102

41034103
$batchSize = \min(Database::INSERT_BATCH_SIZE, \max(1, $batchSize));
@@ -4161,7 +4161,7 @@ public function updateDocuments(
41614161
}
41624162

41634163
$originalLimit = $limit;
4164-
$lastDocument = $cursor;
4164+
$last = $cursor;
41654165
$modified = 0;
41664166

41674167
// Resolve and update relationships
@@ -4176,8 +4176,8 @@ public function updateDocuments(
41764176
Query::limit($batchSize)
41774177
];
41784178

4179-
if (!empty($lastDocument)) {
4180-
$new[] = Query::cursorAfter($lastDocument);
4179+
if (!empty($last)) {
4180+
$new[] = Query::cursorAfter($last);
41814181
}
41824182

41834183
$affectedDocuments = $this->silent(fn () => $this->find(
@@ -4240,13 +4240,15 @@ public function updateDocuments(
42404240
break;
42414241
}
42424242

4243-
$lastDocument = \end($affectedDocuments);
4243+
$last = \end($affectedDocuments);
42444244
}
42454245

42464246
$this->trigger(self::EVENT_DOCUMENTS_UPDATE, new Document([
42474247
'$collection' => $collection->getId(),
42484248
'modified' => $modified
42494249
]));
4250+
4251+
return $modified;
42504252
}
42514253

42524254
/**
@@ -5454,7 +5456,7 @@ private function deleteCascade(Document $collection, Document $relatedCollection
54545456
* @param array<Query> $queries
54555457
* @param callable|null $onNext
54565458
* @param int $batchSize
5457-
* @return void
5459+
* @return int
54585460
* @throws AuthorizationException
54595461
* @throws DatabaseException
54605462
* @throws RestrictedException
@@ -5465,7 +5467,7 @@ public function deleteDocuments(
54655467
array $queries = [],
54665468
?callable $onNext = null,
54675469
int $batchSize = self::DELETE_BATCH_SIZE
5468-
): void {
5470+
): int {
54695471
if ($this->adapter->getSharedTables() && empty($this->adapter->getTenant())) {
54705472
throw new DatabaseException('Missing tenant. Tenant must be set when table sharing is enabled.');
54715473
}
@@ -5510,7 +5512,7 @@ public function deleteDocuments(
55105512
}
55115513

55125514
$originalLimit = $limit;
5513-
$lastDocument = $cursor;
5515+
$last = $cursor;
55145516
$modified = 0;
55155517

55165518
while (true) {
@@ -5524,8 +5526,8 @@ public function deleteDocuments(
55245526
Query::limit($batchSize)
55255527
];
55265528

5527-
if (!empty($lastDocument)) {
5528-
$new[] = Query::cursorAfter($lastDocument);
5529+
if (!empty($last)) {
5530+
$new[] = Query::cursorAfter($last);
55295531
}
55305532

55315533
/**
@@ -5599,13 +5601,15 @@ public function deleteDocuments(
55995601
break;
56005602
}
56015603

5602-
$lastDocument = \end($affectedDocuments);
5604+
$last = \end($affectedDocuments);
56035605
}
56045606

56055607
$this->trigger(self::EVENT_DOCUMENTS_DELETE, new Document([
56065608
'$collection' => $collection->getId(),
56075609
'modified' => $modified
56085610
]));
5611+
5612+
return $modified;
56095613
}
56105614

56115615
/**

src/Database/Mirror.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ public function createDocuments(
555555
array $documents,
556556
?callable $onNext = null,
557557
int $batchSize = self::INSERT_BATCH_SIZE
558-
): void {
558+
): int {
559559
$this->source->createDocuments(
560560
$collection,
561561
$documents,
@@ -571,7 +571,7 @@ public function createDocuments(
571571
$onNext($document);
572572
}
573573

574-
return;
574+
return \count($documents);
575575
}
576576

577577
$upgrade = $this->silent(fn () => $this->getUpgradeStatus($collection));
@@ -580,7 +580,7 @@ public function createDocuments(
580580
$onNext($document);
581581
}
582582

583-
return;
583+
return \count($documents);
584584
}
585585

586586
try {
@@ -624,6 +624,8 @@ public function createDocuments(
624624
} catch (\Throwable $err) {
625625
$this->logError('createDocuments', $err);
626626
}
627+
628+
return \count($documents);
627629
}
628630

629631
public function updateDocument(string $collection, string $id, Document $document): Document
@@ -680,7 +682,7 @@ public function updateDocuments(
680682
array $queries = [],
681683
?callable $onNext = null,
682684
int $batchSize = self::INSERT_BATCH_SIZE
683-
): void {
685+
): int {
684686
$documents = [];
685687

686688
$this->source->updateDocuments(
@@ -695,20 +697,22 @@ public function updateDocuments(
695697
\in_array($collection, self::SOURCE_ONLY_COLLECTIONS)
696698
|| $this->destination === null
697699
) {
700+
// @phpstan-ignore-next-line
698701
foreach ($documents as $document) {
699702
$onNext($document);
700703
}
701704

702-
return;
705+
return \count($documents);
703706
}
704707

705708
$upgrade = $this->silent(fn () => $this->getUpgradeStatus($collection));
706709
if ($upgrade === null || $upgrade->getAttribute('status', '') !== 'upgraded') {
710+
// @phpstan-ignore-next-line
707711
foreach ($documents as $document) {
708712
$onNext($document);
709713
}
710714

711-
return;
715+
return \count($documents);
712716
}
713717

714718
try {
@@ -724,7 +728,7 @@ public function updateDocuments(
724728
);
725729
}
726730

727-
$this->destination->withPreserveDates(
731+
$count = $this->destination->withPreserveDates(
728732
fn () =>
729733
$this->destination->updateDocuments(
730734
$collection,
@@ -747,6 +751,8 @@ public function updateDocuments(
747751
} catch (\Throwable $err) {
748752
$this->logError('updateDocuments', $err);
749753
}
754+
755+
return $count ?? 0;
750756
}
751757

752758
public function deleteDocument(string $collection, string $id): bool
@@ -797,7 +803,7 @@ public function deleteDocuments(
797803
array $queries = [],
798804
?callable $onNext = null,
799805
int $batchSize = self::DELETE_BATCH_SIZE,
800-
): void {
806+
): int {
801807
$documents = [];
802808
$this->source->deleteDocuments(
803809
$collection,
@@ -810,20 +816,22 @@ public function deleteDocuments(
810816
\in_array($collection, self::SOURCE_ONLY_COLLECTIONS)
811817
|| $this->destination === null
812818
) {
819+
// @phpstan-ignore-next-line
813820
foreach ($documents as $document) {
814821
$onNext($document);
815822
}
816823

817-
return;
824+
return \count($documents);
818825
}
819826

820827
$upgrade = $this->silent(fn () => $this->getUpgradeStatus($collection));
821828
if ($upgrade === null || $upgrade->getAttribute('status', '') !== 'upgraded') {
829+
// @phpstan-ignore-next-line
822830
foreach ($documents as $document) {
823831
$onNext($document);
824832
}
825833

826-
return;
834+
return \count($documents);
827835
}
828836

829837
try {
@@ -836,7 +844,7 @@ public function deleteDocuments(
836844
);
837845
}
838846

839-
$this->destination->deleteDocuments(
847+
$count = $this->destination->deleteDocuments(
840848
$collection,
841849
$queries,
842850
$onNext,
@@ -854,6 +862,8 @@ public function deleteDocuments(
854862
} catch (\Throwable $err) {
855863
$this->logError('deleteDocuments', $err);
856864
}
865+
866+
return $count ?? 0;
857867
}
858868

859869
public function updateAttributeRequired(string $collection, string $id, bool $required): Document

tests/e2e/Adapter/Base.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ public function testPreserveDatesCreate(): void
916916
'attr1' => 'value3',
917917
'$createdAt' => $date
918918
]),
919-
], 2);
919+
], batchSize: 2);
920920

921921
$doc1 = static::getDatabase()->getDocument('preserve_create_dates', 'doc1');
922922
$doc2 = static::getDatabase()->getDocument('preserve_create_dates', 'doc2');
@@ -2284,9 +2284,9 @@ public function testCreateDocuments(): array
22842284
]);
22852285
}
22862286

2287-
$documents = static::getDatabase()->createDocuments($collection, $documents, 3);
2287+
$count = static::getDatabase()->createDocuments($collection, $documents, 3);
22882288

2289-
$this->assertEquals($count, count($documents));
2289+
$this->assertEquals($count, \count($documents));
22902290

22912291
foreach ($documents as $document) {
22922292
$this->assertNotEmpty(true, $document->getId());

0 commit comments

Comments
 (0)