Skip to content

Commit 244030e

Browse files
committed
PHPLIB-1227 Use void return types for operations without meaningful result document
1 parent f584124 commit 244030e

21 files changed

+130
-195
lines changed

src/Collection.php

Lines changed: 8 additions & 15 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);
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);
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);
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
/**
@@ -963,18 +957,17 @@ public function mapReduce(JavascriptInterface $map, JavascriptInterface $reduce,
963957
* @throws InvalidArgumentException for parameter/option parsing errors
964958
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
965959
*/
966-
public function rename(string $toCollectionName, ?string $toDatabaseName = null, array $options = []): array|object
960+
public function rename(string $toCollectionName, ?string $toDatabaseName = null, array $options = []): void
967961
{
968962
if (! isset($toDatabaseName)) {
969963
$toDatabaseName = $this->databaseName;
970964
}
971965

972966
$options = $this->inheritWriteOptions($options);
973-
$options = $this->inheritTypeMap($options);
974967

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

977-
return $operation->execute(select_server_for_write($this->manager, $options));
970+
$operation->execute(select_server_for_write($this->manager, $options));
978971
}
979972

980973
/**

src/Database.php

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,8 @@ public function command(array|object $command, array $options = []): CursorInter
276276
* @throws InvalidArgumentException for parameter/option parsing errors
277277
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
278278
*/
279-
public function createCollection(string $collectionName, array $options = []): array|object
279+
public function createCollection(string $collectionName, array $options = []): void
280280
{
281-
if (! isset($options['typeMap'])) {
282-
$options['typeMap'] = $this->typeMap;
283-
}
284-
285281
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
286282
$options['writeConcern'] = $this->writeConcern;
287283
}
@@ -296,7 +292,7 @@ public function createCollection(string $collectionName, array $options = []): a
296292

297293
$server = select_server_for_write($this->manager, $options);
298294

299-
return $operation->execute($server);
295+
$operation->execute($server);
300296
}
301297

302298
/**
@@ -314,17 +310,13 @@ public function createCollection(string $collectionName, array $options = []): a
314310
* getPrevious() and getEncryptedFields() methods, respectively.
315311
*
316312
* @see CreateCollection::__construct() for supported options
317-
* @return array A tuple containing the command result document from creating the collection and the modified "encryptedFields" option
313+
* @return array The modified "encryptedFields" option
318314
* @throws InvalidArgumentException for parameter/option parsing errors
319315
* @throws CreateEncryptedCollectionException for any errors creating data keys or creating the collection
320316
* @throws UnsupportedException if Queryable Encryption is not supported by the selected server
321317
*/
322318
public function createEncryptedCollection(string $collectionName, ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey, array $options): array
323319
{
324-
if (! isset($options['typeMap'])) {
325-
$options['typeMap'] = $this->typeMap;
326-
}
327-
328320
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
329321
$options['writeConcern'] = $this->writeConcern;
330322
}
@@ -334,9 +326,9 @@ public function createEncryptedCollection(string $collectionName, ClientEncrypti
334326

335327
try {
336328
$operation->createDataKeys($clientEncryption, $kmsProvider, $masterKey, $encryptedFields);
337-
$result = $operation->execute($server);
329+
$operation->execute($server);
338330

339-
return [$result, $encryptedFields];
331+
return $encryptedFields;
340332
} catch (Throwable $e) {
341333
throw new CreateEncryptedCollectionException($e, $encryptedFields ?? []);
342334
}
@@ -347,17 +339,12 @@ public function createEncryptedCollection(string $collectionName, ClientEncrypti
347339
*
348340
* @see DropDatabase::__construct() for supported options
349341
* @param array $options Additional options
350-
* @return array|object Command result document
351342
* @throws UnsupportedException if options are unsupported on the selected server
352343
* @throws InvalidArgumentException for parameter/option parsing errors
353344
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
354345
*/
355-
public function drop(array $options = []): array|object
346+
public function drop(array $options = []): void
356347
{
357-
if (! isset($options['typeMap'])) {
358-
$options['typeMap'] = $this->typeMap;
359-
}
360-
361348
$server = select_server_for_write($this->manager, $options);
362349

363350
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -366,7 +353,7 @@ public function drop(array $options = []): array|object
366353

367354
$operation = new DropDatabase($this->databaseName, $options);
368355

369-
return $operation->execute($server);
356+
$operation->execute($server);
370357
}
371358

372359
/**
@@ -375,17 +362,12 @@ public function drop(array $options = []): array|object
375362
* @see DropCollection::__construct() for supported options
376363
* @param string $collectionName Collection name
377364
* @param array $options Additional options
378-
* @return array|object Command result document
379365
* @throws UnsupportedException if options are unsupported on the selected server
380366
* @throws InvalidArgumentException for parameter/option parsing errors
381367
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
382368
*/
383-
public function dropCollection(string $collectionName, array $options = []): array|object
369+
public function dropCollection(string $collectionName, array $options = []): void
384370
{
385-
if (! isset($options['typeMap'])) {
386-
$options['typeMap'] = $this->typeMap;
387-
}
388-
389371
$server = select_server_for_write($this->manager, $options);
390372

391373
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -401,7 +383,7 @@ public function dropCollection(string $collectionName, array $options = []): arr
401383
? new DropEncryptedCollection($this->databaseName, $collectionName, $options)
402384
: new DropCollection($this->databaseName, $collectionName, $options);
403385

404-
return $operation->execute($server);
386+
$operation->execute($server);
405387
}
406388

407389
/**
@@ -497,12 +479,8 @@ public function listCollections(array $options = []): CollectionInfoIterator
497479
* @throws InvalidArgumentException for parameter/option parsing errors
498480
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
499481
*/
500-
public function modifyCollection(string $collectionName, array $collectionOptions, array $options = []): array|object
482+
public function modifyCollection(string $collectionName, array $collectionOptions, array $options = []): void
501483
{
502-
if (! isset($options['typeMap'])) {
503-
$options['typeMap'] = $this->typeMap;
504-
}
505-
506484
$server = select_server_for_write($this->manager, $options);
507485

508486
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -511,7 +489,7 @@ public function modifyCollection(string $collectionName, array $collectionOption
511489

512490
$operation = new ModifyCollection($this->databaseName, $collectionName, $collectionOptions, $options);
513491

514-
return $operation->execute($server);
492+
$operation->execute($server);
515493
}
516494

517495
/**
@@ -527,16 +505,12 @@ public function modifyCollection(string $collectionName, array $collectionOption
527505
* @throws InvalidArgumentException for parameter/option parsing errors
528506
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
529507
*/
530-
public function renameCollection(string $fromCollectionName, string $toCollectionName, ?string $toDatabaseName = null, array $options = []): array|object
508+
public function renameCollection(string $fromCollectionName, string $toCollectionName, ?string $toDatabaseName = null, array $options = []): void
531509
{
532510
if (! isset($toDatabaseName)) {
533511
$toDatabaseName = $this->databaseName;
534512
}
535513

536-
if (! isset($options['typeMap'])) {
537-
$options['typeMap'] = $this->typeMap;
538-
}
539-
540514
$server = select_server_for_write($this->manager, $options);
541515

542516
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -545,7 +519,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio
545519

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

548-
return $operation->execute($server);
522+
$operation->execute($server);
549523
}
550524

551525
/**

src/Operation/CreateCollection.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use MongoDB\Driver\WriteConcern;
2525
use MongoDB\Exception\InvalidArgumentException;
2626

27-
use function current;
2827
use function is_array;
2928
use function is_bool;
3029
use function is_integer;
@@ -112,9 +111,6 @@ final class CreateCollection implements Executable
112111
*
113112
* This is not supported for servers versions < 5.0.
114113
*
115-
* * typeMap (array): Type map for BSON deserialization. This will only be
116-
* used for the returned command result document.
117-
*
118114
* * validationAction (string): Validation action.
119115
*
120116
* * validationLevel (string): Validation level.
@@ -239,19 +235,11 @@ public function __construct(private string $databaseName, private string $collec
239235
/**
240236
* Execute the operation.
241237
*
242-
* @see Executable::execute()
243-
* @return array|object Command result document
244238
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
245239
*/
246-
public function execute(Server $server): array|object
240+
public function execute(Server $server): void
247241
{
248-
$cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
249-
250-
if (isset($this->options['typeMap'])) {
251-
$cursor->setTypeMap($this->options['typeMap']);
252-
}
253-
254-
return current($cursor->toArray());
242+
$server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
255243
}
256244

257245
/**

src/Operation/CreateEncryptedCollection.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,10 @@ public function createDataKeys(ClientEncryption $clientEncryption, string $kmsPr
155155
}
156156

157157
/**
158-
* @see Executable::execute()
159-
* @return array|object Command result document from creating the encrypted collection
160158
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
161159
* @throws UnsupportedException if the server does not support Queryable Encryption
162160
*/
163-
public function execute(Server $server): array|object
161+
public function execute(Server $server): void
164162
{
165163
if (! server_supports_feature($server, self::WIRE_VERSION_FOR_QUERYABLE_ENCRYPTION_V2)) {
166164
throw new UnsupportedException('Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption.');
@@ -170,10 +168,8 @@ public function execute(Server $server): array|object
170168
$createMetadataCollection->execute($server);
171169
}
172170

173-
$result = $this->createCollection->execute($server);
171+
$this->createCollection->execute($server);
174172

175173
$this->createSafeContentIndex->execute($server);
176-
177-
return $result;
178174
}
179175
}

src/Operation/DropCollection.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use MongoDB\Exception\InvalidArgumentException;
2727
use MongoDB\Exception\UnsupportedException;
2828

29-
use function current;
3029
use function is_array;
3130

3231
/**
@@ -83,36 +82,28 @@ public function __construct(private string $databaseName, private string $collec
8382
/**
8483
* Execute the operation.
8584
*
86-
* @see Executable::execute()
87-
* @return array|object Command result document
8885
* @throws UnsupportedException if write concern is used and unsupported
8986
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
9087
*/
91-
public function execute(Server $server): array|object
88+
public function execute(Server $server): void
9289
{
9390
$inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();
9491
if ($inTransaction && isset($this->options['writeConcern'])) {
9592
throw UnsupportedException::writeConcernNotSupportedInTransaction();
9693
}
9794

9895
try {
99-
$cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
96+
$server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
10097
} catch (CommandException $e) {
10198
/* The server may return an error if the collection does not exist.
10299
* Check for an error code and return the command reply instead of
103100
* throwing. */
104101
if ($e->getCode() === self::ERROR_CODE_NAMESPACE_NOT_FOUND) {
105-
return $e->getResultDocument();
102+
return;
106103
}
107104

108105
throw $e;
109106
}
110-
111-
if (isset($this->options['typeMap'])) {
112-
$cursor->setTypeMap($this->options['typeMap']);
113-
}
114-
115-
return current($cursor->toArray());
116107
}
117108

118109
/**

src/Operation/DropDatabase.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use MongoDB\Driver\WriteConcern;
2525
use MongoDB\Exception\InvalidArgumentException;
2626

27-
use function current;
2827
use function is_array;
2928

3029
/**
@@ -78,19 +77,11 @@ public function __construct(private string $databaseName, private array $options
7877
/**
7978
* Execute the operation.
8079
*
81-
* @see Executable::execute()
82-
* @return array|object Command result document
8380
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
8481
*/
85-
public function execute(Server $server): array|object
82+
public function execute(Server $server): void
8683
{
87-
$cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
88-
89-
if (isset($this->options['typeMap'])) {
90-
$cursor->setTypeMap($this->options['typeMap']);
91-
}
92-
93-
return current($cursor->toArray());
84+
$server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
9485
}
9586

9687
/**

0 commit comments

Comments
 (0)