Skip to content

Commit 17c183d

Browse files
committed
PHPLIB-1518 WriteResult::get*Count() always return an int on acknowledged result
1 parent 251bd4b commit 17c183d

11 files changed

+78
-156
lines changed

src/BulkWriteResult.php

Lines changed: 19 additions & 46 deletions
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

Lines changed: 5 additions & 12 deletions
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

Lines changed: 0 additions & 11 deletions
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/InsertManyResult.php

Lines changed: 4 additions & 11 deletions
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

Lines changed: 4 additions & 11 deletions
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
/**

src/UpdateResult.php

Lines changed: 15 additions & 34 deletions
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 an update operation.
2525
*/
2626
class UpdateResult
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 UpdateResult::isAcknowledged()
41-
* @throws BadMethodCallException if the write result is unacknowledged
38+
* @throws LogicException if the write result is unacknowledged
4239
*/
43-
public function getMatchedCount(): ?int
40+
public function getMatchedCount(): int
4441
{
45-
if ($this->isAcknowledged) {
46-
return $this->writeResult->getMatchedCount();
47-
}
48-
49-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
42+
return $this->writeResult->getMatchedCount();
5043
}
5144

5245
/**
@@ -58,15 +51,11 @@ public function getMatchedCount(): ?int
5851
* This method should only be called if the write was acknowledged.
5952
*
6053
* @see UpdateResult::isAcknowledged()
61-
* @throws BadMethodCallException if the write result is unacknowledged
54+
* @throws LogicException if the write result is unacknowledged
6255
*/
63-
public function getModifiedCount(): ?int
56+
public function getModifiedCount(): int
6457
{
65-
if ($this->isAcknowledged) {
66-
return $this->writeResult->getModifiedCount();
67-
}
68-
69-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
58+
return $this->writeResult->getModifiedCount();
7059
}
7160

7261
/**
@@ -75,15 +64,11 @@ public function getModifiedCount(): ?int
7564
* This method should only be called if the write was acknowledged.
7665
*
7766
* @see UpdateResult::isAcknowledged()
78-
* @throws BadMethodCallException if the write result is unacknowledged
67+
* @throws LogicException if the write result is unacknowledged
7968
*/
80-
public function getUpsertedCount(): ?int
69+
public function getUpsertedCount(): int
8170
{
82-
if ($this->isAcknowledged) {
83-
return $this->writeResult->getUpsertedCount();
84-
}
85-
86-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
71+
return $this->writeResult->getUpsertedCount();
8772
}
8873

8974
/**
@@ -98,19 +83,15 @@ public function getUpsertedCount(): ?int
9883
* This method should only be called if the write was acknowledged.
9984
*
10085
* @see UpdateResult::isAcknowledged()
101-
* @throws BadMethodCallException if the write result is unacknowledged
86+
* @throws LogicException if the write result is unacknowledged
10287
*/
10388
public function getUpsertedId(): mixed
10489
{
105-
if ($this->isAcknowledged) {
106-
foreach ($this->writeResult->getUpsertedIds() as $id) {
107-
return $id;
108-
}
109-
110-
return null;
90+
foreach ($this->writeResult->getUpsertedIds() as $id) {
91+
return $id;
11192
}
11293

113-
throw BadMethodCallException::unacknowledgedWriteResultAccess(__METHOD__);
94+
return null;
11495
}
11596

11697
/**
@@ -122,6 +103,6 @@ public function getUpsertedId(): mixed
122103
*/
123104
public function isAcknowledged(): bool
124105
{
125-
return $this->isAcknowledged;
106+
return $this->writeResult->isAcknowledged();
126107
}
127108
}

0 commit comments

Comments
 (0)