Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPLIB-1587 Validate hint option using is_document #1567

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public static function cannotCombineCodecAndTypeMap(): self
return new self('Cannot provide both "codec" and "typeMap" options');
}

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

/**
* Thrown when an argument or option is expected to be a document.
*
Expand Down
5 changes: 2 additions & 3 deletions src/Operation/Aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
use function is_array;
use function is_bool;
use function is_integer;
use function is_object;
use function is_string;
use function MongoDB\is_document;
use function MongoDB\is_last_pipeline_operator_write;
Expand Down Expand Up @@ -150,8 +149,8 @@ public function __construct(private string $databaseName, private ?string $colle
throw InvalidArgumentException::invalidType('"explain" option', $this->options['explain'], 'boolean');
}

if (isset($this->options['hint']) && ! is_string($this->options['hint']) && ! is_array($this->options['hint']) && ! is_object($this->options['hint'])) {
throw InvalidArgumentException::invalidType('"hint" option', $this->options['hint'], 'string or array or object');
if (isset($this->options['hint']) && ! is_string($this->options['hint']) && ! is_document($this->options['hint'])) {
throw InvalidArgumentException::expectedDocumentOrStringType('"hint" option', $this->options['hint']);
}

if (isset($this->options['let']) && ! is_document($this->options['let'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public function __construct(private string $databaseName, private string $collec
throw InvalidArgumentException::expectedDocumentType('"collation" option', $this->options['collation']);
}

if (isset($this->options['hint']) && ! is_string($this->options['hint']) && ! is_array($this->options['hint']) && ! is_object($this->options['hint'])) {
throw InvalidArgumentException::invalidType('"hint" option', $this->options['hint'], 'string or array or object');
if (isset($this->options['hint']) && ! is_string($this->options['hint']) && ! is_document($this->options['hint'])) {
throw InvalidArgumentException::expectedDocumentOrStringType('"hint" option', $this->options['hint']);
}

if (isset($this->options['limit']) && ! is_integer($this->options['limit'])) {
Expand Down
6 changes: 2 additions & 4 deletions src/Operation/Delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;

use function is_array;
use function is_object;
use function is_string;
use function MongoDB\is_document;
use function MongoDB\is_write_concern_acknowledged;
Expand Down Expand Up @@ -96,8 +94,8 @@ public function __construct(private string $databaseName, private string $collec
throw InvalidArgumentException::expectedDocumentType('"collation" option', $this->options['collation']);
}

if (isset($this->options['hint']) && ! is_string($this->options['hint']) && ! is_array($this->options['hint']) && ! is_object($this->options['hint'])) {
throw InvalidArgumentException::invalidType('"hint" option', $this->options['hint'], ['string', 'array', 'object']);
if (isset($this->options['hint']) && ! is_string($this->options['hint']) && ! is_document($this->options['hint'])) {
throw InvalidArgumentException::expectedDocumentOrStringType('"hint" option', $this->options['hint']);
}

if (isset($this->options['session']) && ! $this->options['session'] instanceof Session) {
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function __construct(private string $databaseName, private string $collec
}

if (isset($this->options['hint']) && ! is_string($this->options['hint']) && ! is_document($this->options['hint'])) {
throw InvalidArgumentException::invalidType('"hint" option', $this->options['hint'], 'string or array or object');
throw InvalidArgumentException::expectedDocumentOrStringType('"hint" option', $this->options['hint']);
}

if (isset($this->options['maxTimeMS']) && ! is_integer($this->options['maxTimeMS'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public function __construct(private string $databaseName, private string $collec
}
}

if (isset($this->options['hint']) && ! is_string($this->options['hint']) && ! is_array($this->options['hint']) && ! is_object($this->options['hint'])) {
throw InvalidArgumentException::invalidType('"hint" option', $this->options['hint'], 'string or array or object');
if (isset($this->options['hint']) && ! is_string($this->options['hint']) && ! is_document($this->options['hint'])) {
throw InvalidArgumentException::expectedDocumentOrStringType('"hint" option', $this->options['hint']);
}

if (isset($this->options['limit']) && ! is_integer($this->options['limit'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/FindAndModify.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ public function __construct(private string $databaseName, private string $collec
throw InvalidArgumentException::expectedDocumentType('"fields" option', $options['fields']);
}

if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {
throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], ['string', 'array', 'object']);
if (isset($options['hint']) && ! is_string($options['hint']) && ! is_document($options['hint'])) {
throw InvalidArgumentException::expectedDocumentOrStringType('"hint" option', $options['hint']);
}

if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
Expand Down
5 changes: 2 additions & 3 deletions src/Operation/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

use function is_array;
use function is_bool;
use function is_object;
use function is_string;
use function MongoDB\is_document;
use function MongoDB\is_first_key_operator;
Expand Down Expand Up @@ -122,8 +121,8 @@ public function __construct(private string $databaseName, private string $collec
throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']);
}

if (isset($options['hint']) && ! is_string($options['hint']) && ! is_array($options['hint']) && ! is_object($options['hint'])) {
throw InvalidArgumentException::invalidType('"hint" option', $options['hint'], ['string', 'array', 'object']);
if (isset($options['hint']) && ! is_string($options['hint']) && ! is_document($options['hint'])) {
throw InvalidArgumentException::expectedDocumentOrStringType('"hint" option', $options['hint']);
}

if (! is_bool($options['multi'])) {
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ protected static function getInvalidDocumentCodecValues(): array
*/
protected static function getInvalidHintValues(): array
{
return [123, 3.14, true];
return [123, 3.14, true, PackedArray::fromPHP([])];
}

/**
Expand Down