Skip to content

Commit

Permalink
PHPLIB-1587 Validate hint option using is_document and reject Packe…
Browse files Browse the repository at this point in the history
…dArray (#1567)
  • Loading branch information
GromNaN authored Jan 17, 2025
1 parent a515b67 commit 653d515
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 18 deletions.
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

0 comments on commit 653d515

Please sign in to comment.