Skip to content

Commit 1d1b26c

Browse files
committed
Use array_key_first() and refactor is_pipeline() to use is_first_key_operator()
This introduces a dependency on symfony/polyfill-php73 and bumps both polyfill packages to the latest minor version for consistency. The doc block for is_first_key_operator() was revised to note its additional purpose for validating pipeline stages.
1 parent af0651c commit 1d1b26c

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"ext-json": "*",
1515
"ext-mongodb": "^1.16.0",
1616
"jean85/pretty-package-versions": "^2.0.1",
17-
"symfony/polyfill-php80": "^1.19"
17+
"symfony/polyfill-php73": "^1.27",
18+
"symfony/polyfill-php80": "^1.27"
1819
},
1920
"require-dev": {
2021
"squizlabs/php_codesniffer": "^3.7",

src/functions.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use ReflectionClass;
3535
use ReflectionException;
3636

37+
use function array_key_first;
3738
use function assert;
3839
use function end;
3940
use function get_object_vars;
@@ -207,22 +208,26 @@ function get_encrypted_fields_from_server(string $databaseName, string $collecti
207208
/**
208209
* Return whether the first key in the document starts with a "$" character.
209210
*
210-
* This is used for differentiating update and replacement documents. Since true
211-
* and false return values may be expected in different contexts, this function
212-
* intentionally throws if $document has an unexpected type.
211+
* This is used for validating aggregation pipeline stages and differentiating
212+
* update and replacement documents. Since true and false return values may be
213+
* expected in different contexts, this function intentionally throws if
214+
* $document has an unexpected type instead of returning false.
213215
*
214216
* @internal
215-
* @param array|object $document Update or replacement document
217+
* @param array|object $document
216218
* @throws InvalidArgumentException if $document is not an array or object
217219
*/
218220
function is_first_key_operator($document): bool
219221
{
220222
$document = document_to_array($document);
221223

222-
reset($document);
223-
$firstKey = (string) key($document);
224+
$firstKey = array_key_first($document);
225+
226+
if (! is_string($firstKey)) {
227+
return false;
228+
}
224229

225-
return isset($firstKey[0]) && $firstKey[0] === '$';
230+
return '$' === $firstKey[0] ?? null;
226231
}
227232

228233
/**
@@ -266,11 +271,8 @@ function is_pipeline($pipeline): bool
266271
}
267272

268273
$expectedKey++;
269-
$stage = document_to_array($stage);
270-
reset($stage);
271-
$key = key($stage);
272274

273-
if (! is_string($key) || substr($key, 0, 1) !== '$') {
275+
if (! is_first_key_operator($stage)) {
274276
return false;
275277
}
276278
}

0 commit comments

Comments
 (0)