Skip to content

Commit

Permalink
Deprecate typeMap on operations without meaningful result (#1473)
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN authored Sep 30, 2024
1 parent 0527fa4 commit 8d9f5b5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
use function array_diff_key;
use function is_array;
use function is_string;
use function sprintf;
use function trigger_error;

use const E_USER_DEPRECATED;

class Client
{
Expand Down Expand Up @@ -228,6 +232,8 @@ public function dropDatabase(string $databaseName, array $options = [])
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
} else {
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
}

$server = select_server_for_write($this->manager, $options);
Expand Down
12 changes: 8 additions & 4 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public function distinct(string $fieldName, array|object $filter = [], array $op
public function drop(array $options = [])
{
$options = $this->inheritWriteOptions($options);
$options = $this->inheritTypeMap($options);
$options = $this->inheritTypeMap($options, __FUNCTION__);

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

Expand Down Expand Up @@ -560,7 +560,7 @@ public function dropIndex(string|IndexInfo $indexName, array $options = [])
}

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

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

Expand All @@ -580,7 +580,7 @@ public function dropIndex(string|IndexInfo $indexName, array $options = [])
public function dropIndexes(array $options = [])
{
$options = $this->inheritWriteOptions($options);
$options = $this->inheritTypeMap($options);
$options = $this->inheritTypeMap($options, __FUNCTION__);

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

Expand Down Expand Up @@ -1212,8 +1212,12 @@ private function inheritReadPreference(array $options): array
return $options;
}

private function inheritTypeMap(array $options): array
private function inheritTypeMap(array $options, ?string $deprecatedFunction = null): array
{
if ($deprecatedFunction !== null && isset($options['typeMap'])) {
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', $deprecatedFunction), E_USER_DEPRECATED);
}

// Only inherit the type map if no codec is used
if (! isset($options['typeMap']) && ! isset($options['codec'])) {
$options['typeMap'] = $this->typeMap;
Expand Down
12 changes: 12 additions & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@
use Traversable;

use function is_array;
use function sprintf;
use function strlen;
use function trigger_error;

use const E_USER_DEPRECATED;

class Database
{
Expand Down Expand Up @@ -286,6 +290,8 @@ public function createCollection(string $collectionName, array $options = [])
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
} else {
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
}

if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
Expand Down Expand Up @@ -329,6 +335,8 @@ public function createEncryptedCollection(string $collectionName, ClientEncrypti
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
} else {
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
}

if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
Expand Down Expand Up @@ -362,6 +370,8 @@ public function drop(array $options = [])
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
} else {
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
}

$server = select_server_for_write($this->manager, $options);
Expand Down Expand Up @@ -390,6 +400,8 @@ public function dropCollection(string $collectionName, array $options = [])
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
} else {
@trigger_error(sprintf('The function %s() will return nothing in mongodb/mongodb v2.0, the "typeMap" option is deprecated', __FUNCTION__), E_USER_DEPRECATED);
}

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

0 comments on commit 8d9f5b5

Please sign in to comment.