Skip to content

Commit

Permalink
PHPLIB-1227 Use void return types for operations without meaningful r…
Browse files Browse the repository at this point in the history
…esult document
  • Loading branch information
GromNaN committed Sep 26, 2024
1 parent f584124 commit 6f2085c
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 223 deletions.
18 changes: 0 additions & 18 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,6 @@
</MixedMethodCall>
</file>
<file src="src/Operation/CreateCollection.php">
<MixedArgument>
<code><![CDATA[$this->options['typeMap']]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$cmd[$option]]]></code>
<code><![CDATA[$options['session']]]></code>
Expand Down Expand Up @@ -653,9 +650,6 @@
</MixedMethodCall>
</file>
<file src="src/Operation/DropCollection.php">
<MixedArgument>
<code><![CDATA[$this->options['typeMap']]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$cmd['comment']]]></code>
<code><![CDATA[$options['session']]]></code>
Expand All @@ -666,9 +660,6 @@
</MixedMethodCall>
</file>
<file src="src/Operation/DropDatabase.php">
<MixedArgument>
<code><![CDATA[$this->options['typeMap']]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$cmd['comment']]]></code>
<code><![CDATA[$options['session']]]></code>
Expand All @@ -681,9 +672,6 @@
</MixedArgument>
</file>
<file src="src/Operation/DropIndexes.php">
<MixedArgument>
<code><![CDATA[$this->options['typeMap']]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$cmd[$option]]]></code>
<code><![CDATA[$options['session']]]></code>
Expand Down Expand Up @@ -862,19 +850,13 @@
</MixedMethodCall>
</file>
<file src="src/Operation/ModifyCollection.php">
<MixedArgument>
<code><![CDATA[$this->options['typeMap']]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$cmd['comment']]]></code>
<code><![CDATA[$options['session']]]></code>
<code><![CDATA[$options['writeConcern']]]></code>
</MixedAssignment>
</file>
<file src="src/Operation/RenameCollection.php">
<MixedArgument>
<code><![CDATA[$this->options['typeMap']]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$cmd[$option]]]></code>
<code><![CDATA[$options['session']]]></code>
Expand Down
9 changes: 2 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,12 @@ public function createClientEncryption(array $options): ClientEncryption
* @see DropDatabase::__construct() for supported options
* @param string $databaseName Database name
* @param array $options Additional options
* @return array|object Command result document
* @throws UnsupportedException if options are unsupported on the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function dropDatabase(string $databaseName, array $options = []): array|object
public function dropDatabase(string $databaseName, array $options = []): void
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}

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

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

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

return $operation->execute($server);
$operation->execute($server);
}

/**
Expand Down
24 changes: 8 additions & 16 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,13 @@ public function distinct(string $fieldName, array|object $filter = [], array $op
*
* @see DropCollection::__construct() for supported options
* @param array $options Additional options
* @return array|object Command result document
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function drop(array $options = []): array|object
public function drop(array $options = []): void
{
$options = $this->inheritWriteOptions($options);
$options = $this->inheritTypeMap($options);

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

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

return $operation->execute($server);
$operation->execute($server);
}

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

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

$options = $this->inheritWriteOptions($options);
$options = $this->inheritTypeMap($options);

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

return $operation->execute(select_server_for_write($this->manager, $options));
$operation->execute(select_server_for_write($this->manager, $options));
}

/**
* Drop all indexes in the collection.
*
* @see DropIndexes::__construct() for supported options
* @param array $options Additional options
* @return array|object Command result document
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function dropIndexes(array $options = []): array|object
public function dropIndexes(array $options = []): void
{
$options = $this->inheritWriteOptions($options);
$options = $this->inheritTypeMap($options);

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

return $operation->execute(select_server_for_write($this->manager, $options));
$operation->execute(select_server_for_write($this->manager, $options));
}

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

$options = $this->inheritWriteOptions($options);
$options = $this->inheritTypeMap($options);

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

return $operation->execute(select_server_for_write($this->manager, $options));
$operation->execute(select_server_for_write($this->manager, $options));
}

/**
Expand Down
56 changes: 15 additions & 41 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,12 @@ public function command(array|object $command, array $options = []): CursorInter
* @see CreateCollection::__construct() for supported options
* @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.rst#create-collection-helper
* @see https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/manage-collections/
* @return array|object Command result document
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function createCollection(string $collectionName, array $options = []): array|object
public function createCollection(string $collectionName, array $options = []): void
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}

if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
$options['writeConcern'] = $this->writeConcern;
}
Expand All @@ -296,7 +291,7 @@ public function createCollection(string $collectionName, array $options = []): a

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

return $operation->execute($server);
$operation->execute($server);
}

/**
Expand All @@ -314,17 +309,13 @@ public function createCollection(string $collectionName, array $options = []): a
* getPrevious() and getEncryptedFields() methods, respectively.
*
* @see CreateCollection::__construct() for supported options
* @return array A tuple containing the command result document from creating the collection and the modified "encryptedFields" option
* @return array The modified "encryptedFields" option
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws CreateEncryptedCollectionException for any errors creating data keys or creating the collection
* @throws UnsupportedException if Queryable Encryption is not supported by the selected server
*/
public function createEncryptedCollection(string $collectionName, ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey, array $options): array
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}

if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
$options['writeConcern'] = $this->writeConcern;
}
Expand All @@ -334,9 +325,11 @@ public function createEncryptedCollection(string $collectionName, ClientEncrypti

try {
$operation->createDataKeys($clientEncryption, $kmsProvider, $masterKey, $encryptedFields);
$result = $operation->execute($server);
$operation->execute($server);

assert(is_array($encryptedFields), '$encryptedFields is set');

Check failure on line 330 in src/Database.php

View workflow job for this annotation

GitHub Actions / phpcs

Function assert() should not be referenced via a fallback global name, but via a use statement.

return [$result, $encryptedFields];
return $encryptedFields;
} catch (Throwable $e) {
throw new CreateEncryptedCollectionException($e, $encryptedFields ?? []);
}
Expand All @@ -347,17 +340,12 @@ public function createEncryptedCollection(string $collectionName, ClientEncrypti
*
* @see DropDatabase::__construct() for supported options
* @param array $options Additional options
* @return array|object Command result document
* @throws UnsupportedException if options are unsupported on the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function drop(array $options = []): array|object
public function drop(array $options = []): void
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}

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

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

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

return $operation->execute($server);
$operation->execute($server);
}

/**
Expand All @@ -375,17 +363,12 @@ public function drop(array $options = []): array|object
* @see DropCollection::__construct() for supported options
* @param string $collectionName Collection name
* @param array $options Additional options
* @return array|object Command result document
* @throws UnsupportedException if options are unsupported on the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function dropCollection(string $collectionName, array $options = []): array|object
public function dropCollection(string $collectionName, array $options = []): void
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}

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

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

return $operation->execute($server);
$operation->execute($server);
}

/**
Expand Down Expand Up @@ -497,12 +480,8 @@ public function listCollections(array $options = []): CollectionInfoIterator
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function modifyCollection(string $collectionName, array $collectionOptions, array $options = []): array|object
public function modifyCollection(string $collectionName, array $collectionOptions, array $options = []): void
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}

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

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

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

return $operation->execute($server);
$operation->execute($server);
}

/**
Expand All @@ -522,21 +501,16 @@ public function modifyCollection(string $collectionName, array $collectionOption
* @param string $toCollectionName New name of the collection
* @param string|null $toDatabaseName New database name of the collection. Defaults to the original database.
* @param array $options Additional options
* @return array|object Command result document
* @throws UnsupportedException if options are unsupported on the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function renameCollection(string $fromCollectionName, string $toCollectionName, ?string $toDatabaseName = null, array $options = []): array|object
public function renameCollection(string $fromCollectionName, string $toCollectionName, ?string $toDatabaseName = null, array $options = []): void
{
if (! isset($toDatabaseName)) {
$toDatabaseName = $this->databaseName;
}

if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
}

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

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

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

return $operation->execute($server);
$operation->execute($server);
}

/**
Expand Down
Loading

0 comments on commit 6f2085c

Please sign in to comment.