Skip to content

Commit 028bd7e

Browse files
committed
Introduce new exception factory for expected document type
1 parent 73af4cb commit 028bd7e

34 files changed

+96
-85
lines changed

src/Command/ListCollections.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function __construct(string $databaseName, array $options = [])
7979
}
8080

8181
if (isset($options['filter']) && ! is_document($options['filter'])) {
82-
throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'document');
82+
throw InvalidArgumentException::expectedDocumentType('"filter" option', $options['filter']);
8383
}
8484

8585
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {

src/Command/ListDatabases.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function __construct(array $options = [])
7777
}
7878

7979
if (isset($options['filter']) && ! is_document($options['filter'])) {
80-
throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'document');
80+
throw InvalidArgumentException::expectedDocumentType('"filter" option', $options['filter']);
8181
}
8282

8383
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {

src/Exception/InvalidArgumentException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ public static function invalidType(string $name, $value, $expectedType)
4646
return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, get_debug_type($value)));
4747
}
4848

49+
/**
50+
* Thrown when an argument or option is expected to be a document.
51+
*
52+
* @param string $name Name of the argument or option
53+
* @param mixed $value Actual value (used to derive the type)
54+
*/
55+
public static function expectedDocumentType(string $name, $value): self
56+
{
57+
return new self(sprintf('Expected %s to have type "document" (array or object) but found "%s"', $name, get_debug_type($value)));
58+
}
59+
4960
/** @param list<string> $types */
5061
private static function expectedTypesToString(array $types): string
5162
{

src/GridFS/WritableStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function __construct(CollectionWrapper $collectionWrapper, string $filena
130130
}
131131

132132
if (isset($options['metadata']) && ! is_document($options['metadata'])) {
133-
throw InvalidArgumentException::invalidType('"metadata" option', $options['metadata'], 'document');
133+
throw InvalidArgumentException::expectedDocumentType('"metadata" option', $options['metadata']);
134134
}
135135

136136
$this->chunkSize = $options['chunkSizeBytes'];

src/Model/ChangeStreamIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
7777
public function __construct(Cursor $cursor, int $firstBatchSize, $initialResumeToken, ?object $postBatchResumeToken)
7878
{
7979
if (isset($initialResumeToken) && ! is_document($initialResumeToken)) {
80-
throw InvalidArgumentException::invalidType('$initialResumeToken', $initialResumeToken, 'document');
80+
throw InvalidArgumentException::expectedDocumentType('$initialResumeToken', $initialResumeToken);
8181
}
8282

8383
parent::__construct($cursor);
@@ -236,7 +236,7 @@ public function valid(): bool
236236
private function extractResumeToken($document)
237237
{
238238
if (! is_document($document)) {
239-
throw InvalidArgumentException::invalidType('$document', $document, 'document');
239+
throw InvalidArgumentException::expectedDocumentType('$document', $document);
240240
}
241241

242242
if ($document instanceof Serializable) {

src/Model/IndexInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function __construct(array $index)
5353
}
5454

5555
if (! is_document($index['key'])) {
56-
throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'document');
56+
throw InvalidArgumentException::expectedDocumentType('"key" option', $index['key']);
5757
}
5858

5959
foreach ($index['key'] as $fieldName => $order) {

src/Operation/Aggregate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public function __construct(string $databaseName, ?string $collectionName, array
157157
}
158158

159159
if (isset($options['collation']) && ! is_document($options['collation'])) {
160-
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document');
160+
throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']);
161161
}
162162

163163
if (isset($options['explain']) && ! is_bool($options['explain'])) {
@@ -169,7 +169,7 @@ public function __construct(string $databaseName, ?string $collectionName, array
169169
}
170170

171171
if (isset($options['let']) && ! is_document($options['let'])) {
172-
throw InvalidArgumentException::invalidType('"let" option', $options['let'], 'document');
172+
throw InvalidArgumentException::expectedDocumentType('"let" option', $options['let']);
173173
}
174174

175175
if (isset($options['maxAwaitTimeMS']) && ! is_integer($options['maxAwaitTimeMS'])) {

src/Operation/BulkWrite.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function __construct(string $databaseName, string $collectionName, array
154154
}
155155

156156
if (! is_document($args[0])) {
157-
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][0]', $i, $type), $args[0], 'document');
157+
throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][0]', $i, $type), $args[0]);
158158
}
159159

160160
switch ($type) {
@@ -174,7 +174,7 @@ public function __construct(string $databaseName, string $collectionName, array
174174
$args[1]['limit'] = ($type === self::DELETE_ONE ? 1 : 0);
175175

176176
if (isset($args[1]['collation']) && ! is_document($args[1]['collation'])) {
177-
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]["collation"]', $i, $type), $args[1]['collation'], 'document');
177+
throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][1]["collation"]', $i, $type), $args[1]['collation']);
178178
}
179179

180180
$operations[$i][$type][1] = $args[1];
@@ -187,7 +187,7 @@ public function __construct(string $databaseName, string $collectionName, array
187187
}
188188

189189
if (! is_document($args[1])) {
190-
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'document');
190+
throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1]);
191191
}
192192

193193
// Treat empty arrays as replacement documents for BC
@@ -215,7 +215,7 @@ public function __construct(string $databaseName, string $collectionName, array
215215
$args[2] += ['upsert' => false];
216216

217217
if (isset($args[2]['collation']) && ! is_document($args[2]['collation'])) {
218-
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation'], 'document');
218+
throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation']);
219219
}
220220

221221
if (! is_bool($args[2]['upsert'])) {
@@ -252,7 +252,7 @@ public function __construct(string $databaseName, string $collectionName, array
252252
}
253253

254254
if (isset($args[2]['collation']) && ! is_document($args[2]['collation'])) {
255-
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation'], 'document');
255+
throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][2]["collation"]', $i, $type), $args[2]['collation']);
256256
}
257257

258258
if (! is_bool($args[2]['upsert'])) {
@@ -287,7 +287,7 @@ public function __construct(string $databaseName, string $collectionName, array
287287
}
288288

289289
if (isset($options['let']) && ! is_document($options['let'])) {
290-
throw InvalidArgumentException::invalidType('"let" option', $options['let'], 'document');
290+
throw InvalidArgumentException::expectedDocumentType('"let" option', $options['let']);
291291
}
292292

293293
if (isset($options['bypassDocumentValidation']) && ! $options['bypassDocumentValidation']) {

src/Operation/Count.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ class Count implements Executable, Explainable
9393
public function __construct(string $databaseName, string $collectionName, $filter = [], array $options = [])
9494
{
9595
if (! is_document($filter)) {
96-
throw InvalidArgumentException::invalidType('$filter', $filter, 'document');
96+
throw InvalidArgumentException::expectedDocumentType('$filter', $filter);
9797
}
9898

9999
if (isset($options['collation']) && ! is_document($options['collation'])) {
100-
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document');
100+
throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']);
101101
}
102102

103103
if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {

src/Operation/CountDocuments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class CountDocuments implements Executable
9797
public function __construct(string $databaseName, string $collectionName, $filter, array $options = [])
9898
{
9999
if (! is_document($filter)) {
100-
throw InvalidArgumentException::invalidType('$filter', $filter, 'document');
100+
throw InvalidArgumentException::expectedDocumentType('$filter', $filter);
101101
}
102102

103103
if (isset($options['limit']) && ! is_integer($options['limit'])) {

src/Operation/CreateCollection.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,19 @@ public function __construct(string $databaseName, string $collectionName, array
153153
}
154154

155155
if (isset($options['changeStreamPreAndPostImages']) && ! is_document($options['changeStreamPreAndPostImages'])) {
156-
throw InvalidArgumentException::invalidType('"changeStreamPreAndPostImages" option', $options['changeStreamPreAndPostImages'], 'document');
156+
throw InvalidArgumentException::expectedDocumentType('"changeStreamPreAndPostImages" option', $options['changeStreamPreAndPostImages']);
157157
}
158158

159159
if (isset($options['clusteredIndex']) && ! is_document($options['clusteredIndex'])) {
160-
throw InvalidArgumentException::invalidType('"clusteredIndex" option', $options['clusteredIndex'], 'document');
160+
throw InvalidArgumentException::expectedDocumentType('"clusteredIndex" option', $options['clusteredIndex']);
161161
}
162162

163163
if (isset($options['collation']) && ! is_document($options['collation'])) {
164-
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document');
164+
throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']);
165165
}
166166

167167
if (isset($options['encryptedFields']) && ! is_document($options['encryptedFields'])) {
168-
throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], 'document');
168+
throw InvalidArgumentException::expectedDocumentType('"encryptedFields" option', $options['encryptedFields']);
169169
}
170170

171171
if (isset($options['expireAfterSeconds']) && ! is_integer($options['expireAfterSeconds'])) {
@@ -177,7 +177,7 @@ public function __construct(string $databaseName, string $collectionName, array
177177
}
178178

179179
if (isset($options['indexOptionDefaults']) && ! is_document($options['indexOptionDefaults'])) {
180-
throw InvalidArgumentException::invalidType('"indexOptionDefaults" option', $options['indexOptionDefaults'], 'document');
180+
throw InvalidArgumentException::expectedDocumentType('"indexOptionDefaults" option', $options['indexOptionDefaults']);
181181
}
182182

183183
if (isset($options['max']) && ! is_integer($options['max'])) {
@@ -201,11 +201,11 @@ public function __construct(string $databaseName, string $collectionName, array
201201
}
202202

203203
if (isset($options['storageEngine']) && ! is_document($options['storageEngine'])) {
204-
throw InvalidArgumentException::invalidType('"storageEngine" option', $options['storageEngine'], 'document');
204+
throw InvalidArgumentException::expectedDocumentType('"storageEngine" option', $options['storageEngine']);
205205
}
206206

207207
if (isset($options['timeseries']) && ! is_document($options['timeseries'])) {
208-
throw InvalidArgumentException::invalidType('"timeseries" option', $options['timeseries'], 'document');
208+
throw InvalidArgumentException::expectedDocumentType('"timeseries" option', $options['timeseries']);
209209
}
210210

211211
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
@@ -221,7 +221,7 @@ public function __construct(string $databaseName, string $collectionName, array
221221
}
222222

223223
if (isset($options['validator']) && ! is_document($options['validator'])) {
224-
throw InvalidArgumentException::invalidType('"validator" option', $options['validator'], 'document');
224+
throw InvalidArgumentException::expectedDocumentType('"validator" option', $options['validator']);
225225
}
226226

227227
if (isset($options['viewOn']) && ! is_string($options['viewOn'])) {

src/Operation/CreateEncryptedCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function __construct(string $databaseName, string $collectionName, array
8484
}
8585

8686
if (! is_document($options['encryptedFields'])) {
87-
throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], 'document');
87+
throw InvalidArgumentException::expectedDocumentType('"encryptedFields" option', $options['encryptedFields']);
8888
}
8989

9090
$this->createCollection = new CreateCollection($databaseName, $collectionName, $options);

src/Operation/DatabaseCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class DatabaseCommand implements Executable
6767
public function __construct(string $databaseName, $command, array $options = [])
6868
{
6969
if (! is_document($command)) {
70-
throw InvalidArgumentException::invalidType('$command', $command, 'document');
70+
throw InvalidArgumentException::expectedDocumentType('$command', $command);
7171
}
7272

7373
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {

src/Operation/Delete.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ class Delete implements Executable, Explainable
100100
public function __construct(string $databaseName, string $collectionName, $filter, int $limit, array $options = [])
101101
{
102102
if (! is_document($filter)) {
103-
throw InvalidArgumentException::invalidType('$filter', $filter, 'document');
103+
throw InvalidArgumentException::expectedDocumentType('$filter', $filter);
104104
}
105105

106106
if ($limit !== 0 && $limit !== 1) {
107107
throw new InvalidArgumentException('$limit must be 0 or 1');
108108
}
109109

110110
if (isset($options['collation']) && ! is_document($options['collation'])) {
111-
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document');
111+
throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']);
112112
}
113113

114114
if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {
@@ -124,7 +124,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
124124
}
125125

126126
if (isset($options['let']) && ! is_document($options['let'])) {
127-
throw InvalidArgumentException::invalidType('"let" option', $options['let'], 'document');
127+
throw InvalidArgumentException::expectedDocumentType('"let" option', $options['let']);
128128
}
129129

130130
if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {

src/Operation/Distinct.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ class Distinct implements Executable, Explainable
8989
public function __construct(string $databaseName, string $collectionName, string $fieldName, $filter = [], array $options = [])
9090
{
9191
if (! is_document($filter)) {
92-
throw InvalidArgumentException::invalidType('$filter', $filter, 'document');
92+
throw InvalidArgumentException::expectedDocumentType('$filter', $filter);
9393
}
9494

9595
if (isset($options['collation']) && ! is_document($options['collation'])) {
96-
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'document');
96+
throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']);
9797
}
9898

9999
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {

src/Operation/DropEncryptedCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function __construct(string $databaseName, string $collectionName, array
6868
}
6969

7070
if (! is_document($options['encryptedFields'])) {
71-
throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], 'document');
71+
throw InvalidArgumentException::expectedDocumentType('"encryptedFields" option', $options['encryptedFields']);
7272
}
7373

7474
/** @psalm-var array{ecocCollection?: ?string, escCollection?: ?string} */

0 commit comments

Comments
 (0)