Skip to content

Commit ab0fbc5

Browse files
committed
Convert min/max options in the metadata class
1 parent 6d0274a commit ab0fbc5

File tree

6 files changed

+84
-14
lines changed

6 files changed

+84
-14
lines changed

lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,16 @@ public function mapField(array $mapping): array
23862386
$mapping['nullable'] = false;
23872387
}
23882388

2389+
if (isset($mapping['encrypt']['queryType'])) {
2390+
// The encrypted range query options min and max must be converted to the database type
2391+
$type = Type::getType($mapping['type']);
2392+
foreach (['min', 'max'] as $option) {
2393+
if (isset($mapping['encrypt'][$option])) {
2394+
$mapping['encrypt'][$option] = $type->convertToDatabaseValue($mapping['encrypt'][$option]);
2395+
}
2396+
}
2397+
}
2398+
23892399
if (
23902400
isset($mapping['reference'])
23912401
&& isset($mapping['storeAs'])

lib/Doctrine/ODM/MongoDB/Mapping/Driver/XmlDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ private function addEncryptionMapping(SimpleXMLElement $encrypt, string $type):
940940
foreach ($encrypt->attributes() as $key => $value) {
941941
$encryptMapping[$key] = match ($key) {
942942
'queryType' => EncryptQuery::from((string) $value),
943-
'min', 'max' => $value,
943+
'min', 'max' => (string) $value,
944944
'sparsity', 'precision', 'trimFactor', 'contention' => (int) $value,
945945
};
946946
}

lib/Doctrine/ODM/MongoDB/Utility/EncryptedFieldsMapGenerator.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ private function createEncryptedFieldsMapForClass(
140140
if (isset($mapping['encrypt']['queryType'])) {
141141
$field['queries'] = array_filter($mapping['encrypt'], static fn ($v) => $v !== null);
142142
$field['queries']['queryType'] = $field['queries']['queryType']->value;
143-
144-
foreach (['min', 'max'] as $option) {
145-
if (isset($field['queries'][$option])) {
146-
$field['queries'][$option] = Type::getType($mapping['type'])->convertToDatabaseValue($field['queries'][$option]);
147-
}
148-
}
149143
}
150144

151145
yield $field;

phpstan-baseline.neon

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
parameters:
22
ignoreErrors:
3+
-
4+
message: '#^Call to an undefined method MongoDB\\Driver\\Monitoring\\CommandFailedEvent\|MongoDB\\Driver\\Monitoring\\CommandSucceededEvent\:\:getServer\(\)\.$#'
5+
identifier: method.notFound
6+
count: 1
7+
path: lib/Doctrine/ODM/MongoDB/APM/Command.php
8+
9+
-
10+
message: '#^Call to function assert\(\) with true will always evaluate to true\.$#'
11+
identifier: function.alreadyNarrowedType
12+
count: 2
13+
path: lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php
14+
15+
-
16+
message: '#^Instanceof between MongoDB\\Driver\\CursorInterface and Iterator will always evaluate to true\.$#'
17+
identifier: instanceof.alwaysTrue
18+
count: 1
19+
path: lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php
20+
21+
-
22+
message: '#^Instanceof between MongoDB\\Driver\\CursorInterface and MongoDB\\Driver\\CursorInterface will always evaluate to true\.$#'
23+
identifier: instanceof.alwaysTrue
24+
count: 1
25+
path: lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php
26+
327
-
428
message: '#^Method Doctrine\\ODM\\MongoDB\\Aggregation\\Aggregation\:\:__construct\(\) has parameter \$classMetadata with generic class Doctrine\\ODM\\MongoDB\\Mapping\\ClassMetadata but does not specify its types\: T$#'
529
identifier: missingType.generics
@@ -1092,12 +1116,36 @@ parameters:
10921116
count: 1
10931117
path: lib/Doctrine/ODM/MongoDB/Persisters/CollectionPersister.php
10941118

1119+
-
1120+
message: '#^Call to function assert\(\) with true will always evaluate to true\.$#'
1121+
identifier: function.alreadyNarrowedType
1122+
count: 2
1123+
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1124+
10951125
-
10961126
message: '#^Call to function is_array\(\) with array will always evaluate to true\.$#'
10971127
identifier: function.alreadyNarrowedType
10981128
count: 1
10991129
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
11001130

1131+
-
1132+
message: '#^Instanceof between MongoDB\\Collection and MongoDB\\Collection will always evaluate to true\.$#'
1133+
identifier: instanceof.alwaysTrue
1134+
count: 1
1135+
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1136+
1137+
-
1138+
message: '#^Instanceof between MongoDB\\Driver\\CursorInterface and Iterator will always evaluate to true\.$#'
1139+
identifier: instanceof.alwaysTrue
1140+
count: 1
1141+
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1142+
1143+
-
1144+
message: '#^Instanceof between MongoDB\\Driver\\CursorInterface and MongoDB\\Driver\\CursorInterface will always evaluate to true\.$#'
1145+
identifier: instanceof.alwaysTrue
1146+
count: 1
1147+
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1148+
11011149
-
11021150
message: '#^Method Doctrine\\ODM\\MongoDB\\Persisters\\DocumentPersister\:\:createReferenceManyInverseSideQuery\(\) has parameter \$collection with generic interface Doctrine\\ODM\\MongoDB\\PersistentCollection\\PersistentCollectionInterface but does not specify its types\: TKey, T$#'
11031151
identifier: missingType.generics
@@ -1188,6 +1236,12 @@ parameters:
11881236
count: 1
11891237
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
11901238

1239+
-
1240+
message: '#^Result of && is always true\.$#'
1241+
identifier: booleanAnd.alwaysTrue
1242+
count: 1
1243+
path: lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
1244+
11911245
-
11921246
message: '#^Call to an undefined static method Doctrine\\ODM\\MongoDB\\Proxy\\Factory\\LazyGhostProxyFactory\:\:createLazyGhost\(\)\.$#'
11931247
identifier: staticMethod.notFound
@@ -1945,43 +1999,43 @@ parameters:
19451999
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
19462000

19472001
-
1948-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:199\:\:__construct\(\) has parameter \$classNames with no value type specified in iterable type array\.$#'
2002+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:212\:\:__construct\(\) has parameter \$classNames with no value type specified in iterable type array\.$#'
19492003
identifier: missingType.iterableValue
19502004
count: 1
19512005
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
19522006

19532007
-
1954-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:199\:\:doLoadMetadata\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2008+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:212\:\:doLoadMetadata\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
19552009
identifier: missingType.generics
19562010
count: 1
19572011
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
19582012

19592013
-
1960-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:199\:\:doLoadMetadata\(\) has parameter \$parent with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2014+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:212\:\:doLoadMetadata\(\) has parameter \$parent with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
19612015
identifier: missingType.generics
19622016
count: 1
19632017
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
19642018

19652019
-
1966-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:199\:\:getAllMetadata\(\) return type with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata does not specify its types\: T$#'
2020+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:212\:\:getAllMetadata\(\) return type with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata does not specify its types\: T$#'
19672021
identifier: missingType.generics
19682022
count: 1
19692023
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
19702024

19712025
-
1972-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:199\:\:initializeReflection\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2026+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:212\:\:initializeReflection\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
19732027
identifier: missingType.generics
19742028
count: 1
19752029
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
19762030

19772031
-
1978-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:199\:\:isEntity\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2032+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:212\:\:isEntity\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
19792033
identifier: missingType.generics
19802034
count: 1
19812035
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php
19822036

19832037
-
1984-
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:199\:\:wakeupReflection\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
2038+
message: '#^Method Doctrine\\Persistence\\Mapping\\AbstractClassMetadataFactory@anonymous/tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest\.php\:212\:\:wakeupReflection\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
19852039
identifier: missingType.generics
19862040
count: 1
19872041
path: tests/Doctrine/ODM/MongoDB/Tests/Tools/EncryptedFieldsMapGeneratorTest.php

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/AbstractDriverTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Documents\Phonenumber;
1818
use Documents\Profile;
1919
use MongoDB\BSON\Decimal128;
20+
use MongoDB\BSON\Int64;
2021
use MongoDB\BSON\UTCDateTime;
2122
use PHPUnit\Framework\TestCase;
2223
use TestDocuments\EmbeddedDocument;
@@ -26,6 +27,8 @@
2627
use TestDocuments\QueryResultDocument;
2728
use TestDocuments\User;
2829

30+
use const PHP_INT_MAX;
31+
2932
abstract class AbstractDriverTestCase extends TestCase
3033
{
3134
protected MappingDriver|null $driver;
@@ -573,6 +576,12 @@ public function testEncryptQueryRangeTypes(): void
573576
'max' => 10,
574577
], $classMetadata->fieldMappings['intField']['encrypt']);
575578

579+
self::assertEquals([
580+
'queryType' => EncryptQuery::Range,
581+
'min' => new Int64(5),
582+
'max' => new Int64(PHP_INT_MAX - 5),
583+
], $classMetadata->fieldMappings['int64Field']['encrypt']);
584+
576585
self::assertEquals([
577586
'queryType' => EncryptQuery::Range,
578587
'min' => 5.5,

tests/Doctrine/ODM/MongoDB/Tests/Mapping/Driver/fixtures/xml/Documents.Encryption.RangeTypes.dcm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<field name="intField" type="int">
1111
<encrypt queryType="range" min="5" max="10"/>
1212
</field>
13+
<field name="int64Field" type="int64">
14+
<encrypt queryType="range" min="5" max="9223372036854775802"/>
15+
</field>
1316
<field name="floatField" type="float">
1417
<encrypt queryType="range" min="5.5" max="10.5"/>
1518
</field>

0 commit comments

Comments
 (0)