Skip to content

Commit 8f646a3

Browse files
committed
PHPLIB-141: Replace InvalidArgumentTypeException with a factory method
1 parent 74af87d commit 8f646a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+199
-211
lines changed

src/Client.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\Driver\Manager;
88
use MongoDB\Driver\ReadPreference;
99
use MongoDB\Driver\WriteConcern;
10+
use MongoDB\Exception\InvalidArgumentException;
1011
use MongoDB\Model\DatabaseInfoIterator;
1112
use MongoDB\Operation\DropDatabase;
1213
use MongoDB\Operation\ListDatabases;
@@ -49,7 +50,7 @@ public function __construct($uri = 'mongodb://localhost:27017', array $uriOption
4950
];
5051

5152
if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
52-
throw new InvalidArgumentTypeException('"typeMap" driver option', $driverOptions['typeMap'], 'array');
53+
throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
5354
}
5455

5556
$this->manager = new Manager($uri, $uriOptions, $driverOptions);

src/Collection.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use MongoDB\Driver\Server;
1111
use MongoDB\Driver\WriteConcern;
1212
use MongoDB\Exception\InvalidArgumentException;
13-
use MongoDB\Exception\InvalidArgumentTypeException;
1413
use MongoDB\Model\IndexInfoIterator;
1514
use MongoDB\Model\IndexInput;
1615
use MongoDB\Operation\Aggregate;
@@ -85,19 +84,19 @@ public function __construct(Manager $manager, $namespace, array $options = [])
8584
$this->collectionName = $parts[1];
8685

8786
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
88-
throw new InvalidArgumentTypeException('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
87+
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
8988
}
9089

9190
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
92-
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
91+
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
9392
}
9493

9594
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
96-
throw new InvalidArgumentTypeException('"typeMap" option', $options['typeMap'], 'array');
95+
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
9796
}
9897

9998
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
100-
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
99+
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
101100
}
102101

103102
$this->manager = $manager;

src/Database.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use MongoDB\Driver\Server;
1212
use MongoDB\Driver\WriteConcern;
1313
use MongoDB\Exception\InvalidArgumentException;
14-
use MongoDB\Exception\InvalidArgumentTypeException;
1514
use MongoDB\Model\CollectionInfoIterator;
1615
use MongoDB\Operation\CreateCollection;
1716
use MongoDB\Operation\DatabaseCommand;
@@ -62,19 +61,19 @@ public function __construct(Manager $manager, $databaseName, array $options = []
6261
}
6362

6463
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
65-
throw new InvalidArgumentTypeException('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
64+
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
6665
}
6766

6867
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
69-
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
68+
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
7069
}
7170

7271
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
73-
throw new InvalidArgumentTypeException('"typeMap" option', $options['typeMap'], 'array');
72+
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
7473
}
7574

7675
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
77-
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
76+
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
7877
}
7978

8079
$this->manager = $manager;

src/Exception/InvalidArgumentException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,16 @@
44

55
class InvalidArgumentException extends \MongoDB\Driver\Exception\InvalidArgumentException implements Exception
66
{
7+
/**
8+
* Thrown when an argument or option has an invalid type.
9+
*
10+
* @param string $name Name of the argument or option
11+
* @param mixed $value Actual value (used to derive the type)
12+
* @param string $expectedType Expected type
13+
* @return self
14+
*/
15+
public static function invalidType($name, $value, $expectedType)
16+
{
17+
return new static(sprintf('Expected %s to have type "%s" but found "%s"', $name, $expectedType, is_object($value) ? get_class($value) : gettype($value)));
18+
}
719
}

src/Exception/InvalidArgumentTypeException.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Model/IndexInput.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use MongoDB\BSON\Serializable;
66
use MongoDB\Exception\InvalidArgumentException;
7-
use MongoDB\Exception\InvalidArgumentTypeException;
87

98
/**
109
* Index input model class.
@@ -24,6 +23,7 @@ class IndexInput implements Serializable
2423
* Constructor.
2524
*
2625
* @param array $index Index specification
26+
* @throws InvalidArgumentException
2727
*/
2828
public function __construct(array $index)
2929
{
@@ -32,12 +32,12 @@ public function __construct(array $index)
3232
}
3333

3434
if ( ! is_array($index['key']) && ! is_object($index['key'])) {
35-
throw new InvalidArgumentTypeException('"key" option', $index['key'], 'array or object');
35+
throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'array or object');
3636
}
3737

3838
foreach ($index['key'] as $fieldName => $order) {
3939
if ( ! is_int($order) && ! is_float($order) && ! is_string($order)) {
40-
throw new InvalidArgumentTypeException(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string');
40+
throw InvalidArgumentException::invalidType(sprintf('order value for "%s" field within "key" option', $fieldName), $order, 'numeric or string');
4141
}
4242
}
4343

@@ -46,15 +46,15 @@ public function __construct(array $index)
4646
}
4747

4848
if ( ! is_string($index['ns'])) {
49-
throw new InvalidArgumentTypeException('"ns" option', $index['ns'], 'string');
49+
throw InvalidArgumentException::invalidType('"ns" option', $index['ns'], 'string');
5050
}
5151

5252
if ( ! isset($index['name'])) {
5353
$index['name'] = \MongoDB\generate_index_name($index['key']);
5454
}
5555

5656
if ( ! is_string($index['name'])) {
57-
throw new InvalidArgumentTypeException('"name" option', $index['name'], 'string');
57+
throw InvalidArgumentException::invalidType('"name" option', $index['name'], 'string');
5858
}
5959

6060
$this->index = $index;

src/Operation/Aggregate.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use MongoDB\Driver\ReadPreference;
88
use MongoDB\Driver\Server;
99
use MongoDB\Exception\InvalidArgumentException;
10-
use MongoDB\Exception\InvalidArgumentTypeException;
1110
use MongoDB\Exception\UnexpectedValueException;
1211
use ArrayIterator;
1312
use stdClass;
@@ -95,7 +94,7 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
9594
}
9695

9796
if ( ! is_array($operation) && ! is_object($operation)) {
98-
throw new InvalidArgumentTypeException(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
97+
throw InvalidArgumentException::invalidType(sprintf('$pipeline[%d]', $i), $operation, 'array or object');
9998
}
10099

101100
$expectedIndex += 1;
@@ -107,35 +106,35 @@ public function __construct($databaseName, $collectionName, array $pipeline, arr
107106
];
108107

109108
if ( ! is_bool($options['allowDiskUse'])) {
110-
throw new InvalidArgumentTypeException('"allowDiskUse" option', $options['allowDiskUse'], 'boolean');
109+
throw InvalidArgumentException::invalidType('"allowDiskUse" option', $options['allowDiskUse'], 'boolean');
111110
}
112111

113112
if (isset($options['batchSize']) && ! is_integer($options['batchSize'])) {
114-
throw new InvalidArgumentTypeException('"batchSize" option', $options['batchSize'], 'integer');
113+
throw InvalidArgumentException::invalidType('"batchSize" option', $options['batchSize'], 'integer');
115114
}
116115

117116
if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
118-
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
117+
throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
119118
}
120119

121120
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
122-
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
121+
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
123122
}
124123

125124
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
126-
throw new InvalidArgumentTypeException('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
125+
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
127126
}
128127

129128
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
130-
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
129+
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
131130
}
132131

133132
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
134-
throw new InvalidArgumentTypeException('"typeMap" option', $options['typeMap'], 'array');
133+
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
135134
}
136135

137136
if ( ! is_bool($options['useCursor'])) {
138-
throw new InvalidArgumentTypeException('"useCursor" option', $options['useCursor'], 'boolean');
137+
throw InvalidArgumentException::invalidType('"useCursor" option', $options['useCursor'], 'boolean');
139138
}
140139

141140
if (isset($options['batchSize']) && ! $options['useCursor']) {

src/Operation/BulkWrite.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use MongoDB\Driver\Server;
88
use MongoDB\Driver\WriteConcern;
99
use MongoDB\Exception\InvalidArgumentException;
10-
use MongoDB\Exception\InvalidArgumentTypeException;
1110

1211
/**
1312
* Operation for executing multiple write operations.
@@ -85,7 +84,7 @@ public function __construct($databaseName, $collectionName, array $operations, a
8584
}
8685

8786
if ( ! is_array($operation)) {
88-
throw new InvalidArgumentTypeException(sprintf('$operations[%d]', $i), $operation, 'array');
87+
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]', $i), $operation, 'array');
8988
}
9089

9190
if (count($operation) !== 1) {
@@ -100,7 +99,7 @@ public function __construct($databaseName, $collectionName, array $operations, a
10099
}
101100

102101
if ( ! is_array($args[0]) && ! is_object($args[0])) {
103-
throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][0]', $i, $type), $args[0], 'array or object');
102+
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][0]', $i, $type), $args[0], 'array or object');
104103
}
105104

106105
switch ($type) {
@@ -119,7 +118,7 @@ public function __construct($databaseName, $collectionName, array $operations, a
119118
}
120119

121120
if ( ! is_array($args[1]) && ! is_object($args[1])) {
122-
throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
121+
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
123122
}
124123

125124
if (\MongoDB\is_first_key_operator($args[1])) {
@@ -131,14 +130,14 @@ public function __construct($databaseName, $collectionName, array $operations, a
131130
}
132131

133132
if ( ! is_array($args[2])) {
134-
throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array');
133+
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array');
135134
}
136135

137136
$args[2]['multi'] = false;
138137
$args[2] += ['upsert' => false];
139138

140139
if ( ! is_bool($args[2]['upsert'])) {
141-
throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
140+
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
142141
}
143142

144143
$operations[$i][$type][2] = $args[2];
@@ -152,7 +151,7 @@ public function __construct($databaseName, $collectionName, array $operations, a
152151
}
153152

154153
if ( ! is_array($args[1]) && ! is_object($args[1])) {
155-
throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
154+
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
156155
}
157156

158157
if ( ! \MongoDB\is_first_key_operator($args[1])) {
@@ -164,14 +163,14 @@ public function __construct($databaseName, $collectionName, array $operations, a
164163
}
165164

166165
if ( ! is_array($args[2])) {
167-
throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array');
166+
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]', $i, $type), $args[2], 'array');
168167
}
169168

170169
$args[2]['multi'] = ($type === self::UPDATE_MANY);
171170
$args[2] += ['upsert' => false];
172171

173172
if ( ! is_bool($args[2]['upsert'])) {
174-
throw new InvalidArgumentTypeException(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
173+
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
175174
}
176175

177176
$operations[$i][$type][2] = $args[2];
@@ -188,15 +187,15 @@ public function __construct($databaseName, $collectionName, array $operations, a
188187
$options += ['ordered' => true];
189188

190189
if (isset($options['bypassDocumentValidation']) && ! is_bool($options['bypassDocumentValidation'])) {
191-
throw new InvalidArgumentTypeException('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
190+
throw InvalidArgumentException::invalidType('"bypassDocumentValidation" option', $options['bypassDocumentValidation'], 'boolean');
192191
}
193192

194193
if ( ! is_bool($options['ordered'])) {
195-
throw new InvalidArgumentTypeException('"ordered" option', $options['ordered'], 'boolean');
194+
throw InvalidArgumentException::invalidType('"ordered" option', $options['ordered'], 'boolean');
196195
}
197196

198197
if (isset($options['writeConcern']) && ! $options['writeConcern'] instanceof WriteConcern) {
199-
throw new InvalidArgumentTypeException('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
198+
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], 'MongoDB\Driver\WriteConcern');
200199
}
201200

202201
$this->databaseName = (string) $databaseName;

src/Operation/Count.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use MongoDB\Driver\ReadPreference;
88
use MongoDB\Driver\Server;
99
use MongoDB\Exception\InvalidArgumentException;
10-
use MongoDB\Exception\InvalidArgumentTypeException;
1110
use MongoDB\Exception\UnexpectedValueException;
1211

1312
/**
@@ -58,7 +57,7 @@ class Count implements Executable
5857
public function __construct($databaseName, $collectionName, $filter = [], array $options = [])
5958
{
6059
if ( ! is_array($filter) && ! is_object($filter)) {
61-
throw new InvalidArgumentTypeException('$filter', $filter, 'array or object');
60+
throw InvalidArgumentException::invalidType('$filter', $filter, 'array or object');
6261
}
6362

6463
if (isset($options['hint'])) {
@@ -67,28 +66,28 @@ public function __construct($databaseName, $collectionName, $filter = [], array
6766
}
6867

6968
if ( ! is_string($options['hint'])) {
70-
throw new InvalidArgumentTypeException('"hint" option', $options['hint'], 'string or array or object');
69+
throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], 'string or array or object');
7170
}
7271
}
7372

7473
if (isset($options['limit']) && ! is_integer($options['limit'])) {
75-
throw new InvalidArgumentTypeException('"limit" option', $options['limit'], 'integer');
74+
throw InvalidArgumentException::invalidType('"limit" option', $options['limit'], 'integer');
7675
}
7776

7877
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
79-
throw new InvalidArgumentTypeException('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
78+
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
8079
}
8180

8281
if (isset($options['readConcern']) && ! $options['readConcern'] instanceof ReadConcern) {
83-
throw new InvalidArgumentTypeException('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
82+
throw InvalidArgumentException::invalidType('"readConcern" option', $options['readConcern'], 'MongoDB\Driver\ReadConcern');
8483
}
8584

8685
if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
87-
throw new InvalidArgumentTypeException('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
86+
throw InvalidArgumentException::invalidType('"readPreference" option', $options['readPreference'], 'MongoDB\Driver\ReadPreference');
8887
}
8988

9089
if (isset($options['skip']) && ! is_integer($options['skip'])) {
91-
throw new InvalidArgumentTypeException('"skip" option', $options['skip'], 'integer');
90+
throw InvalidArgumentException::invalidType('"skip" option', $options['skip'], 'integer');
9291
}
9392

9493
$this->databaseName = (string) $databaseName;

0 commit comments

Comments
 (0)