Skip to content

Commit b8af536

Browse files
authored
PHPLIB-1174: Rely on Binary constructor default type (#1121)
Update psalm-baseline.xml to suppress the TooFewArguments error since Psalm is not aware of the Binary constructor's default type.
1 parent 8b483f2 commit b8af536

13 files changed

+47
-45
lines changed

docs/tutorial/client-side-encryption.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ more than one encryption key or create them dynamically.
3939
use MongoDB\Client;
4040
use MongoDB\Driver\ClientEncryption;
4141

42-
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
42+
$localKey = new Binary('<binary key data (96 bytes)>');
4343

4444
$clientEncryptionOpts = [
4545
'keyVaultNamespace' => 'encryption.__keyVault',
@@ -78,7 +78,7 @@ side.
7878
use MongoDB\Client;
7979
use MongoDB\Driver\ClientEncryption;
8080

81-
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
81+
$localKey = new Binary('<binary key data (96 bytes)>');
8282
$encryptionOpts = [
8383
'keyVaultNamespace' => 'encryption.__keyVault',
8484
'kmsProviders' => [
@@ -142,7 +142,7 @@ encrypted fields.
142142
use MongoDB\Client;
143143
use MongoDB\Driver\ClientEncryption;
144144

145-
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
145+
$localKey = new Binary('<binary key data (96 bytes)>');
146146

147147
$client = new Client();
148148

@@ -197,7 +197,7 @@ encrypts and decrypts values in the document.
197197
use MongoDB\Client;
198198
use MongoDB\Driver\ClientEncryption;
199199

200-
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
200+
$localKey = new Binary('<binary key data (96 bytes)>');
201201

202202
$clientEncryptionOpts = [
203203
'keyVaultNamespace' => 'encryption.__keyVault',
@@ -250,7 +250,7 @@ To use an alternate name when referencing an encryption key, use the
250250
use MongoDB\Client;
251251
use MongoDB\Driver\ClientEncryption;
252252

253-
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
253+
$localKey = new Binary('<binary key data (96 bytes)>');
254254

255255
$clientEncryptionOpts = [
256256
'keyVaultNamespace' => 'encryption.__keyVault',
@@ -300,7 +300,7 @@ query on the ``encryptedIndexed`` field.
300300
use MongoDB\BSON\Binary;
301301
use MongoDB\Client;
302302

303-
$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
303+
$localKey = new Binary('<binary key data (96 bytes)>');
304304

305305
$encryptionOpts = [
306306
'keyVaultNamespace' => 'encryption.__keyVault',

psalm-baseline.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@
131131
</UnusedFunctionCall>
132132
</file>
133133
<file src="src/GridFS/WritableStream.php">
134+
<TooFewArguments occurrences="1">
135+
<code>new Binary($data)</code>
136+
</TooFewArguments>
134137
<UnusedFunctionCall occurrences="1">
135138
<code>hash_update</code>
136139
</UnusedFunctionCall>
@@ -292,13 +295,12 @@
292295
<code>$this-&gt;options['typeMap']</code>
293296
<code>$this-&gt;options['typeMap']</code>
294297
</MixedArgument>
295-
<MixedAssignment occurrences="6">
298+
<MixedAssignment occurrences="5">
296299
<code>$cmdOptions['maxAwaitTimeMS']</code>
297300
<code>$cmd[$option]</code>
298301
<code>$cmd['hint']</code>
299302
<code>$cmd['readConcern']</code>
300303
<code>$options[$option]</code>
301-
<code>$options['writeConcern']</code>
302304
</MixedAssignment>
303305
<MixedMethodCall occurrences="3">
304306
<code>isDefault</code>

src/GridFS/WritableStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private function insertChunkFromBuffer(): void
283283
$chunk = [
284284
'files_id' => $this->file['_id'],
285285
'n' => $this->chunkOffset,
286-
'data' => new Binary($data, Binary::TYPE_GENERIC),
286+
'data' => new Binary($data),
287287
];
288288

289289
if (! $this->disableMD5 && $this->hashCtx) {

tests/DocumentationExamplesTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ public function testQueryableEncryption(): void
18301830
* default to the same client. */
18311831
$clientEncryption = $client->createClientEncryption([
18321832
'keyVaultNamespace' => 'keyvault.datakeys',
1833-
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(ClientSideEncryptionSpecTest::LOCAL_MASTERKEY), 0)]],
1833+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(ClientSideEncryptionSpecTest::LOCAL_MASTERKEY))]],
18341834
]);
18351835

18361836
// Create two data keys, one for each encrypted field
@@ -1839,7 +1839,7 @@ public function testQueryableEncryption(): void
18391839

18401840
$autoEncryptionOpts = [
18411841
'keyVaultNamespace' => 'keyvault.datakeys',
1842-
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(ClientSideEncryptionSpecTest::LOCAL_MASTERKEY), 0)]],
1842+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(ClientSideEncryptionSpecTest::LOCAL_MASTERKEY))]],
18431843
'encryptedFieldsMap' => [
18441844
$namespace => [
18451845
'fields' => [

tests/GridFS/BucketFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function testDownloadingFileWithUnexpectedChunkSize(): void
170170

171171
$this->chunksCollection->updateOne(
172172
['files_id' => $id, 'n' => 0],
173-
['$set' => ['data' => new Binary('fooba', Binary::TYPE_GENERIC)]]
173+
['$set' => ['data' => new Binary('fooba')]]
174174
);
175175

176176
$this->expectException(CorruptFileException::class);

tests/GridFS/ReadableStreamFunctionalTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ public function setUp(): void
3434
]);
3535

3636
$this->chunksCollection->insertMany([
37-
['_id' => 1, 'files_id' => 'length-0-with-empty-chunk', 'n' => 0, 'data' => new Binary('', Binary::TYPE_GENERIC)],
38-
['_id' => 2, 'files_id' => 'length-2', 'n' => 0, 'data' => new Binary('ab', Binary::TYPE_GENERIC)],
39-
['_id' => 3, 'files_id' => 'length-8', 'n' => 0, 'data' => new Binary('abcd', Binary::TYPE_GENERIC)],
40-
['_id' => 4, 'files_id' => 'length-8', 'n' => 1, 'data' => new Binary('efgh', Binary::TYPE_GENERIC)],
41-
['_id' => 5, 'files_id' => 'length-10', 'n' => 0, 'data' => new Binary('abcd', Binary::TYPE_GENERIC)],
42-
['_id' => 6, 'files_id' => 'length-10', 'n' => 1, 'data' => new Binary('efgh', Binary::TYPE_GENERIC)],
43-
['_id' => 7, 'files_id' => 'length-10', 'n' => 2, 'data' => new Binary('ij', Binary::TYPE_GENERIC)],
37+
['_id' => 1, 'files_id' => 'length-0-with-empty-chunk', 'n' => 0, 'data' => new Binary('')],
38+
['_id' => 2, 'files_id' => 'length-2', 'n' => 0, 'data' => new Binary('ab')],
39+
['_id' => 3, 'files_id' => 'length-8', 'n' => 0, 'data' => new Binary('abcd')],
40+
['_id' => 4, 'files_id' => 'length-8', 'n' => 1, 'data' => new Binary('efgh')],
41+
['_id' => 5, 'files_id' => 'length-10', 'n' => 0, 'data' => new Binary('abcd')],
42+
['_id' => 6, 'files_id' => 'length-10', 'n' => 1, 'data' => new Binary('efgh')],
43+
['_id' => 7, 'files_id' => 'length-10', 'n' => 2, 'data' => new Binary('ij')],
4444
]);
4545
}
4646

@@ -161,7 +161,7 @@ public function testReadBytesWithUnexpectedChunkSize(): void
161161
{
162162
$this->chunksCollection->updateOne(
163163
['files_id' => 'length-10', 'n' => 2],
164-
['$set' => ['data' => new Binary('i', Binary::TYPE_GENERIC)]]
164+
['$set' => ['data' => new Binary('i')]]
165165
);
166166

167167
$fileDocument = $this->collectionWrapper->findFileById('length-10');

tests/GridFS/StreamWrapperFunctionalTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function setUp(): void
3030
]);
3131

3232
$this->chunksCollection->insertMany([
33-
['_id' => 1, 'files_id' => 'length-10', 'n' => 0, 'data' => new Binary('abcd', Binary::TYPE_GENERIC)],
34-
['_id' => 2, 'files_id' => 'length-10', 'n' => 1, 'data' => new Binary('efgh', Binary::TYPE_GENERIC)],
35-
['_id' => 3, 'files_id' => 'length-10', 'n' => 2, 'data' => new Binary('ij', Binary::TYPE_GENERIC)],
33+
['_id' => 1, 'files_id' => 'length-10', 'n' => 0, 'data' => new Binary('abcd')],
34+
['_id' => 2, 'files_id' => 'length-10', 'n' => 1, 'data' => new Binary('efgh')],
35+
['_id' => 3, 'files_id' => 'length-10', 'n' => 2, 'data' => new Binary('ij')],
3636
]);
3737
}
3838

tests/Operation/CreateEncryptedCollectionFunctionalTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function setUp(): void
5050
$this->clientEncryption = $client->createClientEncryption([
5151
'keyVaultNamespace' => 'keyvault.datakeys',
5252
'kmsProviders' => [
53-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
53+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
5454
],
5555
]);
5656
}

tests/SpecTests/ClientSideEncryption/Prose21_AutomaticDataEncryptionKeysTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function setUp(): void
5151
'keyVaultNamespace' => 'keyvault.datakeys',
5252
'kmsProviders' => [
5353
'aws' => self::getAWSCredentials(),
54-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
54+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
5555
],
5656
]);
5757
}

tests/SpecTests/ClientSideEncryption/Prose22_RangeExplicitEncryptionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public function setUp(): void
5353

5454
$this->clientEncryption = $client->createClientEncryption([
5555
'keyVaultNamespace' => 'keyvault.datakeys',
56-
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
56+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
5757
]);
5858

5959
$autoEncryptionOpts = [
6060
'keyVaultNamespace' => 'keyvault.datakeys',
61-
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
61+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
6262
'bypassQueryAnalysis' => true,
6363
];
6464

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public function testDataKeyAndDoubleEncryption(string $providerName, $masterKey)
283283
'aws' => Context::getAWSCredentials(),
284284
'azure' => Context::getAzureCredentials(),
285285
'gcp' => Context::getGCPCredentials(),
286-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
286+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
287287
'kmip' => ['endpoint' => Context::getKmipEndpoint()],
288288
],
289289
'tlsOptions' => [
@@ -421,7 +421,7 @@ public function testExternalKeyVault($withExternalKeyVault): void
421421
$encryptionOpts = [
422422
'keyVaultNamespace' => 'keyvault.datakeys',
423423
'kmsProviders' => [
424-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
424+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
425425
],
426426
];
427427

@@ -570,7 +570,7 @@ public function testBSONSizeLimitsAndBatchSplitting(Closure $test): void
570570
$autoEncryptionOpts = [
571571
'keyVaultNamespace' => 'keyvault.datakeys',
572572
'kmsProviders' => [
573-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
573+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
574574
],
575575
'keyVaultClient' => $client,
576576
];
@@ -599,7 +599,7 @@ public function testViewsAreProhibited(): void
599599
$autoEncryptionOpts = [
600600
'keyVaultNamespace' => 'keyvault.datakeys',
601601
'kmsProviders' => [
602-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
602+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
603603
],
604604
];
605605

@@ -648,7 +648,7 @@ public function testCorpus($schemaMap = true): void
648648
'aws' => Context::getAWSCredentials(),
649649
'azure' => Context::getAzureCredentials(),
650650
'gcp' => Context::getGCPCredentials(),
651-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
651+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
652652
'kmip' => ['endpoint' => Context::getKmipEndpoint()],
653653
],
654654
'tlsOptions' => [
@@ -904,7 +904,7 @@ public function testBypassSpawningMongocryptdViaLoadingSharedLibrary(): void
904904
$autoEncryptionOpts = [
905905
'keyVaultNamespace' => 'keyvault.datakeys',
906906
'kmsProviders' => [
907-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
907+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
908908
],
909909
'schemaMap' => [
910910
'db.coll' => $this->decodeJson(file_get_contents(__DIR__ . '/client-side-encryption/external/external-schema.json')),
@@ -946,7 +946,7 @@ public function testBypassSpawningMongocryptdViaBypassSpawn(): void
946946
$autoEncryptionOpts = [
947947
'keyVaultNamespace' => 'keyvault.datakeys',
948948
'kmsProviders' => [
949-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
949+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
950950
],
951951
'schemaMap' => [
952952
'db.coll' => $this->decodeJson(file_get_contents(__DIR__ . '/client-side-encryption/external/external-schema.json')),
@@ -986,7 +986,7 @@ public function testBypassSpawningMongocryptdViaBypassAutoEncryption(): void
986986
$autoEncryptionOpts = [
987987
'keyVaultNamespace' => 'keyvault.datakeys',
988988
'kmsProviders' => [
989-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
989+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
990990
],
991991
'bypassAutoEncryption' => true,
992992
'extraOptions' => [
@@ -1020,7 +1020,7 @@ public function testBypassSpawningMongocryptdViaBypassQueryAnalysis(): void
10201020
$autoEncryptionOpts = [
10211021
'keyVaultNamespace' => 'keyvault.datakeys',
10221022
'kmsProviders' => [
1023-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
1023+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
10241024
],
10251025
'bypassQueryAnalysis' => true,
10261026
'extraOptions' => [
@@ -1350,12 +1350,12 @@ public function testExplicitEncryption(Closure $test): void
13501350
$clientEncryption = new ClientEncryption([
13511351
'keyVaultClient' => $keyVaultClient->getManager(),
13521352
'keyVaultNamespace' => 'keyvault.datakeys',
1353-
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
1353+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
13541354
]);
13551355

13561356
$autoEncryptionOpts = [
13571357
'keyVaultNamespace' => 'keyvault.datakeys',
1358-
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
1358+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
13591359
'bypassQueryAnalysis' => true,
13601360
];
13611361

@@ -1519,7 +1519,7 @@ public function testUniqueIndexOnKeyAltNames(Closure $test): void
15191519
$clientEncryption = new ClientEncryption([
15201520
'keyVaultClient' => $client->getManager(),
15211521
'keyVaultNamespace' => 'keyvault.datakeys',
1522-
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
1522+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
15231523
]);
15241524

15251525
$clientEncryption->createDataKey('local', ['keyAltNames' => ['def']]);
@@ -1598,7 +1598,7 @@ public function testDecryptionEvents(Closure $test): void
15981598
$clientEncryption = new ClientEncryption([
15991599
'keyVaultClient' => $setupClient->getManager(),
16001600
'keyVaultNamespace' => 'keyvault.datakeys',
1601-
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
1601+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
16021602
]);
16031603

16041604
$keyId = $clientEncryption->createDataKey('local');
@@ -1613,7 +1613,7 @@ public function testDecryptionEvents(Closure $test): void
16131613

16141614
$autoEncryptionOpts = [
16151615
'keyVaultNamespace' => 'keyvault.datakeys',
1616-
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
1616+
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
16171617
];
16181618

16191619
$encryptedClient = static::createTestClient(null, ['retryReads' => false], ['autoEncryption' => $autoEncryptionOpts]);
@@ -1800,7 +1800,7 @@ public function testRewrapManyDataKey(string $srcProvider, string $dstProvider):
18001800
'azure' => Context::getAzureCredentials(),
18011801
'gcp' => Context::getGCPCredentials(),
18021802
'kmip' => ['endpoint' => Context::getKmipEndpoint()],
1803-
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
1803+
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
18041804
],
18051805
'tlsOptions' => [
18061806
'kmip' => Context::getKmsTlsOptions(),
@@ -1900,7 +1900,7 @@ private function encryptCorpusValue(string $fieldName, stdClass $data, ClientEnc
19001900

19011901
switch ($data->identifier) {
19021902
case 'id':
1903-
$encryptionOptions['keyId'] = new Binary(base64_decode($keyId), 4);
1903+
$encryptionOptions['keyId'] = new Binary(base64_decode($keyId), Binary::TYPE_UUID);
19041904
break;
19051905

19061906
case 'altname':

tests/SpecTests/DocumentsMatchConstraintTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function provideBSONTypes()
9393
'string' => ['string', 'foo'],
9494
'object' => ['object', new BSONDocument()],
9595
'array' => ['array', ['foo']],
96-
'binData' => ['binData', new Binary('', 0)],
96+
'binData' => ['binData', new Binary('')],
9797
'undefined' => ['undefined', $undefined],
9898
'objectId' => ['objectId', new ObjectId()],
9999
'bool' => ['bool', true],

tests/UnifiedSpecTests/Constraint/IsBsonTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function provideTypes()
5151
// Note: additional tests in testTypeArray
5252
'array(indexed array)' => ['array', ['foo']],
5353
'array(BSONArray)' => ['array', new BSONArray()],
54-
'binData' => ['binData', new Binary('', 0)],
54+
'binData' => ['binData', new Binary('')],
5555
'undefined' => ['undefined', $undefined],
5656
'objectId' => ['objectId', new ObjectId()],
5757
'bool' => ['bool', true],

0 commit comments

Comments
 (0)