Skip to content

Commit f4962c9

Browse files
authored
PHPLIB-1227 Use void return types for operations without meaningful result document (#1468)
1 parent 3d46eaf commit f4962c9

27 files changed

+89
-272
lines changed

UPGRADE-2.0.md

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ UPGRADE FROM 1.x to 2.0
2323
* `MongoDB\Model\IndexInfoIteratorIterator`
2424
* `MongoDB\Operation\Executable`
2525

26+
Operations with no result
27+
-------------------------
28+
29+
The following operations no longer return the raw command result. The return
30+
type changed to `void`. In case of an error, an exception is thrown.
31+
32+
* `MongoDB\Client`: `dropDatabase`
33+
* `MongoDB\Collection`: `drop`, `dropIndex`, `dropIndexes`, `dropSearchIndex`, `rename`
34+
* `MongoDB\Database`: `createCollection`, `drop`, `dropCollection`, `renameCollection`
35+
* `MongoDB\Database::createEncryptedCollection()` returns the list of encrypted fields
36+
37+
If you still need to access the raw command result, you can use a
38+
[`CommandSubscriber`](https://www.php.net/manual/en/class.mongodb-driver-monitoring-commandsubscriber.php).
39+
2640
GridFS
2741
------
2842

psalm-baseline.xml

-15
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,6 @@
519519
</MixedMethodCall>
520520
</file>
521521
<file src="src/Operation/CreateCollection.php">
522-
<MixedArgument>
523-
<code><![CDATA[$this->options['typeMap']]]></code>
524-
</MixedArgument>
525522
<MixedAssignment>
526523
<code><![CDATA[$cmd[$option]]]></code>
527524
<code><![CDATA[$options['session']]]></code>
@@ -593,9 +590,6 @@
593590
</MixedMethodCall>
594591
</file>
595592
<file src="src/Operation/DropCollection.php">
596-
<MixedArgument>
597-
<code><![CDATA[$this->options['typeMap']]]></code>
598-
</MixedArgument>
599593
<MixedAssignment>
600594
<code><![CDATA[$cmd['comment']]]></code>
601595
<code><![CDATA[$options['session']]]></code>
@@ -606,9 +600,6 @@
606600
</MixedMethodCall>
607601
</file>
608602
<file src="src/Operation/DropDatabase.php">
609-
<MixedArgument>
610-
<code><![CDATA[$this->options['typeMap']]]></code>
611-
</MixedArgument>
612603
<MixedAssignment>
613604
<code><![CDATA[$cmd['comment']]]></code>
614605
<code><![CDATA[$options['session']]]></code>
@@ -621,9 +612,6 @@
621612
</MixedArgument>
622613
</file>
623614
<file src="src/Operation/DropIndexes.php">
624-
<MixedArgument>
625-
<code><![CDATA[$this->options['typeMap']]]></code>
626-
</MixedArgument>
627615
<MixedAssignment>
628616
<code><![CDATA[$cmd[$option]]]></code>
629617
<code><![CDATA[$options['session']]]></code>
@@ -755,9 +743,6 @@
755743
</MixedAssignment>
756744
</file>
757745
<file src="src/Operation/RenameCollection.php">
758-
<MixedArgument>
759-
<code><![CDATA[$this->options['typeMap']]]></code>
760-
</MixedArgument>
761746
<MixedAssignment>
762747
<code><![CDATA[$cmd[$option]]]></code>
763748
<code><![CDATA[$options['session']]]></code>

src/Client.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@
4949
use function array_diff_key;
5050
use function is_array;
5151
use function is_string;
52-
use function sprintf;
53-
use function trigger_error;
54-
55-
use const E_USER_DEPRECATED;
5652

5753
class Client
5854
{
@@ -217,19 +213,12 @@ public function createClientEncryption(array $options): ClientEncryption
217213
* @see DropDatabase::__construct() for supported options
218214
* @param string $databaseName Database name
219215
* @param array $options Additional options
220-
* @return array|object Command result document
221216
* @throws UnsupportedException if options are unsupported on the selected server
222217
* @throws InvalidArgumentException for parameter/option parsing errors
223218
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
224219
*/
225-
public function dropDatabase(string $databaseName, array $options = []): array|object
220+
public function dropDatabase(string $databaseName, array $options = []): void
226221
{
227-
if (! isset($options['typeMap'])) {
228-
$options['typeMap'] = $this->typeMap;
229-
} else {
230-
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
231-
}
232-
233222
$server = select_server_for_write($this->manager, $options);
234223

235224
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -238,7 +227,7 @@ public function dropDatabase(string $databaseName, array $options = []): array|o
238227

239228
$operation = new DropDatabase($databaseName, $options);
240229

241-
return $operation->execute($server);
230+
$operation->execute($server);
242231
}
243232

244233
/**

src/Collection.php

+9-21
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,13 @@ public function distinct(string $fieldName, array|object $filter = [], array $op
501501
*
502502
* @see DropCollection::__construct() for supported options
503503
* @param array $options Additional options
504-
* @return array|object Command result document
505504
* @throws UnsupportedException if options are not supported by the selected server
506505
* @throws InvalidArgumentException for parameter/option parsing errors
507506
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
508507
*/
509-
public function drop(array $options = []): array|object
508+
public function drop(array $options = []): void
510509
{
511510
$options = $this->inheritWriteOptions($options);
512-
$options = $this->inheritTypeMap($options, __FUNCTION__);
513511

514512
$server = select_server_for_write($this->manager, $options);
515513

@@ -522,7 +520,7 @@ public function drop(array $options = []): array|object
522520
? new DropEncryptedCollection($this->databaseName, $this->collectionName, $options)
523521
: new DropCollection($this->databaseName, $this->collectionName, $options);
524522

525-
return $operation->execute($server);
523+
$operation->execute($server);
526524
}
527525

528526
/**
@@ -531,12 +529,11 @@ public function drop(array $options = []): array|object
531529
* @see DropIndexes::__construct() for supported options
532530
* @param string|IndexInfo $indexName Index name or model object
533531
* @param array $options Additional options
534-
* @return array|object Command result document
535532
* @throws UnsupportedException if options are not supported by the selected server
536533
* @throws InvalidArgumentException for parameter/option parsing errors
537534
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
538535
*/
539-
public function dropIndex(string|IndexInfo $indexName, array $options = []): array|object
536+
public function dropIndex(string|IndexInfo $indexName, array $options = []): void
540537
{
541538
$indexName = (string) $indexName;
542539

@@ -545,31 +542,28 @@ public function dropIndex(string|IndexInfo $indexName, array $options = []): arr
545542
}
546543

547544
$options = $this->inheritWriteOptions($options);
548-
$options = $this->inheritTypeMap($options, __FUNCTION__);
549545

550546
$operation = new DropIndexes($this->databaseName, $this->collectionName, $indexName, $options);
551547

552-
return $operation->execute(select_server_for_write($this->manager, $options));
548+
$operation->execute(select_server_for_write($this->manager, $options));
553549
}
554550

555551
/**
556552
* Drop all indexes in the collection.
557553
*
558554
* @see DropIndexes::__construct() for supported options
559555
* @param array $options Additional options
560-
* @return array|object Command result document
561556
* @throws UnsupportedException if options are not supported by the selected server
562557
* @throws InvalidArgumentException for parameter/option parsing errors
563558
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
564559
*/
565-
public function dropIndexes(array $options = []): array|object
560+
public function dropIndexes(array $options = []): void
566561
{
567562
$options = $this->inheritWriteOptions($options);
568-
$options = $this->inheritTypeMap($options, __FUNCTION__);
569563

570564
$operation = new DropIndexes($this->databaseName, $this->collectionName, '*', $options);
571565

572-
return $operation->execute(select_server_for_write($this->manager, $options));
566+
$operation->execute(select_server_for_write($this->manager, $options));
573567
}
574568

575569
/**
@@ -909,23 +903,21 @@ public function listSearchIndexes(array $options = []): Iterator
909903
* @param string $toCollectionName New name of the collection
910904
* @param string|null $toDatabaseName New database name of the collection. Defaults to the original database.
911905
* @param array $options Additional options
912-
* @return array|object Command result document
913906
* @throws UnsupportedException if options are not supported by the selected server
914907
* @throws InvalidArgumentException for parameter/option parsing errors
915908
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
916909
*/
917-
public function rename(string $toCollectionName, ?string $toDatabaseName = null, array $options = []): array|object
910+
public function rename(string $toCollectionName, ?string $toDatabaseName = null, array $options = []): void
918911
{
919912
if (! isset($toDatabaseName)) {
920913
$toDatabaseName = $this->databaseName;
921914
}
922915

923916
$options = $this->inheritWriteOptions($options);
924-
$options = $this->inheritTypeMap($options);
925917

926918
$operation = new RenameCollection($this->databaseName, $this->collectionName, $toDatabaseName, $toCollectionName, $options);
927919

928-
return $operation->execute(select_server_for_write($this->manager, $options));
920+
$operation->execute(select_server_for_write($this->manager, $options));
929921
}
930922

931923
/**
@@ -1127,12 +1119,8 @@ private function inheritReadPreference(array $options): array
11271119
return $options;
11281120
}
11291121

1130-
private function inheritTypeMap(array $options, ?string $deprecatedFunction = null): array
1122+
private function inheritTypeMap(array $options): array
11311123
{
1132-
if ($deprecatedFunction !== null && isset($options['typeMap'])) {
1133-
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', $deprecatedFunction), E_USER_DEPRECATED);
1134-
}
1135-
11361124
// Only inherit the type map if no codec is used
11371125
if (! isset($options['typeMap']) && ! isset($options['codec'])) {
11381126
$options['typeMap'] = $this->typeMap;

src/Database.php

+12-48
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@
5454
use Throwable;
5555

5656
use function is_array;
57-
use function sprintf;
5857
use function strlen;
59-
use function trigger_error;
60-
61-
use const E_USER_DEPRECATED;
6258

6359
class Database
6460
{
@@ -274,19 +270,12 @@ public function command(array|object $command, array $options = []): CursorInter
274270
* @see CreateCollection::__construct() for supported options
275271
* @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.rst#create-collection-helper
276272
* @see https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/manage-collections/
277-
* @return array|object Command result document
278273
* @throws UnsupportedException if options are not supported by the selected server
279274
* @throws InvalidArgumentException for parameter/option parsing errors
280275
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
281276
*/
282-
public function createCollection(string $collectionName, array $options = []): array|object
277+
public function createCollection(string $collectionName, array $options = []): void
283278
{
284-
if (! isset($options['typeMap'])) {
285-
$options['typeMap'] = $this->typeMap;
286-
} else {
287-
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
288-
}
289-
290279
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
291280
$options['writeConcern'] = $this->writeConcern;
292281
}
@@ -301,7 +290,7 @@ public function createCollection(string $collectionName, array $options = []): a
301290

302291
$server = select_server_for_write($this->manager, $options);
303292

304-
return $operation->execute($server);
293+
$operation->execute($server);
305294
}
306295

307296
/**
@@ -319,19 +308,13 @@ public function createCollection(string $collectionName, array $options = []): a
319308
* getPrevious() and getEncryptedFields() methods, respectively.
320309
*
321310
* @see CreateCollection::__construct() for supported options
322-
* @return array A tuple containing the command result document from creating the collection and the modified "encryptedFields" option
311+
* @return array The modified "encryptedFields" option
323312
* @throws InvalidArgumentException for parameter/option parsing errors
324313
* @throws CreateEncryptedCollectionException for any errors creating data keys or creating the collection
325314
* @throws UnsupportedException if Queryable Encryption is not supported by the selected server
326315
*/
327316
public function createEncryptedCollection(string $collectionName, ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey, array $options): array
328317
{
329-
if (! isset($options['typeMap'])) {
330-
$options['typeMap'] = $this->typeMap;
331-
} else {
332-
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
333-
}
334-
335318
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
336319
$options['writeConcern'] = $this->writeConcern;
337320
}
@@ -340,10 +323,10 @@ public function createEncryptedCollection(string $collectionName, ClientEncrypti
340323
$server = select_server_for_write($this->manager, $options);
341324

342325
try {
343-
$operation->createDataKeys($clientEncryption, $kmsProvider, $masterKey, $encryptedFields);
344-
$result = $operation->execute($server);
326+
$encryptedFields = $operation->createDataKeys($clientEncryption, $kmsProvider, $masterKey);
327+
$operation->execute($server);
345328

346-
return [$result, $encryptedFields];
329+
return $encryptedFields;
347330
} catch (Throwable $e) {
348331
throw new CreateEncryptedCollectionException($e, $encryptedFields ?? []);
349332
}
@@ -354,19 +337,12 @@ public function createEncryptedCollection(string $collectionName, ClientEncrypti
354337
*
355338
* @see DropDatabase::__construct() for supported options
356339
* @param array $options Additional options
357-
* @return array|object Command result document
358340
* @throws UnsupportedException if options are unsupported on the selected server
359341
* @throws InvalidArgumentException for parameter/option parsing errors
360342
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
361343
*/
362-
public function drop(array $options = []): array|object
344+
public function drop(array $options = []): void
363345
{
364-
if (! isset($options['typeMap'])) {
365-
$options['typeMap'] = $this->typeMap;
366-
} else {
367-
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
368-
}
369-
370346
$server = select_server_for_write($this->manager, $options);
371347

372348
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -375,7 +351,7 @@ public function drop(array $options = []): array|object
375351

376352
$operation = new DropDatabase($this->databaseName, $options);
377353

378-
return $operation->execute($server);
354+
$operation->execute($server);
379355
}
380356

381357
/**
@@ -384,19 +360,12 @@ public function drop(array $options = []): array|object
384360
* @see DropCollection::__construct() for supported options
385361
* @param string $collectionName Collection name
386362
* @param array $options Additional options
387-
* @return array|object Command result document
388363
* @throws UnsupportedException if options are unsupported on the selected server
389364
* @throws InvalidArgumentException for parameter/option parsing errors
390365
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
391366
*/
392-
public function dropCollection(string $collectionName, array $options = []): array|object
367+
public function dropCollection(string $collectionName, array $options = []): void
393368
{
394-
if (! isset($options['typeMap'])) {
395-
$options['typeMap'] = $this->typeMap;
396-
} else {
397-
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
398-
}
399-
400369
$server = select_server_for_write($this->manager, $options);
401370

402371
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -412,7 +381,7 @@ public function dropCollection(string $collectionName, array $options = []): arr
412381
? new DropEncryptedCollection($this->databaseName, $collectionName, $options)
413382
: new DropCollection($this->databaseName, $collectionName, $options);
414383

415-
return $operation->execute($server);
384+
$operation->execute($server);
416385
}
417386

418387
/**
@@ -534,21 +503,16 @@ public function modifyCollection(string $collectionName, array $collectionOption
534503
* @param string $toCollectionName New name of the collection
535504
* @param string|null $toDatabaseName New database name of the collection. Defaults to the original database.
536505
* @param array $options Additional options
537-
* @return array|object Command result document
538506
* @throws UnsupportedException if options are unsupported on the selected server
539507
* @throws InvalidArgumentException for parameter/option parsing errors
540508
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
541509
*/
542-
public function renameCollection(string $fromCollectionName, string $toCollectionName, ?string $toDatabaseName = null, array $options = []): array|object
510+
public function renameCollection(string $fromCollectionName, string $toCollectionName, ?string $toDatabaseName = null, array $options = []): void
543511
{
544512
if (! isset($toDatabaseName)) {
545513
$toDatabaseName = $this->databaseName;
546514
}
547515

548-
if (! isset($options['typeMap'])) {
549-
$options['typeMap'] = $this->typeMap;
550-
}
551-
552516
$server = select_server_for_write($this->manager, $options);
553517

554518
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -557,7 +521,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio
557521

558522
$operation = new RenameCollection($this->databaseName, $fromCollectionName, $toDatabaseName, $toCollectionName, $options);
559523

560-
return $operation->execute($server);
524+
$operation->execute($server);
561525
}
562526

563527
/**

0 commit comments

Comments
 (0)