Skip to content

Commit f2c700d

Browse files
committed
Address code review feedback
1 parent 17f3ea0 commit f2c700d

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,12 +1138,13 @@ private function convertToDatabaseValue(string $fieldName, $value, ?ClassMetadat
11381138

11391139
private function prepareQueryReference(mixed $value, ClassMetadata $class): mixed
11401140
{
1141-
if (! is_array($value)) {
1142-
return $class->getDatabaseIdentifierValue($value);
1143-
}
1144-
1145-
// Objects without operators or with DBRef fields can be converted immediately
1146-
if (! $this->hasQueryOperators($value) || $this->hasDBRefFields($value)) {
1141+
if (
1142+
// Scalar values are prepared immediately
1143+
! is_array($value)
1144+
// Objects without operators can be prepared immediately
1145+
|| ! $this->hasQueryOperators($value)
1146+
// Objects with DBRef fields can be prepared immediately
1147+
|| $this->hasDBRefFields($value)) {
11471148
return $class->getDatabaseIdentifierValue($value);
11481149
}
11491150

@@ -1159,7 +1160,7 @@ private function prepareQueryReference(mixed $value, ClassMetadata $class): mixe
11591160
*
11601161
* @param mixed $value
11611162
*
1162-
* @return array<array{string, mixed}>
1163+
* @return array<array{string, mixed}> Returns an array of tuples containing the prepared field name and value
11631164
*/
11641165
private function prepareQueryElement(string $originalFieldName, $value = null, ?ClassMetadata $class = null, bool $prepareValue = true, bool $inNewObj = false, string $fieldNamePrefix = ''): array
11651166
{
@@ -1264,6 +1265,7 @@ private function prepareQueryElement(string $originalFieldName, $value = null, ?
12641265
// Third part (if any) is the name of a field
12651266
// That means, we can implode all field parts except the first as the next field name
12661267
if ($fieldNameParts[1] === '$') {
1268+
assert($partCount >= 3);
12671269
$objectProperty = $fieldNameParts[2];
12681270
$referencePrefix = $fieldNamePrefix . $mapping['name'] . '.$';
12691271
} else {
@@ -1283,7 +1285,7 @@ private function prepareQueryElement(string $originalFieldName, $value = null, ?
12831285
/*
12841286
* 1 element: impossible (because of the dot)
12851287
* 2 elements: fieldName.objectProperty, fieldName.<index>, or fieldName.$. For EmbedMany and ReferenceMany, treat the second element as index if $inNewObj is true and convert the value. Otherwise, recurse.
1286-
* 3+ elements: fieldname.foo.bar, fieldName.<index>.foo, or fieldName.$foo. For EmbedMany and ReferenceMany, treat the second element as index, and recurse into the third element. Otherwise, recurse with the second element as field name.
1288+
* 3+ elements: fieldname.foo.bar, fieldName.<index>.foo, or fieldName.$.foo. For EmbedMany and ReferenceMany, treat the second element as index, and recurse into the third element. Otherwise, recurse with the second element as field name.
12871289
*/
12881290
if ($mapping['type'] === ClassMetadata::MANY) {
12891291
if ($inNewObj || CollectionHelper::isHash($mapping['strategy'])) {
@@ -1298,7 +1300,7 @@ private function prepareQueryElement(string $originalFieldName, $value = null, ?
12981300
$newPrefix = $fieldNamePrefix . $mapping['name'] . '.' . $fieldNameParts[1] . '.';
12991301
$newFieldName = implode('.', array_slice($fieldNameParts, 2));
13001302
} else {
1301-
// When serialising a query, the second segment is a positional operator ($), a numeric index for collections, or anything else for a hash.
1303+
// When serializing a query, the second segment is a positional operator ($), a numeric index for collections, or anything else for a hash.
13021304
$newPrefix = $fieldNamePrefix . $mapping['name'] . '.';
13031305
$newFieldName = implode('.', array_slice($fieldNameParts, 1));
13041306
}

0 commit comments

Comments
 (0)