Skip to content

Commit bb73ec5

Browse files
committed
Add argument to is_pipeline to set return value for empty pipeline
Necessary to handle non-array values
1 parent 41aad07 commit bb73ec5

File tree

8 files changed

+16
-14
lines changed

8 files changed

+16
-14
lines changed

src/Operation/BulkWrite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function __construct(string $databaseName, string $collectionName, array
228228
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][1]', $i, $type), $args[1], 'array or object');
229229
}
230230

231-
if ($args[1] === [] || ! is_first_key_operator($args[1]) && ! is_pipeline($args[1])) {
231+
if (! is_first_key_operator($args[1]) && ! is_pipeline($args[1], false)) {
232232
throw new InvalidArgumentException(sprintf('First key in $operations[%d]["%s"][1] is neither an update operator nor a pipeline', $i, $type));
233233
}
234234

src/Operation/FindOneAndUpdate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
113113
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
114114
}
115115

116-
if ($update === [] || ! is_first_key_operator($update) && ! is_pipeline($update)) {
116+
if (! is_first_key_operator($update) && ! is_pipeline($update, false)) {
117117
throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
118118
}
119119

src/Operation/ReplaceOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
8989
throw new InvalidArgumentException('First key in $replacement argument is an update operator');
9090
}
9191

92-
if ($replacement === [] || is_pipeline($replacement)) {
92+
if (is_pipeline($replacement, false)) {
9393
throw new InvalidArgumentException('$replacement argument is a pipeline');
9494
}
9595

src/Operation/Update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
147147
throw InvalidArgumentException::invalidType('"multi" option', $options['multi'], 'boolean');
148148
}
149149

150-
if ($options['multi'] && ($update === [] || ! is_first_key_operator($update) && ! is_pipeline($update))) {
150+
if ($options['multi'] && ! is_first_key_operator($update) && ! is_pipeline($update, false)) {
151151
throw new InvalidArgumentException('"multi" option cannot be true if $update is a replacement document');
152152
}
153153

src/Operation/UpdateMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
8888
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
8989
}
9090

91-
if ($update === [] || ! is_first_key_operator($update) && ! is_pipeline($update)) {
91+
if (! is_first_key_operator($update) && ! is_pipeline($update, false)) {
9292
throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
9393
}
9494

src/Operation/UpdateOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function __construct(string $databaseName, string $collectionName, $filte
8888
throw InvalidArgumentException::invalidType('$update', $update, 'array or object');
8989
}
9090

91-
if ($update === [] || ! is_first_key_operator($update) && ! is_pipeline($update)) {
91+
if (! is_first_key_operator($update) && ! is_pipeline($update, false)) {
9292
throw new InvalidArgumentException('Expected an update document with operator as first key or a pipeline');
9393
}
9494

src/functions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ function is_first_key_operator($document): bool
223223
* @param array|object $pipeline
224224
* @throws InvalidArgumentException
225225
*/
226-
function is_pipeline($pipeline): bool
226+
function is_pipeline($pipeline, bool $emptyIsValid = true): bool
227227
{
228228
if ($pipeline instanceof PackedArray) {
229229
/* Nested documents and arrays are intentionally left as BSON. We avoid
@@ -244,7 +244,7 @@ function is_pipeline($pipeline): bool
244244
}
245245

246246
if ($pipeline === []) {
247-
return true;
247+
return $emptyIsValid;
248248
}
249249

250250
$expectedKey = 0;

tests/FunctionsTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ public function testIsLastPipelineOperatorWrite(callable $cast): void
243243
}
244244

245245
/** @dataProvider providePipelines */
246-
public function testIsPipeline($expected, $pipeline): void
246+
public function testIsPipeline($expected, $pipeline, $emptyIsValid = true): void
247247
{
248-
$this->assertSame($expected, is_pipeline($pipeline));
248+
$this->assertSame($expected, is_pipeline($pipeline, $emptyIsValid));
249249
}
250250

251251
public function providePipelines(): array
@@ -265,7 +265,6 @@ public function providePipelines(): array
265265

266266
return [
267267
// Valid pipeline in various forms
268-
'valid: empty array' => [true, []],
269268
'valid: array' => [true, $valid],
270269
'valid: Serializable' => [true, new BSONArray($valid)],
271270
'valid: PackedArray' => [true, PackedArray::fromPHP($valid)],
@@ -287,9 +286,12 @@ public function providePipelines(): array
287286
'invalid pipeline element type: Serializable' => [false, new BSONArray([new BSONArray([])])],
288287
'invalid pipeline element type: PackedArray' => [false, PackedArray::fromPHP([[]])],
289288
// Empty array has no pipeline stages
290-
'invalid empty: array' => [false, []],
291-
'invalid empty: Serializable' => [false, new BSONArray([])],
292-
'invalid empty: PackedArray' => [false, PackedArray::fromPHP([])],
289+
'valid empty: array' => [true, []],
290+
'valid empty: Serializable' => [true, new BSONArray([])],
291+
'valid empty: PackedArray' => [true, PackedArray::fromPHP([])],
292+
'invalid empty: array' => [false, [], false],
293+
'invalid empty: Serializable' => [false, new BSONArray([]), false],
294+
'invalid empty: PackedArray' => [false, PackedArray::fromPHP([]), false],
293295
// False positive: DBRef in numeric field
294296
'false positive DBRef: array' => [true, $dbrefInNumericField],
295297
'false positive DBRef: Serializable' => [true, new BSONArray($dbrefInNumericField)],

0 commit comments

Comments
 (0)