Skip to content

Commit 260c266

Browse files
committed
PHPLIB-1227 Use void return type for operations without meaningful result document
1 parent 3d46eaf commit 260c266

30 files changed

+129
-322
lines changed

UPGRADE-2.0.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ 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`, `modifyCollection`, `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 `CommandSubscriber`.
38+
2639
GridFS
2740
------
2841

psalm-baseline.xml

Lines changed: 0 additions & 18 deletions
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>
@@ -745,19 +733,13 @@
745733
</MixedAssignment>
746734
</file>
747735
<file src="src/Operation/ModifyCollection.php">
748-
<MixedArgument>
749-
<code><![CDATA[$this->options['typeMap']]]></code>
750-
</MixedArgument>
751736
<MixedAssignment>
752737
<code><![CDATA[$cmd['comment']]]></code>
753738
<code><![CDATA[$options['session']]]></code>
754739
<code><![CDATA[$options['writeConcern']]]></code>
755740
</MixedAssignment>
756741
</file>
757742
<file src="src/Operation/RenameCollection.php">
758-
<MixedArgument>
759-
<code><![CDATA[$this->options['typeMap']]]></code>
760-
</MixedArgument>
761743
<MixedAssignment>
762744
<code><![CDATA[$cmd[$option]]]></code>
763745
<code><![CDATA[$options['session']]]></code>

src/Client.php

Lines changed: 2 additions & 13 deletions
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

Lines changed: 9 additions & 21 deletions
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;

0 commit comments

Comments
 (0)