Skip to content

Commit 653d515

Browse files
authored
PHPLIB-1587 Validate hint option using is_document and reject PackedArray (#1567)
1 parent a515b67 commit 653d515

File tree

9 files changed

+25
-18
lines changed

9 files changed

+25
-18
lines changed

src/Exception/InvalidArgumentException.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ public static function cannotCombineCodecAndTypeMap(): self
3434
return new self('Cannot provide both "codec" and "typeMap" options');
3535
}
3636

37+
/**
38+
* Thrown when an argument or option is expected to be a string or a document.
39+
*
40+
* @param string $name Name of the argument or option
41+
* @param mixed $value Actual value (used to derive the type)
42+
*/
43+
public static function expectedDocumentOrStringType(string $name, mixed $value): self
44+
{
45+
return new self(sprintf('Expected %s to have type "string" or "document" (array or object) but found "%s"', $name, get_debug_type($value)));
46+
}
47+
3748
/**
3849
* Thrown when an argument or option is expected to be a document.
3950
*

src/Operation/Aggregate.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
use function is_array;
3838
use function is_bool;
3939
use function is_integer;
40-
use function is_object;
4140
use function is_string;
4241
use function MongoDB\is_document;
4342
use function MongoDB\is_last_pipeline_operator_write;
@@ -150,8 +149,8 @@ public function __construct(private string $databaseName, private ?string $colle
150149
throw InvalidArgumentException::invalidType('"explain" option', $this->options['explain'], 'boolean');
151150
}
152151

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

157156
if (isset($this->options['let']) && ! is_document($this->options['let'])) {

src/Operation/Count.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ public function __construct(private string $databaseName, private string $collec
9090
throw InvalidArgumentException::expectedDocumentType('"collation" option', $this->options['collation']);
9191
}
9292

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

9797
if (isset($this->options['limit']) && ! is_integer($this->options['limit'])) {

src/Operation/Delete.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
use MongoDB\Exception\InvalidArgumentException;
2727
use MongoDB\Exception\UnsupportedException;
2828

29-
use function is_array;
30-
use function is_object;
3129
use function is_string;
3230
use function MongoDB\is_document;
3331
use function MongoDB\is_write_concern_acknowledged;
@@ -96,8 +94,8 @@ public function __construct(private string $databaseName, private string $collec
9694
throw InvalidArgumentException::expectedDocumentType('"collation" option', $this->options['collation']);
9795
}
9896

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

103101
if (isset($this->options['session']) && ! $this->options['session'] instanceof Session) {

src/Operation/Distinct.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public function __construct(private string $databaseName, private string $collec
9191
}
9292

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

9797
if (isset($this->options['maxTimeMS']) && ! is_integer($this->options['maxTimeMS'])) {

src/Operation/Find.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ public function __construct(private string $databaseName, private string $collec
195195
}
196196
}
197197

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

202202
if (isset($this->options['limit']) && ! is_integer($this->options['limit'])) {

src/Operation/FindAndModify.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ public function __construct(private string $databaseName, private string $collec
151151
throw InvalidArgumentException::expectedDocumentType('"fields" option', $options['fields']);
152152
}
153153

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

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

src/Operation/Update.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
use function is_array;
3030
use function is_bool;
31-
use function is_object;
3231
use function is_string;
3332
use function MongoDB\is_document;
3433
use function MongoDB\is_first_key_operator;
@@ -122,8 +121,8 @@ public function __construct(private string $databaseName, private string $collec
122121
throw InvalidArgumentException::expectedDocumentType('"collation" option', $options['collation']);
123122
}
124123

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

129128
if (! is_bool($options['multi'])) {

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ protected static function getInvalidDocumentCodecValues(): array
250250
*/
251251
protected static function getInvalidHintValues(): array
252252
{
253-
return [123, 3.14, true];
253+
return [123, 3.14, true, PackedArray::fromPHP([])];
254254
}
255255

256256
/**

0 commit comments

Comments
 (0)