Skip to content

Commit ea818c8

Browse files
authored
PHPLIB-1518 WriteResult::get*Count() always return an int on acknowledged result (#1454)
1 parent f584124 commit ea818c8

15 files changed

+116
-168
lines changed

psalm.xml.dist

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<file name="stubs/BSON/PackedArray.stub.php"/>
2424
<file name="stubs/Driver/Cursor.stub.php"/>
2525
<file name="stubs/Driver/CursorInterface.stub.php"/>
26+
<file name="stubs/Driver/WriteResult.stub.php"/>
2627
</stubs>
2728

2829
<issueHandlers>

src/BulkWriteResult.php

+19-46
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717

1818
namespace MongoDB;
1919

20+
use MongoDB\Driver\Exception\LogicException;
2021
use MongoDB\Driver\WriteResult;
21-
use MongoDB\Exception\BadMethodCallException;
2222

2323
/**
2424
* Result class for a bulk write operation.
2525
*/
2626
class BulkWriteResult
2727
{
28-
private bool $isAcknowledged;
29-
3028
public function __construct(private WriteResult $writeResult, private array $insertedIds)
3129
{
32-
$this->isAcknowledged = $writeResult->isAcknowledged();
3330
}
3431

3532
/**
@@ -38,15 +35,11 @@ public function __construct(private WriteResult $writeResult, private array $ins
3835
* This method should only be called if the write was acknowledged.
3936
*
4037
* @see BulkWriteResult::isAcknowledged()
41-
* @throws BadMethodCallException if the write result is unacknowledged
38+
* @throws LogicException if the write result is unacknowledged
4239
*/
43-
public function getDeletedCount(): ?int
40+
public function getDeletedCount(): int
4441
{
45-
if ($this->isAcknowledged) {
46-
return $this->writeResult->getDeletedCount();
47-
}
48-
49-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
42+
return $this->writeResult->getDeletedCount();
5043
}
5144

5245
/**
@@ -55,15 +48,11 @@ public function getDeletedCount(): ?int
5548
* This method should only be called if the write was acknowledged.
5649
*
5750
* @see BulkWriteResult::isAcknowledged()
58-
* @throws BadMethodCallException if the write result is unacknowledged
51+
* @throws LogicException if the write result is unacknowledged
5952
*/
60-
public function getInsertedCount(): ?int
53+
public function getInsertedCount(): int
6154
{
62-
if ($this->isAcknowledged) {
63-
return $this->writeResult->getInsertedCount();
64-
}
65-
66-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
55+
return $this->writeResult->getInsertedCount();
6756
}
6857

6958
/**
@@ -86,15 +75,11 @@ public function getInsertedIds(): array
8675
* This method should only be called if the write was acknowledged.
8776
*
8877
* @see BulkWriteResult::isAcknowledged()
89-
* @throws BadMethodCallException if the write result is unacknowledged
78+
* @throws LogicException if the write result is unacknowledged
9079
*/
91-
public function getMatchedCount(): ?int
80+
public function getMatchedCount(): int
9281
{
93-
if ($this->isAcknowledged) {
94-
return $this->writeResult->getMatchedCount();
95-
}
96-
97-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
82+
return $this->writeResult->getMatchedCount();
9883
}
9984

10085
/**
@@ -106,15 +91,11 @@ public function getMatchedCount(): ?int
10691
* This method should only be called if the write was acknowledged.
10792
*
10893
* @see BulkWriteResult::isAcknowledged()
109-
* @throws BadMethodCallException if the write result is unacknowledged
94+
* @throws LogicException if the write result is unacknowledged
11095
*/
111-
public function getModifiedCount(): ?int
96+
public function getModifiedCount(): int
11297
{
113-
if ($this->isAcknowledged) {
114-
return $this->writeResult->getModifiedCount();
115-
}
116-
117-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
98+
return $this->writeResult->getModifiedCount();
11899
}
119100

120101
/**
@@ -123,15 +104,11 @@ public function getModifiedCount(): ?int
123104
* This method should only be called if the write was acknowledged.
124105
*
125106
* @see BulkWriteResult::isAcknowledged()
126-
* @throws BadMethodCallException if the write result is unacknowledged
107+
* @throws LogicException if the write result is unacknowledged
127108
*/
128-
public function getUpsertedCount(): ?int
109+
public function getUpsertedCount(): int
129110
{
130-
if ($this->isAcknowledged) {
131-
return $this->writeResult->getUpsertedCount();
132-
}
133-
134-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
111+
return $this->writeResult->getUpsertedCount();
135112
}
136113

137114
/**
@@ -145,15 +122,11 @@ public function getUpsertedCount(): ?int
145122
* This method should only be called if the write was acknowledged.
146123
*
147124
* @see BulkWriteResult::isAcknowledged()
148-
* @throws BadMethodCallException if the write result is unacknowledged
125+
* @throws LogicException if the write result is unacknowledged
149126
*/
150127
public function getUpsertedIds(): array
151128
{
152-
if ($this->isAcknowledged) {
153-
return $this->writeResult->getUpsertedIds();
154-
}
155-
156-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
129+
return $this->writeResult->getUpsertedIds();
157130
}
158131

159132
/**
@@ -164,6 +137,6 @@ public function getUpsertedIds(): array
164137
*/
165138
public function isAcknowledged(): bool
166139
{
167-
return $this->isAcknowledged;
140+
return $this->writeResult->isAcknowledged();
168141
}
169142
}

src/DeleteResult.php

+5-12
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717

1818
namespace MongoDB;
1919

20+
use MongoDB\Driver\Exception\LogicException;
2021
use MongoDB\Driver\WriteResult;
21-
use MongoDB\Exception\BadMethodCallException;
2222

2323
/**
2424
* Result class for a delete operation.
2525
*/
2626
class DeleteResult
2727
{
28-
private bool $isAcknowledged;
29-
3028
public function __construct(private WriteResult $writeResult)
3129
{
32-
$this->isAcknowledged = $writeResult->isAcknowledged();
3330
}
3431

3532
/**
@@ -38,15 +35,11 @@ public function __construct(private WriteResult $writeResult)
3835
* This method should only be called if the write was acknowledged.
3936
*
4037
* @see DeleteResult::isAcknowledged()
41-
* @throws BadMethodCallException if the write result is unacknowledged
38+
* @throws LogicException if the write result is unacknowledged
4239
*/
43-
public function getDeletedCount(): ?int
40+
public function getDeletedCount(): int
4441
{
45-
if ($this->isAcknowledged) {
46-
return $this->writeResult->getDeletedCount();
47-
}
48-
49-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
42+
return $this->writeResult->getDeletedCount();
5043
}
5144

5245
/**
@@ -57,6 +50,6 @@ public function getDeletedCount(): ?int
5750
*/
5851
public function isAcknowledged(): bool
5952
{
60-
return $this->isAcknowledged;
53+
return $this->writeResult->isAcknowledged();
6154
}
6255
}

src/Exception/BadMethodCallException.php

-11
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,4 @@ public static function classIsImmutable(string $class): self
3333
{
3434
return new self(sprintf('%s is immutable', $class));
3535
}
36-
37-
/**
38-
* Thrown when accessing a result field on an unacknowledged write result.
39-
*
40-
* @param string $method Method name
41-
* @internal
42-
*/
43-
public static function unacknowledgedWriteResultAccess(string $method): self
44-
{
45-
return new self(sprintf('%s should not be called for an unacknowledged write result', $method));
46-
}
4736
}

src/GridFS/Bucket.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -583,16 +583,9 @@ public function rename(mixed $id, string $newFilename): void
583583
return;
584584
}
585585

586-
/* If the update resulted in no modification, it's possible that the
587-
* file did not exist, in which case we must raise an error. Checking
588-
* the write result's matched count will be most efficient, but fall
589-
* back to a findOne operation if necessary (i.e. legacy writes).
590-
*/
591-
$found = $updateResult->getMatchedCount() !== null
592-
? $updateResult->getMatchedCount() === 1
593-
: $this->collectionWrapper->findFileById($id) !== null;
594-
595-
if (! $found) {
586+
// If the update resulted in no modification, it's possible that the
587+
// file did not exist, in which case we must raise an error.
588+
if ($updateResult->getMatchedCount() !== 1) {
596589
throw FileNotFoundException::byId($id, $this->getFilesNamespace());
597590
}
598591
}

src/GridFS/CollectionWrapper.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function deleteChunksByFilesId(mixed $id): void
7373
/**
7474
* Delete all GridFS files and chunks for a given filename.
7575
*/
76-
public function deleteFileAndChunksByFilename(string $filename): ?int
76+
public function deleteFileAndChunksByFilename(string $filename): int
7777
{
7878
/** @var iterable<array{_id: mixed}> $files */
7979
$files = $this->findFiles(['filename' => $filename], [
@@ -262,7 +262,7 @@ public function insertFile(array|object $file): void
262262
/**
263263
* Updates the filename field in the file document for all the files with a given filename.
264264
*/
265-
public function updateFilenameForFilename(string $filename, string $newFilename): ?int
265+
public function updateFilenameForFilename(string $filename, string $newFilename): int
266266
{
267267
return $this->filesCollection->updateMany(
268268
['filename' => $filename],

src/InsertManyResult.php

+4-11
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717

1818
namespace MongoDB;
1919

20+
use MongoDB\Driver\Exception\LogicException;
2021
use MongoDB\Driver\WriteResult;
21-
use MongoDB\Exception\BadMethodCallException;
2222

2323
/**
2424
* Result class for a multi-document insert operation.
2525
*/
2626
class InsertManyResult
2727
{
28-
private bool $isAcknowledged;
29-
3028
public function __construct(private WriteResult $writeResult, private array $insertedIds)
3129
{
32-
$this->isAcknowledged = $writeResult->isAcknowledged();
3330
}
3431

3532
/**
@@ -38,15 +35,11 @@ public function __construct(private WriteResult $writeResult, private array $ins
3835
* This method should only be called if the write was acknowledged.
3936
*
4037
* @see InsertManyResult::isAcknowledged()
41-
* @throws BadMethodCallException if the write result is unacknowledged
38+
* @throws LogicException if the write result is unacknowledged
4239
*/
43-
public function getInsertedCount(): ?int
40+
public function getInsertedCount(): int
4441
{
45-
if ($this->isAcknowledged) {
46-
return $this->writeResult->getInsertedCount();
47-
}
48-
49-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
42+
return $this->writeResult->getInsertedCount();
5043
}
5144

5245
/**

src/InsertOneResult.php

+4-11
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,16 @@
1717

1818
namespace MongoDB;
1919

20+
use MongoDB\Driver\Exception\LogicException;
2021
use MongoDB\Driver\WriteResult;
21-
use MongoDB\Exception\BadMethodCallException;
2222

2323
/**
2424
* Result class for a single-document insert operation.
2525
*/
2626
class InsertOneResult
2727
{
28-
private bool $isAcknowledged;
29-
3028
public function __construct(private WriteResult $writeResult, private mixed $insertedId)
3129
{
32-
$this->isAcknowledged = $writeResult->isAcknowledged();
3330
}
3431

3532
/**
@@ -38,15 +35,11 @@ public function __construct(private WriteResult $writeResult, private mixed $ins
3835
* This method should only be called if the write was acknowledged.
3936
*
4037
* @see InsertOneResult::isAcknowledged()
41-
* @throws BadMethodCallException if the write result is unacknowledged
38+
* @throws LogicException if the write result is unacknowledged
4239
*/
43-
public function getInsertedCount(): ?int
40+
public function getInsertedCount(): int
4441
{
45-
if ($this->isAcknowledged) {
46-
return $this->writeResult->getInsertedCount();
47-
}
48-
49-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
42+
return $this->writeResult->getInsertedCount();
5043
}
5144

5245
/**

0 commit comments

Comments
 (0)