Skip to content

Commit

Permalink
Introduce new exception factory for expected document type
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jun 27, 2023
1 parent 72cbcc1 commit 3af1d1f
Show file tree
Hide file tree
Showing 34 changed files with 96 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/Command/ListCollections.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function __construct(string $databaseName, array $options = [])
}

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

if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Command/ListDatabases.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __construct(array $options = [])
}

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

if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
Expand Down
11 changes: 11 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@

class InvalidArgumentException extends DriverInvalidArgumentException implements Exception
{
/**
* Thrown when an argument or option is expected to be a document.
*
* @param string $name Name of the argument or option
* @param mixed $value Actual value (used to derive the type)
*/
public static function expectedDocumentType(string $name, $value): self
{
return new self(sprintf('Expected %s to have type "document" (array or object) but found "%s"', $name, get_debug_type($value)));
}

/**
* Thrown when an argument or option has an invalid type.
*
Expand Down
2 changes: 1 addition & 1 deletion src/GridFS/WritableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function __construct(CollectionWrapper $collectionWrapper, string $filena
}

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

$this->chunkSize = $options['chunkSizeBytes'];
Expand Down
4 changes: 2 additions & 2 deletions src/Model/ChangeStreamIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
public function __construct(Cursor $cursor, int $firstBatchSize, $initialResumeToken, ?object $postBatchResumeToken)
{
if (isset($initialResumeToken) && ! is_document($initialResumeToken)) {
throw InvalidArgumentException::invalidType('$initialResumeToken', $initialResumeToken, 'document');
throw InvalidArgumentException::expectedDocumentType('$initialResumeToken', $initialResumeToken);
}

parent::__construct($cursor);
Expand Down Expand Up @@ -236,7 +236,7 @@ public function valid(): bool
private function extractResumeToken($document)
{
if (! is_document($document)) {
throw InvalidArgumentException::invalidType('$document', $document, 'document');
throw InvalidArgumentException::expectedDocumentType('$document', $document);
}

if ($document instanceof Serializable) {
Expand Down
2 changes: 1 addition & 1 deletion src/Model/IndexInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(array $index)
}

if (! is_document($index['key'])) {
throw InvalidArgumentException::invalidType('"key" option', $index['key'], 'document');
throw InvalidArgumentException::expectedDocumentType('"key" option', $index['key']);
}

foreach ($index['key'] as $fieldName => $order) {
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function __construct(string $databaseName, ?string $collectionName, array
}

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

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

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

if (isset($options['maxAwaitTimeMS']) && ! is_integer($options['maxAwaitTimeMS'])) {
Expand Down
12 changes: 6 additions & 6 deletions src/Operation/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function __construct(string $databaseName, string $collectionName, array
}

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

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

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

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

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

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

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

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

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

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

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

if (isset($options['bypassDocumentValidation']) && ! $options['bypassDocumentValidation']) {
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ class Count implements Executable, Explainable
public function __construct(string $databaseName, string $collectionName, $filter = [], array $options = [])
{
if (! is_document($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'document');
throw InvalidArgumentException::expectedDocumentType('$filter', $filter);
}

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

if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/CountDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class CountDocuments implements Executable
public function __construct(string $databaseName, string $collectionName, $filter, array $options = [])
{
if (! is_document($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'document');
throw InvalidArgumentException::expectedDocumentType('$filter', $filter);
}

if (isset($options['limit']) && ! is_integer($options['limit'])) {
Expand Down
16 changes: 8 additions & 8 deletions src/Operation/CreateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,19 @@ public function __construct(string $databaseName, string $collectionName, array
}

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

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

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

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

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

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

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

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

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

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

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

if (isset($options['viewOn']) && ! is_string($options['viewOn'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/CreateEncryptedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __construct(string $databaseName, string $collectionName, array
}

if (! is_document($options['encryptedFields'])) {
throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], 'document');
throw InvalidArgumentException::expectedDocumentType('"encryptedFields" option', $options['encryptedFields']);
}

$this->createCollection = new CreateCollection($databaseName, $collectionName, $options);
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/DatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class DatabaseCommand implements Executable
public function __construct(string $databaseName, $command, array $options = [])
{
if (! is_document($command)) {
throw InvalidArgumentException::invalidType('$command', $command, 'document');
throw InvalidArgumentException::expectedDocumentType('$command', $command);
}

if (isset($options['readPreference']) && ! $options['readPreference'] instanceof ReadPreference) {
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ class Delete implements Executable, Explainable
public function __construct(string $databaseName, string $collectionName, $filter, int $limit, array $options = [])
{
if (! is_document($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'document');
throw InvalidArgumentException::expectedDocumentType('$filter', $filter);
}

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

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

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

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

if (isset($options['writeConcern']) && $options['writeConcern']->isDefault()) {
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ class Distinct implements Executable, Explainable
public function __construct(string $databaseName, string $collectionName, string $fieldName, $filter = [], array $options = [])
{
if (! is_document($filter)) {
throw InvalidArgumentException::invalidType('$filter', $filter, 'document');
throw InvalidArgumentException::expectedDocumentType('$filter', $filter);
}

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

if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/DropEncryptedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct(string $databaseName, string $collectionName, array
}

if (! is_document($options['encryptedFields'])) {
throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], 'document');
throw InvalidArgumentException::expectedDocumentType('"encryptedFields" option', $options['encryptedFields']);
}

/** @psalm-var array{ecocCollection?: ?string, escCollection?: ?string} */
Expand Down
Loading

0 comments on commit 3af1d1f

Please sign in to comment.