Skip to content

Commit 378073a

Browse files
committed
PHPLIB-1227 Use void return types for operations without meaningful result document
1 parent d8b7877 commit 378073a

32 files changed

+129
-332
lines changed

UPGRADE-2.0.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ UPGRADE FROM 1.x to 2.0
44
* Classes in the namespace `MongoDB\Operation\` are `final`.
55
* All methods in interfaces and classes now define a return type.
66

7+
Operations with no result
8+
-------------------------
9+
10+
The following operations no longer return the raw command result. The return
11+
type changed to `void`. In case of an error, an exception is thrown.
12+
13+
* `MongoDB\Client`: `dropDatabase`
14+
* `MongoDB\Collection`: `drop`, `dropIndex`, `dropIndexes`, `dropSearchIndex`, `rename`
15+
* `MongoDB\Database`: `createCollection`, `drop`, `dropCollection`, `modifyCollection`, `renameCollection`
16+
* `MongoDB\Database::createEncryptedCollection()` return the list of encrypted fields
17+
18+
If you still need to access the raw command result, you can use a `CommandSubscriber`.
19+
720
GridFS
821
------
922

psalm-baseline.xml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,6 @@
579579
</MixedMethodCall>
580580
</file>
581581
<file src="src/Operation/CreateCollection.php">
582-
<MixedArgument>
583-
<code><![CDATA[$this->options['typeMap']]]></code>
584-
</MixedArgument>
585582
<MixedAssignment>
586583
<code><![CDATA[$cmd[$option]]]></code>
587584
<code><![CDATA[$options['session']]]></code>
@@ -653,9 +650,6 @@
653650
</MixedMethodCall>
654651
</file>
655652
<file src="src/Operation/DropCollection.php">
656-
<MixedArgument>
657-
<code><![CDATA[$this->options['typeMap']]]></code>
658-
</MixedArgument>
659653
<MixedAssignment>
660654
<code><![CDATA[$cmd['comment']]]></code>
661655
<code><![CDATA[$options['session']]]></code>
@@ -666,9 +660,6 @@
666660
</MixedMethodCall>
667661
</file>
668662
<file src="src/Operation/DropDatabase.php">
669-
<MixedArgument>
670-
<code><![CDATA[$this->options['typeMap']]]></code>
671-
</MixedArgument>
672663
<MixedAssignment>
673664
<code><![CDATA[$cmd['comment']]]></code>
674665
<code><![CDATA[$options['session']]]></code>
@@ -681,9 +672,6 @@
681672
</MixedArgument>
682673
</file>
683674
<file src="src/Operation/DropIndexes.php">
684-
<MixedArgument>
685-
<code><![CDATA[$this->options['typeMap']]]></code>
686-
</MixedArgument>
687675
<MixedAssignment>
688676
<code><![CDATA[$cmd[$option]]]></code>
689677
<code><![CDATA[$options['session']]]></code>
@@ -862,19 +850,13 @@
862850
</MixedMethodCall>
863851
</file>
864852
<file src="src/Operation/ModifyCollection.php">
865-
<MixedArgument>
866-
<code><![CDATA[$this->options['typeMap']]]></code>
867-
</MixedArgument>
868853
<MixedAssignment>
869854
<code><![CDATA[$cmd['comment']]]></code>
870855
<code><![CDATA[$options['session']]]></code>
871856
<code><![CDATA[$options['writeConcern']]]></code>
872857
</MixedAssignment>
873858
</file>
874859
<file src="src/Operation/RenameCollection.php">
875-
<MixedArgument>
876-
<code><![CDATA[$this->options['typeMap']]]></code>
877-
</MixedArgument>
878860
<MixedAssignment>
879861
<code><![CDATA[$cmd[$option]]]></code>
880862
<code><![CDATA[$options['session']]]></code>

src/Client.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@
5050
use function array_diff_key;
5151
use function is_array;
5252
use function is_string;
53-
use function sprintf;
54-
use function trigger_error;
55-
56-
use const E_USER_DEPRECATED;
5753

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

236225
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -239,7 +228,7 @@ public function dropDatabase(string $databaseName, array $options = []): array|o
239228

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

242-
return $operation->execute($server);
231+
$operation->execute($server);
243232
}
244233

245234
/**

src/Collection.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -508,15 +508,13 @@ public function distinct(string $fieldName, array|object $filter = [], array $op
508508
*
509509
* @see DropCollection::__construct() for supported options
510510
* @param array $options Additional options
511-
* @return array|object Command result document
512511
* @throws UnsupportedException if options are not supported by the selected server
513512
* @throws InvalidArgumentException for parameter/option parsing errors
514513
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
515514
*/
516-
public function drop(array $options = []): array|object
515+
public function drop(array $options = []): void
517516
{
518517
$options = $this->inheritWriteOptions($options);
519-
$options = $this->inheritTypeMap($options, __FUNCTION__);
520518

521519
$server = select_server_for_write($this->manager, $options);
522520

@@ -529,7 +527,7 @@ public function drop(array $options = []): array|object
529527
? new DropEncryptedCollection($this->databaseName, $this->collectionName, $options)
530528
: new DropCollection($this->databaseName, $this->collectionName, $options);
531529

532-
return $operation->execute($server);
530+
$operation->execute($server);
533531
}
534532

535533
/**
@@ -538,12 +536,11 @@ public function drop(array $options = []): array|object
538536
* @see DropIndexes::__construct() for supported options
539537
* @param string|IndexInfo $indexName Index name or model object
540538
* @param array $options Additional options
541-
* @return array|object Command result document
542539
* @throws UnsupportedException if options are not supported by the selected server
543540
* @throws InvalidArgumentException for parameter/option parsing errors
544541
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
545542
*/
546-
public function dropIndex(string|IndexInfo $indexName, array $options = []): array|object
543+
public function dropIndex(string|IndexInfo $indexName, array $options = []): void
547544
{
548545
$indexName = (string) $indexName;
549546

@@ -552,31 +549,28 @@ public function dropIndex(string|IndexInfo $indexName, array $options = []): arr
552549
}
553550

554551
$options = $this->inheritWriteOptions($options);
555-
$options = $this->inheritTypeMap($options, __FUNCTION__);
556552

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

559-
return $operation->execute(select_server_for_write($this->manager, $options));
555+
$operation->execute(select_server_for_write($this->manager, $options));
560556
}
561557

562558
/**
563559
* Drop all indexes in the collection.
564560
*
565561
* @see DropIndexes::__construct() for supported options
566562
* @param array $options Additional options
567-
* @return array|object Command result document
568563
* @throws UnsupportedException if options are not supported by the selected server
569564
* @throws InvalidArgumentException for parameter/option parsing errors
570565
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
571566
*/
572-
public function dropIndexes(array $options = []): array|object
567+
public function dropIndexes(array $options = []): void
573568
{
574569
$options = $this->inheritWriteOptions($options);
575-
$options = $this->inheritTypeMap($options, __FUNCTION__);
576570

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

579-
return $operation->execute(select_server_for_write($this->manager, $options));
573+
$operation->execute(select_server_for_write($this->manager, $options));
580574
}
581575

582576
/**
@@ -958,23 +952,21 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
958952
* @param string $toCollectionName New name of the collection
959953
* @param string|null $toDatabaseName New database name of the collection. Defaults to the original database.
960954
* @param array $options Additional options
961-
* @return array|object Command result document
962955
* @throws UnsupportedException if options are not supported by the selected server
963956
* @throws InvalidArgumentException for parameter/option parsing errors
964957
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
965958
*/
966-
public function rename(string $toCollectionName, ?string $toDatabaseName = null, array $options = []): array|object
959+
public function rename(string $toCollectionName, ?string $toDatabaseName = null, array $options = []): void
967960
{
968961
if (! isset($toDatabaseName)) {
969962
$toDatabaseName = $this->databaseName;
970963
}
971964

972965
$options = $this->inheritWriteOptions($options);
973-
$options = $this->inheritTypeMap($options);
974966

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

977-
return $operation->execute(select_server_for_write($this->manager, $options));
969+
$operation->execute(select_server_for_write($this->manager, $options));
978970
}
979971

980972
/**
@@ -1176,12 +1168,8 @@ private function inheritReadPreference(array $options): array
11761168
return $options;
11771169
}
11781170

1179-
private function inheritTypeMap(array $options, ?string $deprecatedFunction = null): array
1171+
private function inheritTypeMap(array $options): array
11801172
{
1181-
if ($deprecatedFunction !== null && isset($options['typeMap'])) {
1182-
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', $deprecatedFunction), E_USER_DEPRECATED);
1183-
}
1184-
11851173
// Only inherit the type map if no codec is used
11861174
if (! isset($options['typeMap']) && ! isset($options['codec'])) {
11871175
$options['typeMap'] = $this->typeMap;

0 commit comments

Comments
 (0)