Skip to content

PHPLIB-1174: Rely on Binary constructor default type #1121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/tutorial/client-side-encryption.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ more than one encryption key or create them dynamically.
use MongoDB\Client;
use MongoDB\Driver\ClientEncryption;

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

$clientEncryptionOpts = [
'keyVaultNamespace' => 'encryption.__keyVault',
Expand Down Expand Up @@ -78,7 +78,7 @@ side.
use MongoDB\Client;
use MongoDB\Driver\ClientEncryption;

$localKey = new Binary('<binary key data (96 bytes)>', Binary::TYPE_GENERIC);
$localKey = new Binary('<binary key data (96 bytes)>');
$encryptionOpts = [
'keyVaultNamespace' => 'encryption.__keyVault',
'kmsProviders' => [
Expand Down Expand Up @@ -142,7 +142,7 @@ encrypted fields.
use MongoDB\Client;
use MongoDB\Driver\ClientEncryption;

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

$client = new Client();

Expand Down Expand Up @@ -197,7 +197,7 @@ encrypts and decrypts values in the document.
use MongoDB\Client;
use MongoDB\Driver\ClientEncryption;

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

$clientEncryptionOpts = [
'keyVaultNamespace' => 'encryption.__keyVault',
Expand Down Expand Up @@ -250,7 +250,7 @@ To use an alternate name when referencing an encryption key, use the
use MongoDB\Client;
use MongoDB\Driver\ClientEncryption;

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

$clientEncryptionOpts = [
'keyVaultNamespace' => 'encryption.__keyVault',
Expand Down Expand Up @@ -300,7 +300,7 @@ query on the ``encryptedIndexed`` field.
use MongoDB\BSON\Binary;
use MongoDB\Client;

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

$encryptionOpts = [
'keyVaultNamespace' => 'encryption.__keyVault',
Expand Down
6 changes: 4 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@
</UnusedFunctionCall>
</file>
<file src="src/GridFS/WritableStream.php">
<TooFewArguments occurrences="1">
<code>new Binary($data)</code>
</TooFewArguments>
<UnusedFunctionCall occurrences="1">
<code>hash_update</code>
</UnusedFunctionCall>
Expand Down Expand Up @@ -292,13 +295,12 @@
<code>$this-&gt;options['typeMap']</code>
<code>$this-&gt;options['typeMap']</code>
</MixedArgument>
<MixedAssignment occurrences="6">
<MixedAssignment occurrences="5">
<code>$cmdOptions['maxAwaitTimeMS']</code>
<code>$cmd[$option]</code>
<code>$cmd['hint']</code>
<code>$cmd['readConcern']</code>
<code>$options[$option]</code>
<code>$options['writeConcern']</code>
</MixedAssignment>
<MixedMethodCall occurrences="3">
<code>isDefault</code>
Expand Down
2 changes: 1 addition & 1 deletion src/GridFS/WritableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private function insertChunkFromBuffer(): void
$chunk = [
'files_id' => $this->file['_id'],
'n' => $this->chunkOffset,
'data' => new Binary($data, Binary::TYPE_GENERIC),
'data' => new Binary($data),
Copy link
Member

@GromNaN GromNaN Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The psalm error may be due to outdated stubs.

Strangely, it only alerts on this line, whereas it should be the same everywhere you've modified the call to new Binary.

I propose that you update the baseline until JetBrains/phpstorm-stubs#1553.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aye, forgot to run CS tools locally. I'll get this fixed up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed that the Binary stub was never updated for PHPC-2101. I opened PHPORM-40 to track that, as there might have been other things missed in v1.15.

https://jira.mongodb.org/browse/PHPORM-40

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GromNaN: What does PhpStorm have to do with Psalm?

The Psalm stub for ext-mongodb only defines a few things with generics. I don't see anything in Psalm's composer.json that pulls in PhpStorm stubs.

I was under the impression that Psalm inferred extension APIs via Reflection, which should be correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exact. Sorry for the confusion.

];

if (! $this->disableMD5 && $this->hashCtx) {
Expand Down
4 changes: 2 additions & 2 deletions tests/DocumentationExamplesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,7 @@ public function testQueryableEncryption(): void
* default to the same client. */
$clientEncryption = $client->createClientEncryption([
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(ClientSideEncryptionSpecTest::LOCAL_MASTERKEY), 0)]],
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(ClientSideEncryptionSpecTest::LOCAL_MASTERKEY))]],
]);

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

$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(ClientSideEncryptionSpecTest::LOCAL_MASTERKEY), 0)]],
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(ClientSideEncryptionSpecTest::LOCAL_MASTERKEY))]],
'encryptedFieldsMap' => [
$namespace => [
'fields' => [
Expand Down
2 changes: 1 addition & 1 deletion tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function testDownloadingFileWithUnexpectedChunkSize(): void

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

$this->expectException(CorruptFileException::class);
Expand Down
16 changes: 8 additions & 8 deletions tests/GridFS/ReadableStreamFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public function setUp(): void
]);

$this->chunksCollection->insertMany([
['_id' => 1, 'files_id' => 'length-0-with-empty-chunk', 'n' => 0, 'data' => new Binary('', Binary::TYPE_GENERIC)],
['_id' => 2, 'files_id' => 'length-2', 'n' => 0, 'data' => new Binary('ab', Binary::TYPE_GENERIC)],
['_id' => 3, 'files_id' => 'length-8', 'n' => 0, 'data' => new Binary('abcd', Binary::TYPE_GENERIC)],
['_id' => 4, 'files_id' => 'length-8', 'n' => 1, 'data' => new Binary('efgh', Binary::TYPE_GENERIC)],
['_id' => 5, 'files_id' => 'length-10', 'n' => 0, 'data' => new Binary('abcd', Binary::TYPE_GENERIC)],
['_id' => 6, 'files_id' => 'length-10', 'n' => 1, 'data' => new Binary('efgh', Binary::TYPE_GENERIC)],
['_id' => 7, 'files_id' => 'length-10', 'n' => 2, 'data' => new Binary('ij', Binary::TYPE_GENERIC)],
['_id' => 1, 'files_id' => 'length-0-with-empty-chunk', 'n' => 0, 'data' => new Binary('')],
['_id' => 2, 'files_id' => 'length-2', 'n' => 0, 'data' => new Binary('ab')],
['_id' => 3, 'files_id' => 'length-8', 'n' => 0, 'data' => new Binary('abcd')],
['_id' => 4, 'files_id' => 'length-8', 'n' => 1, 'data' => new Binary('efgh')],
['_id' => 5, 'files_id' => 'length-10', 'n' => 0, 'data' => new Binary('abcd')],
['_id' => 6, 'files_id' => 'length-10', 'n' => 1, 'data' => new Binary('efgh')],
['_id' => 7, 'files_id' => 'length-10', 'n' => 2, 'data' => new Binary('ij')],
]);
}

Expand Down Expand Up @@ -161,7 +161,7 @@ public function testReadBytesWithUnexpectedChunkSize(): void
{
$this->chunksCollection->updateOne(
['files_id' => 'length-10', 'n' => 2],
['$set' => ['data' => new Binary('i', Binary::TYPE_GENERIC)]]
['$set' => ['data' => new Binary('i')]]
);

$fileDocument = $this->collectionWrapper->findFileById('length-10');
Expand Down
6 changes: 3 additions & 3 deletions tests/GridFS/StreamWrapperFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function setUp(): void
]);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function setUp(): void
$this->clientEncryption = $client->createClientEncryption([
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => [
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
],
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function setUp(): void
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => [
'aws' => self::getAWSCredentials(),
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
],
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ public function setUp(): void

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

$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
'bypassQueryAnalysis' => true,
];

Expand Down
32 changes: 16 additions & 16 deletions tests/SpecTests/ClientSideEncryptionSpecTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public function testDataKeyAndDoubleEncryption(string $providerName, $masterKey)
'aws' => Context::getAWSCredentials(),
'azure' => Context::getAzureCredentials(),
'gcp' => Context::getGCPCredentials(),
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
'kmip' => ['endpoint' => Context::getKmipEndpoint()],
],
'tlsOptions' => [
Expand Down Expand Up @@ -421,7 +421,7 @@ public function testExternalKeyVault($withExternalKeyVault): void
$encryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => [
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
],
];

Expand Down Expand Up @@ -570,7 +570,7 @@ public function testBSONSizeLimitsAndBatchSplitting(Closure $test): void
$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => [
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
],
'keyVaultClient' => $client,
];
Expand Down Expand Up @@ -599,7 +599,7 @@ public function testViewsAreProhibited(): void
$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => [
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
],
];

Expand Down Expand Up @@ -648,7 +648,7 @@ public function testCorpus($schemaMap = true): void
'aws' => Context::getAWSCredentials(),
'azure' => Context::getAzureCredentials(),
'gcp' => Context::getGCPCredentials(),
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
'kmip' => ['endpoint' => Context::getKmipEndpoint()],
],
'tlsOptions' => [
Expand Down Expand Up @@ -904,7 +904,7 @@ public function testBypassSpawningMongocryptdViaLoadingSharedLibrary(): void
$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => [
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
],
'schemaMap' => [
'db.coll' => $this->decodeJson(file_get_contents(__DIR__ . '/client-side-encryption/external/external-schema.json')),
Expand Down Expand Up @@ -946,7 +946,7 @@ public function testBypassSpawningMongocryptdViaBypassSpawn(): void
$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => [
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
],
'schemaMap' => [
'db.coll' => $this->decodeJson(file_get_contents(__DIR__ . '/client-side-encryption/external/external-schema.json')),
Expand Down Expand Up @@ -986,7 +986,7 @@ public function testBypassSpawningMongocryptdViaBypassAutoEncryption(): void
$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => [
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
],
'bypassAutoEncryption' => true,
'extraOptions' => [
Expand Down Expand Up @@ -1020,7 +1020,7 @@ public function testBypassSpawningMongocryptdViaBypassQueryAnalysis(): void
$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => [
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)],
'local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))],
],
'bypassQueryAnalysis' => true,
'extraOptions' => [
Expand Down Expand Up @@ -1350,12 +1350,12 @@ public function testExplicitEncryption(Closure $test): void
$clientEncryption = new ClientEncryption([
'keyVaultClient' => $keyVaultClient->getManager(),
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
]);

$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
'bypassQueryAnalysis' => true,
];

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

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

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

$autoEncryptionOpts = [
'keyVaultNamespace' => 'keyvault.datakeys',
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]],
'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY))]],
];

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

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

case 'altname':
Expand Down
2 changes: 1 addition & 1 deletion tests/SpecTests/DocumentsMatchConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function provideBSONTypes()
'string' => ['string', 'foo'],
'object' => ['object', new BSONDocument()],
'array' => ['array', ['foo']],
'binData' => ['binData', new Binary('', 0)],
'binData' => ['binData', new Binary('')],
'undefined' => ['undefined', $undefined],
'objectId' => ['objectId', new ObjectId()],
'bool' => ['bool', true],
Expand Down
2 changes: 1 addition & 1 deletion tests/UnifiedSpecTests/Constraint/IsBsonTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function provideTypes()
// Note: additional tests in testTypeArray
'array(indexed array)' => ['array', ['foo']],
'array(BSONArray)' => ['array', new BSONArray()],
'binData' => ['binData', new Binary('', 0)],
'binData' => ['binData', new Binary('')],
'undefined' => ['undefined', $undefined],
'objectId' => ['objectId', new ObjectId()],
'bool' => ['bool', true],
Expand Down