diff --git a/.evergreen/config/generated/build/build-extension.yml b/.evergreen/config/generated/build/build-extension.yml
index c16586063..02033b002 100644
--- a/.evergreen/config/generated/build/build-extension.yml
+++ b/.evergreen/config/generated/build/build-extension.yml
@@ -7,11 +7,11 @@ tasks:
vars:
PHP_VERSION: "8.3"
- func: "compile extension"
- # TODO: remove once 1.20.0 is released
+ # TODO: remove once 2.0.0 is released
vars:
- EXTENSION_BRANCH: "v1.20"
+ EXTENSION_BRANCH: "v2.x"
- func: "upload extension"
- # TODO: re-enable once 1.20.0 is released
+ # TODO: re-enable once 2.0.0 is released
# - name: "build-php-8.3-lowest"
# tags: ["build", "php8.3", "lowest", "pr", "tag"]
# commands:
@@ -20,7 +20,7 @@ tasks:
# PHP_VERSION: "8.3"
# - func: "compile extension"
# vars:
-# EXTENSION_VERSION: "1.20.0"
+# EXTENSION_VERSION: "2.0.0"
# - func: "upload extension"
# - name: "build-php-8.3-next-stable"
# tags: ["build", "php8.3", "next-stable", "pr", "tag"]
@@ -30,7 +30,7 @@ tasks:
# PHP_VERSION: "8.3"
# - func: "compile extension"
# vars:
-# EXTENSION_BRANCH: "v1.20.0"
+# EXTENSION_BRANCH: "v2.0"
# - func: "upload extension"
# - name: "build-php-8.3-next-minor"
# tags: ["build", "php8.3", "next-minor"]
@@ -40,7 +40,7 @@ tasks:
# PHP_VERSION: "8.3"
# - func: "compile extension"
# vars:
-# EXTENSION_BRANCH: "master"
+# EXTENSION_BRANCH: "v2.x"
# - func: "upload extension"
- name: "build-php-8.2"
tags: ["build", "php8.2", "stable", "pr", "tag"]
@@ -49,11 +49,11 @@ tasks:
vars:
PHP_VERSION: "8.2"
- func: "compile extension"
- # TODO: remove once 1.20.0 is released
+ # TODO: remove once 2.0.0 is released
vars:
- EXTENSION_BRANCH: "v1.20"
+ EXTENSION_BRANCH: "v2.x"
- func: "upload extension"
- # TODO: re-enable once 1.20.0 is released
+ # TODO: re-enable once 2.0.0 is released
# - name: "build-php-8.2-lowest"
# tags: ["build", "php8.2", "lowest", "pr", "tag"]
# commands:
@@ -62,7 +62,7 @@ tasks:
# PHP_VERSION: "8.2"
# - func: "compile extension"
# vars:
-# EXTENSION_VERSION: "1.20.0"
+# EXTENSION_VERSION: "2.0.0"
# - func: "upload extension"
# - name: "build-php-8.2-next-stable"
# tags: ["build", "php8.2", "next-stable", "pr", "tag"]
@@ -72,7 +72,7 @@ tasks:
# PHP_VERSION: "8.2"
# - func: "compile extension"
# vars:
-# EXTENSION_BRANCH: "v1.20.0"
+# EXTENSION_BRANCH: "v2.0"
# - func: "upload extension"
# - name: "build-php-8.2-next-minor"
# tags: ["build", "php8.2", "next-minor"]
@@ -82,7 +82,7 @@ tasks:
# PHP_VERSION: "8.2"
# - func: "compile extension"
# vars:
-# EXTENSION_BRANCH: "master"
+# EXTENSION_BRANCH: "v2.x"
# - func: "upload extension"
- name: "build-php-8.1"
tags: ["build", "php8.1", "stable", "pr", "tag"]
@@ -91,11 +91,11 @@ tasks:
vars:
PHP_VERSION: "8.1"
- func: "compile extension"
- # TODO: remove once 1.20.0 is released
+ # TODO: remove once 2.0.0 is released
vars:
- EXTENSION_BRANCH: "v1.20"
+ EXTENSION_BRANCH: "v2.x"
- func: "upload extension"
- # TODO: re-enable once 1.20.0 is released
+ # TODO: re-enable once 2.0.0 is released
# - name: "build-php-8.1-lowest"
# tags: ["build", "php8.1", "lowest", "pr", "tag"]
# commands:
@@ -104,7 +104,7 @@ tasks:
# PHP_VERSION: "8.1"
# - func: "compile extension"
# vars:
-# EXTENSION_VERSION: "1.20.0"
+# EXTENSION_VERSION: "2.0.0"
# - func: "upload extension"
# - name: "build-php-8.1-next-stable"
# tags: ["build", "php8.1", "next-stable", "pr", "tag"]
@@ -114,7 +114,7 @@ tasks:
# PHP_VERSION: "8.1"
# - func: "compile extension"
# vars:
-# EXTENSION_BRANCH: "v1.20.0"
+# EXTENSION_BRANCH: "v2.0"
# - func: "upload extension"
# - name: "build-php-8.1-next-minor"
# tags: ["build", "php8.1", "next-minor"]
@@ -124,5 +124,5 @@ tasks:
# PHP_VERSION: "8.1"
# - func: "compile extension"
# vars:
-# EXTENSION_BRANCH: "master"
+# EXTENSION_BRANCH: "v2.x"
# - func: "upload extension"
diff --git a/.evergreen/config/templates/build/build-extension.yml b/.evergreen/config/templates/build/build-extension.yml
index ecd1fffdd..1599967ab 100644
--- a/.evergreen/config/templates/build/build-extension.yml
+++ b/.evergreen/config/templates/build/build-extension.yml
@@ -5,11 +5,11 @@
vars:
PHP_VERSION: "%phpVersion%"
- func: "compile extension"
- # TODO: remove once 1.20.0 is released
+ # TODO: remove once 2.0.0 is released
vars:
- EXTENSION_BRANCH: "v1.20"
+ EXTENSION_BRANCH: "v2.x"
- func: "upload extension"
- # TODO: re-enable once 1.20.0 is released
+ # TODO: re-enable once 2.0.0 is released
# - name: "build-php-%phpVersion%-lowest"
# tags: ["build", "php%phpVersion%", "lowest", "pr", "tag"]
# commands:
@@ -18,7 +18,7 @@
# PHP_VERSION: "%phpVersion%"
# - func: "compile extension"
# vars:
-# EXTENSION_VERSION: "1.20.0"
+# EXTENSION_VERSION: "2.0.0"
# - func: "upload extension"
# - name: "build-php-%phpVersion%-next-stable"
# tags: ["build", "php%phpVersion%", "next-stable", "pr", "tag"]
@@ -28,7 +28,7 @@
# PHP_VERSION: "%phpVersion%"
# - func: "compile extension"
# vars:
-# EXTENSION_BRANCH: "v1.20.0"
+# EXTENSION_BRANCH: "v2.0"
# - func: "upload extension"
# - name: "build-php-%phpVersion%-next-minor"
# tags: ["build", "php%phpVersion%", "next-minor"]
@@ -38,5 +38,5 @@
# PHP_VERSION: "%phpVersion%"
# - func: "compile extension"
# vars:
-# EXTENSION_BRANCH: "master"
+# EXTENSION_BRANCH: "v2.x"
# - func: "upload extension"
diff --git a/.github/workflows/coding-standards.yml b/.github/workflows/coding-standards.yml
index 5dbb7dd71..b03f7779f 100644
--- a/.github/workflows/coding-standards.yml
+++ b/.github/workflows/coding-standards.yml
@@ -13,9 +13,9 @@ on:
env:
PHP_VERSION: "8.2"
- # TODO: change to "stable" once 1.20.0 is released
+ # TODO: change to "stable" once 2.0.0 is released
# DRIVER_VERSION: "stable"
- DRIVER_VERSION: "mongodb/mongo-php-driver@v1.20"
+ DRIVER_VERSION: "mongodb/mongo-php-driver@v2.x"
jobs:
phpcs:
diff --git a/.github/workflows/generator.yml b/.github/workflows/generator.yml
index 2427a41eb..711befabd 100644
--- a/.github/workflows/generator.yml
+++ b/.github/workflows/generator.yml
@@ -13,9 +13,9 @@ on:
env:
PHP_VERSION: "8.2"
- # TODO: change to "stable" once 1.20.0 is released
+ # TODO: change to "stable" once 2.0.0 is released
# DRIVER_VERSION: "stable"
- DRIVER_VERSION: "mongodb/mongo-php-driver@v1.20"
+ DRIVER_VERSION: "mongodb/mongo-php-driver@v2.x"
jobs:
psalm:
diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml
index ebb60cabf..82919098f 100644
--- a/.github/workflows/static-analysis.yml
+++ b/.github/workflows/static-analysis.yml
@@ -19,9 +19,9 @@ on:
env:
PHP_VERSION: "8.2"
- # TODO: change to "stable" once 1.20.0 is released
+ # TODO: change to "stable" once 2.0.0 is released
# DRIVER_VERSION: "stable"
- DRIVER_VERSION: "mongodb/mongo-php-driver@v1.20"
+ DRIVER_VERSION: "mongodb/mongo-php-driver@v2.x"
jobs:
psalm:
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 803eb476d..29d36194f 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -12,9 +12,9 @@ on:
- "feature/*"
env:
- # TODO: change to "stable" once 1.20.0 is released
+ # TODO: change to "stable" once 2.0.0 is released
# DRIVER_VERSION: "stable"
- DRIVER_VERSION: "mongodb/mongo-php-driver@v1.20"
+ DRIVER_VERSION: "mongodb/mongo-php-driver@v2.x"
jobs:
phpunit:
diff --git a/UPGRADE-2.0.md b/UPGRADE-2.0.md
index 2c131e24c..8dabbdd59 100644
--- a/UPGRADE-2.0.md
+++ b/UPGRADE-2.0.md
@@ -2,6 +2,7 @@ UPGRADE FROM 1.x to 2.0
========================
* Classes in the namespace `MongoDB\Operation\` are `final`.
+ * All methods in interfaces and classes now define a return type.
GridFS
------
diff --git a/composer.json b/composer.json
index d9d0516b4..95b96ddb8 100644
--- a/composer.json
+++ b/composer.json
@@ -12,7 +12,7 @@
"require": {
"php": "^8.1",
"ext-json": "*",
- "ext-mongodb": "^1.20.0",
+ "ext-mongodb": "^2.0",
"composer-runtime-api": "^2.0",
"psr/log": "^1.1.4|^2|^3",
"symfony/polyfill-php80": "^1.27",
diff --git a/phpcs.xml.dist b/phpcs.xml.dist
index de542ac64..3c2b9441b 100644
--- a/phpcs.xml.dist
+++ b/phpcs.xml.dist
@@ -133,14 +133,6 @@
-
-
-
-
- src
-
-
-
diff --git a/psalm-baseline.xml b/psalm-baseline.xml
index 039b43007..5ea414eb3 100644
--- a/psalm-baseline.xml
+++ b/psalm-baseline.xml
@@ -5,11 +5,7 @@
name]]>
-
-
-
-
@@ -23,6 +19,9 @@
+
+
+
@@ -184,11 +183,14 @@
-
-
-
+
+
+
+
+ ]]>
+
@@ -196,6 +198,9 @@
+
+ ]]>
+
@@ -227,10 +232,19 @@
+
+
+
+
+ ]]>
+
+
+ ]]>
+
@@ -257,10 +271,19 @@
+
+
+
+
+ ]]>
+
+
+ ]]>
+
@@ -324,6 +347,7 @@
getArrayCopy()]]>
+ getArrayCopy()]]>
@@ -331,6 +355,7 @@
+
@@ -339,6 +364,15 @@
+
+
+
+
+ ]]>
+
+
+ ]]>
+
cursor->nextBatch]]>
@@ -354,11 +388,6 @@
current()]]>
-
-
-
-
-
@@ -724,18 +753,6 @@
value ?? null) : null]]>
-
-
-
-
-
-
-
-
-
-
-
-
@@ -786,14 +803,26 @@
-
- listCollections->execute($server), $this->databaseName)]]>
-
+
+
+
+
+ ]]>
+
+
+ ]]>
+
-
- listDatabases->execute($server))]]>
-
+
+
+
+
+ ]]>
+
+
+ ]]>
+
@@ -801,10 +830,19 @@
databaseName . '.' . $this->collectionName)]]>
+
+
+
+
+ ]]>
+
+
+ ]]>
+
diff --git a/psalm.xml.dist b/psalm.xml.dist
index 83d825d51..d9640583b 100644
--- a/psalm.xml.dist
+++ b/psalm.xml.dist
@@ -21,6 +21,8 @@
+
+
diff --git a/src/BulkWriteResult.php b/src/BulkWriteResult.php
index 4fb556e0b..548acc9a7 100644
--- a/src/BulkWriteResult.php
+++ b/src/BulkWriteResult.php
@@ -38,10 +38,9 @@ public function __construct(private WriteResult $writeResult, private array $ins
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
- * @return integer|null
* @throws BadMethodCallException if the write result is unacknowledged
*/
- public function getDeletedCount()
+ public function getDeletedCount(): ?int
{
if ($this->isAcknowledged) {
return $this->writeResult->getDeletedCount();
@@ -56,10 +55,9 @@ public function getDeletedCount()
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
- * @return integer|null
* @throws BadMethodCallException if the write result is unacknowledged
*/
- public function getInsertedCount()
+ public function getInsertedCount(): ?int
{
if ($this->isAcknowledged) {
return $this->writeResult->getInsertedCount();
@@ -76,10 +74,8 @@ public function getInsertedCount()
* the driver did not generate an ID), the index will contain its "_id"
* field value. Any driver-generated ID will be a MongoDB\BSON\ObjectId
* instance.
- *
- * @return array
*/
- public function getInsertedIds()
+ public function getInsertedIds(): array
{
return $this->insertedIds;
}
@@ -90,10 +86,9 @@ public function getInsertedIds()
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
- * @return integer|null
* @throws BadMethodCallException if the write result is unacknowledged
*/
- public function getMatchedCount()
+ public function getMatchedCount(): ?int
{
if ($this->isAcknowledged) {
return $this->writeResult->getMatchedCount();
@@ -111,10 +106,9 @@ public function getMatchedCount()
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
- * @return integer|null
* @throws BadMethodCallException if the write result is unacknowledged
*/
- public function getModifiedCount()
+ public function getModifiedCount(): ?int
{
if ($this->isAcknowledged) {
return $this->writeResult->getModifiedCount();
@@ -129,10 +123,9 @@ public function getModifiedCount()
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
- * @return integer|null
* @throws BadMethodCallException if the write result is unacknowledged
*/
- public function getUpsertedCount()
+ public function getUpsertedCount(): ?int
{
if ($this->isAcknowledged) {
return $this->writeResult->getUpsertedCount();
@@ -152,10 +145,9 @@ public function getUpsertedCount()
* This method should only be called if the write was acknowledged.
*
* @see BulkWriteResult::isAcknowledged()
- * @return array
* @throws BadMethodCallException if the write result is unacknowledged
*/
- public function getUpsertedIds()
+ public function getUpsertedIds(): array
{
if ($this->isAcknowledged) {
return $this->writeResult->getUpsertedIds();
@@ -169,10 +161,8 @@ public function getUpsertedIds()
*
* If the update was not acknowledged, other fields from the WriteResult
* (e.g. matchedCount) will be undefined.
- *
- * @return boolean
*/
- public function isAcknowledged()
+ public function isAcknowledged(): bool
{
return $this->isAcknowledged;
}
diff --git a/src/ChangeStream.php b/src/ChangeStream.php
index 55f25b031..a1a732666 100644
--- a/src/ChangeStream.php
+++ b/src/ChangeStream.php
@@ -21,22 +21,16 @@
use MongoDB\BSON\Document;
use MongoDB\BSON\Int64;
use MongoDB\Codec\DocumentCodec;
-use MongoDB\Driver\CursorId;
use MongoDB\Driver\Exception\ConnectionException;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Exception\BadMethodCallException;
use MongoDB\Exception\ResumeTokenException;
use MongoDB\Model\ChangeStreamIterator;
-use ReturnTypeWillChange;
use function assert;
use function call_user_func;
use function in_array;
-use function sprintf;
-use function trigger_error;
-
-use const E_USER_DEPRECATED;
/**
* Iterator for a change stream.
@@ -88,12 +82,8 @@ class ChangeStream implements Iterator
*/
private bool $hasAdvanced = false;
- /**
- * @see https://php.net/iterator.current
- * @return array|object|null
- */
- #[ReturnTypeWillChange]
- public function current()
+ /** @see https://php.net/iterator.current */
+ public function current(): array|object|null
{
$value = $this->iterator->current();
@@ -106,26 +96,9 @@ public function current()
return $this->codec->decode($value);
}
- /**
- * @return CursorId|Int64
- * @psalm-return ($asInt64 is true ? Int64 : CursorId)
- */
- #[ReturnTypeWillChange]
- public function getCursorId(bool $asInt64 = false)
+ public function getCursorId(): Int64
{
- if (! $asInt64) {
- @trigger_error(
- sprintf(
- 'The method "%s" will no longer return a "%s" instance in the future. Pass "true" as argument to change to the new behavior and receive a "%s" instance instead.',
- __METHOD__,
- CursorId::class,
- Int64::class,
- ),
- E_USER_DEPRECATED,
- );
- }
-
- return $this->iterator->getInnerIterator()->getId($asInt64);
+ return $this->iterator->getInnerIterator()->getId();
}
/**
@@ -134,20 +107,14 @@ public function getCursorId(bool $asInt64 = false)
* Null may be returned if no change documents have been iterated and the
* server did not include a postBatchResumeToken in its aggregate or getMore
* command response.
- *
- * @return array|object|null
*/
- public function getResumeToken()
+ public function getResumeToken(): array|object|null
{
return $this->iterator->getResumeToken();
}
- /**
- * @see https://php.net/iterator.key
- * @return int|null
- */
- #[ReturnTypeWillChange]
- public function key()
+ /** @see https://php.net/iterator.key */
+ public function key(): ?int
{
if ($this->valid()) {
return $this->key;
@@ -158,11 +125,9 @@ public function key()
/**
* @see https://php.net/iterator.next
- * @return void
* @throws ResumeTokenException
*/
- #[ReturnTypeWillChange]
- public function next()
+ public function next(): void
{
try {
$this->iterator->next();
@@ -174,11 +139,9 @@ public function next()
/**
* @see https://php.net/iterator.rewind
- * @return void
* @throws ResumeTokenException
*/
- #[ReturnTypeWillChange]
- public function rewind()
+ public function rewind(): void
{
try {
$this->iterator->rewind();
@@ -191,12 +154,8 @@ public function rewind()
}
}
- /**
- * @see https://php.net/iterator.valid
- * @return boolean
- */
- #[ReturnTypeWillChange]
- public function valid()
+ /** @see https://php.net/iterator.valid */
+ public function valid(): bool
{
return $this->iterator->valid();
}
@@ -255,7 +214,8 @@ private function onIteration(bool $incrementKey): void
* have been received in the last response. Therefore, we can unset the
* resumeCallable. This will free any reference to Watch as well as the
* only reference to any implicit session created therein. */
- if ((string) $this->getCursorId(true) === '0') {
+ // Use a type-unsafe comparison to compare with Int64 instances
+ if ($this->getCursorId() == 0) {
$this->resumeCallable = null;
}
diff --git a/src/Client.php b/src/Client.php
index 5ed76a0e0..00a1a6ed3 100644
--- a/src/Client.php
+++ b/src/Client.php
@@ -39,6 +39,7 @@
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use MongoDB\Model\DatabaseInfo;
+use MongoDB\Model\DatabaseInfoIterator;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\ListDatabaseNames;
use MongoDB\Operation\ListDatabases;
@@ -143,9 +144,8 @@ public function __construct(?string $uri = null, array $uriOptions = [], array $
* Return internal properties for debugging purposes.
*
* @see https://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
- * @return array
*/
- public function __debugInfo()
+ public function __debugInfo(): array
{
return [
'manager' => $this->manager,
@@ -166,19 +166,16 @@ public function __debugInfo()
* @see https://php.net/oop5.overloading#object.get
* @see https://php.net/types.string#language.types.string.parsing.complex
* @param string $databaseName Name of the database to select
- * @return Database
*/
- public function __get(string $databaseName)
+ public function __get(string $databaseName): Database
{
return $this->selectDatabase($databaseName);
}
/**
* Return the connection string (i.e. URI).
- *
- * @return string
*/
- public function __toString()
+ public function __toString(): string
{
return $this->uri;
}
@@ -197,10 +194,8 @@ final public function addSubscriber(Subscriber $subscriber): void
* Returns a ClientEncryption instance for explicit encryption and decryption
*
* @param array $options Encryption options
- *
- * @return ClientEncryption
*/
- public function createClientEncryption(array $options)
+ public function createClientEncryption(array $options): ClientEncryption
{
if (isset($options['keyVaultClient'])) {
if ($options['keyVaultClient'] instanceof self) {
@@ -224,7 +219,7 @@ public function createClientEncryption(array $options)
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
- public function dropDatabase(string $databaseName, array $options = [])
+ public function dropDatabase(string $databaseName, array $options = []): array|object
{
if (! isset($options['typeMap'])) {
$options['typeMap'] = $this->typeMap;
@@ -243,10 +238,8 @@ public function dropDatabase(string $databaseName, array $options = [])
/**
* Return the Manager.
- *
- * @return Manager
*/
- public function getManager()
+ public function getManager(): Manager
{
return $this->manager;
}
@@ -255,29 +248,24 @@ public function getManager()
* Return the read concern for this client.
*
* @see https://php.net/manual/en/mongodb-driver-readconcern.isdefault.php
- * @return ReadConcern
*/
- public function getReadConcern()
+ public function getReadConcern(): ReadConcern
{
return $this->readConcern;
}
/**
* Return the read preference for this client.
- *
- * @return ReadPreference
*/
- public function getReadPreference()
+ public function getReadPreference(): ReadPreference
{
return $this->readPreference;
}
/**
* Return the type map for this client.
- *
- * @return array
*/
- public function getTypeMap()
+ public function getTypeMap(): array
{
return $this->typeMap;
}
@@ -286,9 +274,8 @@ public function getTypeMap()
* Return the write concern for this client.
*
* @see https://php.net/manual/en/mongodb-driver-writeconcern.isdefault.php
- * @return WriteConcern
*/
- public function getWriteConcern()
+ public function getWriteConcern(): WriteConcern
{
return $this->writeConcern;
}
@@ -318,7 +305,7 @@ public function listDatabaseNames(array $options = []): Iterator
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
- public function listDatabases(array $options = [])
+ public function listDatabases(array $options = []): DatabaseInfoIterator
{
$operation = new ListDatabases($options);
$server = select_server($this->manager, $options);
@@ -343,10 +330,9 @@ final public function removeSubscriber(Subscriber $subscriber): void
* @param string $databaseName Name of the database containing the collection
* @param string $collectionName Name of the collection to select
* @param array $options Collection constructor options
- * @return Collection
* @throws InvalidArgumentException for parameter/option parsing errors
*/
- public function selectCollection(string $databaseName, string $collectionName, array $options = [])
+ public function selectCollection(string $databaseName, string $collectionName, array $options = []): Collection
{
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
@@ -359,10 +345,9 @@ public function selectCollection(string $databaseName, string $collectionName, a
* @see Database::__construct() for supported options
* @param string $databaseName Name of the database to select
* @param array $options Database constructor options
- * @return Database
* @throws InvalidArgumentException for parameter/option parsing errors
*/
- public function selectDatabase(string $databaseName, array $options = [])
+ public function selectDatabase(string $databaseName, array $options = []): Database
{
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];
@@ -374,9 +359,8 @@ public function selectDatabase(string $databaseName, array $options = [])
*
* @see https://php.net/manual/en/mongodb-driver-manager.startsession.php
* @param array $options Session options
- * @return Session
*/
- public function startSession(array $options = [])
+ public function startSession(array $options = []): Session
{
return $this->manager->startSession($options);
}
@@ -387,10 +371,9 @@ public function startSession(array $options = [])
* @see Watch::__construct() for supported options
* @param array $pipeline Aggregation pipeline
* @param array $options Command options
- * @return ChangeStream
* @throws InvalidArgumentException for parameter/option parsing errors
*/
- public function watch(array $pipeline = [], array $options = [])
+ public function watch(array $pipeline = [], array $options = []): ChangeStream
{
if (is_builder_pipeline($pipeline)) {
$pipeline = new Pipeline(...$pipeline);
diff --git a/src/Codec/DecodeIfSupported.php b/src/Codec/DecodeIfSupported.php
index 7fd768011..ea8dde15c 100644
--- a/src/Codec/DecodeIfSupported.php
+++ b/src/Codec/DecodeIfSupported.php
@@ -30,17 +30,13 @@ abstract public function canDecode(mixed $value): bool;
/**
* @psalm-param BSONType $value
- * @return mixed
* @psalm-return NativeType
* @throws UnsupportedValueException if the decoder does not support the value
*/
- abstract public function decode(mixed $value);
+ abstract public function decode(mixed $value): mixed;
- /**
- * @return mixed
- * @psalm-return ($value is BSONType ? NativeType : $value)
- */
- public function decodeIfSupported(mixed $value)
+ /** @psalm-return ($value is BSONType ? NativeType : $value) */
+ public function decodeIfSupported(mixed $value): mixed
{
return $this->canDecode($value) ? $this->decode($value) : $value;
}
diff --git a/src/Codec/Decoder.php b/src/Codec/Decoder.php
index 432fb2dd7..37ff9b263 100644
--- a/src/Codec/Decoder.php
+++ b/src/Codec/Decoder.php
@@ -37,11 +37,10 @@ public function canDecode(mixed $value): bool;
* should throw an exception.
*
* @psalm-param BSONType $value
- * @return mixed
* @psalm-return NativeType
* @throws UnsupportedValueException if the decoder does not support the value
*/
- public function decode(mixed $value);
+ public function decode(mixed $value): mixed;
/**
* Decodes a given value if supported, otherwise returns the value as-is.
@@ -49,8 +48,7 @@ public function decode(mixed $value);
* The DecodeIfSupported trait provides a default implementation of this
* method.
*
- * @return mixed
* @psalm-return ($value is BSONType ? NativeType : $value)
*/
- public function decodeIfSupported(mixed $value);
+ public function decodeIfSupported(mixed $value): mixed;
}
diff --git a/src/Codec/EncodeIfSupported.php b/src/Codec/EncodeIfSupported.php
index 33823cfd6..2ce1fcf53 100644
--- a/src/Codec/EncodeIfSupported.php
+++ b/src/Codec/EncodeIfSupported.php
@@ -30,17 +30,13 @@ abstract public function canEncode(mixed $value): bool;
/**
* @psalm-param NativeType $value
- * @return mixed
* @psalm-return BSONType
* @throws UnsupportedValueException if the encoder does not support the value
*/
- abstract public function encode(mixed $value);
+ abstract public function encode(mixed $value): mixed;
- /**
- * @return mixed
- * @psalm-return ($value is NativeType ? BSONType : $value)
- */
- public function encodeIfSupported(mixed $value)
+ /** @psalm-return ($value is NativeType ? BSONType : $value) */
+ public function encodeIfSupported(mixed $value): mixed
{
return $this->canEncode($value) ? $this->encode($value) : $value;
}
diff --git a/src/Codec/Encoder.php b/src/Codec/Encoder.php
index 0cd0d58cb..c8ee0917b 100644
--- a/src/Codec/Encoder.php
+++ b/src/Codec/Encoder.php
@@ -37,11 +37,10 @@ public function canEncode(mixed $value): bool;
* should throw an exception.
*
* @psalm-param NativeType $value
- * @return mixed
* @psalm-return BSONType
* @throws UnsupportedValueException if the encoder does not support the value
*/
- public function encode(mixed $value);
+ public function encode(mixed $value): mixed;
/**
* Encodes a given value if supported, otherwise returns the value as-is.
@@ -49,8 +48,7 @@ public function encode(mixed $value);
* The EncodeIfSupported trait provides a default implementation of this
* method.
*
- * @return mixed
* @psalm-return ($value is NativeType ? BSONType : $value)
*/
- public function encodeIfSupported(mixed $value);
+ public function encodeIfSupported(mixed $value): mixed;
}
diff --git a/src/Collection.php b/src/Collection.php
index ab82cca5d..2c659f5db 100644
--- a/src/Collection.php
+++ b/src/Collection.php
@@ -38,6 +38,7 @@
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use MongoDB\Model\IndexInfo;
+use MongoDB\Model\IndexInfoIterator;
use MongoDB\Operation\Aggregate;
use MongoDB\Operation\BulkWrite;
use MongoDB\Operation\Count;
@@ -181,9 +182,8 @@ public function __construct(private Manager $manager, private string $databaseNa
* Return internal properties for debugging purposes.
*
* @see https://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
- * @return array
*/
- public function __debugInfo()
+ public function __debugInfo(): array
{
return [
'builderEncoder' => $this->builderEncoder,
@@ -202,9 +202,8 @@ public function __debugInfo()
* Return the collection namespace (e.g. "db.collection").
*
* @see https://mongodb.com/docs/manual/core/databases-and-collections/
- * @return string
*/
- public function __toString()
+ public function __toString(): string
{
return $this->databaseName . '.' . $this->collectionName;
}
@@ -215,13 +214,12 @@ public function __toString()
* @see Aggregate::__construct() for supported options
* @param array $pipeline Aggregation pipeline
* @param array $options Command options
- * @return CursorInterface&Iterator
* @throws UnexpectedValueException if the command response was malformed
* @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 aggregate(array $pipeline, array $options = [])
+ public function aggregate(array $pipeline, array $options = []): CursorInterface
{
if (is_builder_pipeline($pipeline)) {
$pipeline = new Pipeline(...$pipeline);
@@ -261,12 +259,11 @@ public function aggregate(array $pipeline, array $options = [])
* @see BulkWrite::__construct() for supported options
* @param array[] $operations List of write operations
* @param array $options Command options
- * @return BulkWriteResult
* @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 bulkWrite(array $operations, array $options = [])
+ public function bulkWrite(array $operations, array $options = []): BulkWriteResult
{
$options = $this->inheritBuilderEncoder($options);
$options = $this->inheritWriteOptions($options);
@@ -283,7 +280,6 @@ public function bulkWrite(array $operations, array $options = [])
* @see Count::__construct() for supported options
* @param array|object $filter Query by which to filter documents
* @param array $options Command options
- * @return integer
* @throws UnexpectedValueException if the command response was malformed
* @throws UnsupportedException if options are not supported by the selected server
* @throws InvalidArgumentException for parameter/option parsing errors
@@ -291,7 +287,7 @@ public function bulkWrite(array $operations, array $options = [])
*
* @deprecated 1.4
*/
- public function count(array|object $filter = [], array $options = [])
+ public function count(array|object $filter = [], array $options = []): int
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritReadOptions($options);
@@ -307,13 +303,12 @@ public function count(array|object $filter = [], array $options = [])
* @see CountDocuments::__construct() for supported options
* @param array|object $filter Query by which to filter documents
* @param array $options Command options
- * @return integer
* @throws UnexpectedValueException if the command response was malformed
* @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 countDocuments(array|object $filter = [], array $options = [])
+ public function countDocuments(array|object $filter = [], array $options = []): int
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritReadOptions($options);
@@ -336,7 +331,7 @@ public function countDocuments(array|object $filter = [], array $options = [])
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
- public function createIndex(array|object $key, array $options = [])
+ public function createIndex(array|object $key, array $options = []): string
{
$operationOptionKeys = ['comment' => 1, 'commitQuorum' => 1, 'maxTimeMS' => 1, 'session' => 1, 'writeConcern' => 1];
$indexOptions = array_diff_key($options, $operationOptionKeys);
@@ -372,7 +367,7 @@ public function createIndex(array|object $key, array $options = [])
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
- public function createIndexes(array $indexes, array $options = [])
+ public function createIndexes(array $indexes, array $options = []): array
{
$options = $this->inheritWriteOptions($options);
@@ -446,12 +441,11 @@ public function createSearchIndexes(array $indexes, array $options = []): array
* @see https://mongodb.com/docs/manual/reference/command/delete/
* @param array|object $filter Query by which to delete documents
* @param array $options Command options
- * @return DeleteResult
* @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 deleteMany(array|object $filter, array $options = [])
+ public function deleteMany(array|object $filter, array $options = []): DeleteResult
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritWriteOptions($options);
@@ -468,12 +462,11 @@ public function deleteMany(array|object $filter, array $options = [])
* @see https://mongodb.com/docs/manual/reference/command/delete/
* @param array|object $filter Query by which to delete documents
* @param array $options Command options
- * @return DeleteResult
* @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 deleteOne(array|object $filter, array $options = [])
+ public function deleteOne(array|object $filter, array $options = []): DeleteResult
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritWriteOptions($options);
@@ -490,13 +483,12 @@ public function deleteOne(array|object $filter, array $options = [])
* @param string $fieldName Field for which to return distinct values
* @param array|object $filter Query by which to filter documents
* @param array $options Command options
- * @return array
* @throws UnexpectedValueException if the command response was malformed
* @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 distinct(string $fieldName, array|object $filter = [], array $options = [])
+ public function distinct(string $fieldName, array|object $filter = [], array $options = []): array
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritReadOptions($options);
@@ -517,7 +509,7 @@ public function distinct(string $fieldName, array|object $filter = [], array $op
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
- public function drop(array $options = [])
+ public function drop(array $options = []): array|object
{
$options = $this->inheritWriteOptions($options);
$options = $this->inheritTypeMap($options);
@@ -547,7 +539,7 @@ public function drop(array $options = [])
* @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 = [])
+ public function dropIndex(string|IndexInfo $indexName, array $options = []): array|object
{
$indexName = (string) $indexName;
@@ -573,7 +565,7 @@ public function dropIndex(string|IndexInfo $indexName, array $options = [])
* @throws InvalidArgumentException for parameter/option parsing errors
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
- public function dropIndexes(array $options = [])
+ public function dropIndexes(array $options = []): array|object
{
$options = $this->inheritWriteOptions($options);
$options = $this->inheritTypeMap($options);
@@ -606,13 +598,12 @@ public function dropSearchIndex(string $name, array $options = []): void
*
* @see EstimatedDocumentCount::__construct() for supported options
* @param array $options Command options
- * @return integer
* @throws UnexpectedValueException if the command response was malformed
* @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 estimatedDocumentCount(array $options = [])
+ public function estimatedDocumentCount(array $options = []): int
{
$options = $this->inheritReadOptions($options);
@@ -628,12 +619,11 @@ public function estimatedDocumentCount(array $options = [])
* @see https://mongodb.com/docs/manual/reference/command/explain/
* @param Explainable $explainable Command on which to run explain
* @param array $options Additional options
- * @return array|object
* @throws UnsupportedException if explainable or 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 explain(Explainable $explainable, array $options = [])
+ public function explain(Explainable $explainable, array $options = []): array|object
{
$options = $this->inheritReadPreference($options);
$options = $this->inheritTypeMap($options);
@@ -650,12 +640,11 @@ public function explain(Explainable $explainable, array $options = [])
* @see https://mongodb.com/docs/manual/crud/#read-operations
* @param array|object $filter Query by which to filter documents
* @param array $options Additional options
- * @return CursorInterface&Iterator
* @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 find(array|object $filter = [], array $options = [])
+ public function find(array|object $filter = [], array $options = []): CursorInterface
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritReadOptions($options);
@@ -673,12 +662,11 @@ public function find(array|object $filter = [], array $options = [])
* @see https://mongodb.com/docs/manual/crud/#read-operations
* @param array|object $filter Query by which to filter documents
* @param array $options Additional options
- * @return array|object|null
* @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 findOne(array|object $filter = [], array $options = [])
+ public function findOne(array|object $filter = [], array $options = []): array|object|null
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritReadOptions($options);
@@ -698,13 +686,12 @@ public function findOne(array|object $filter = [], array $options = [])
* @see https://mongodb.com/docs/manual/reference/command/findAndModify/
* @param array|object $filter Query by which to filter documents
* @param array $options Command options
- * @return array|object|null
* @throws UnexpectedValueException if the command response was malformed
* @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 findOneAndDelete(array|object $filter, array $options = [])
+ public function findOneAndDelete(array|object $filter, array $options = []): array|object|null
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritWriteOptions($options);
@@ -729,13 +716,12 @@ public function findOneAndDelete(array|object $filter, array $options = [])
* @param array|object $filter Query by which to filter documents
* @param array|object $replacement Replacement document
* @param array $options Command options
- * @return array|object|null
* @throws UnexpectedValueException if the command response was malformed
* @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 findOneAndReplace(array|object $filter, array|object $replacement, array $options = [])
+ public function findOneAndReplace(array|object $filter, array|object $replacement, array $options = []): array|object|null
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritWriteOptions($options);
@@ -760,13 +746,12 @@ public function findOneAndReplace(array|object $filter, array|object $replacemen
* @param array|object $filter Query by which to filter documents
* @param array|object $update Update to apply to the matched document
* @param array $options Command options
- * @return array|object|null
* @throws UnexpectedValueException if the command response was malformed
* @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 findOneAndUpdate(array|object $filter, array|object $update, array $options = [])
+ public function findOneAndUpdate(array|object $filter, array|object $update, array $options = []): array|object|null
{
$filter = $this->builderEncoder->encodeIfSupported($filter);
$options = $this->inheritWriteOptions($options);
@@ -779,30 +764,24 @@ public function findOneAndUpdate(array|object $filter, array|object $update, arr
/**
* Return the collection name.
- *
- * @return string
*/
- public function getCollectionName()
+ public function getCollectionName(): string
{
return $this->collectionName;
}
/**
* Return the database name.
- *
- * @return string
*/
- public function getDatabaseName()
+ public function getDatabaseName(): string
{
return $this->databaseName;
}
/**
* Return the Manager.
- *
- * @return Manager
*/
- public function getManager()
+ public function getManager(): Manager
{
return $this->manager;
}
@@ -811,9 +790,8 @@ public function getManager()
* Return the collection namespace.
*
* @see https://mongodb.com/docs/manual/reference/glossary/#term-namespace
- * @return string
*/
- public function getNamespace()
+ public function getNamespace(): string
{
return $this->databaseName . '.' . $this->collectionName;
}
@@ -822,29 +800,24 @@ public function getNamespace()
* Return the read concern for this collection.
*
* @see https://php.net/manual/en/mongodb-driver-readconcern.isdefault.php
- * @return ReadConcern
*/
- public function getReadConcern()
+ public function getReadConcern(): ReadConcern
{
return $this->readConcern;
}
/**
* Return the read preference for this collection.
- *
- * @return ReadPreference
*/
- public function getReadPreference()
+ public function getReadPreference(): ReadPreference
{
return $this->readPreference;
}
/**
* Return the type map for this collection.
- *
- * @return array
*/
- public function getTypeMap()
+ public function getTypeMap(): array
{
return $this->typeMap;
}
@@ -853,9 +826,8 @@ public function getTypeMap()
* Return the write concern for this collection.
*
* @see https://php.net/manual/en/mongodb-driver-writeconcern.isdefault.php
- * @return WriteConcern
*/
- public function getWriteConcern()
+ public function getWriteConcern(): WriteConcern
{
return $this->writeConcern;
}
@@ -867,11 +839,10 @@ public function getWriteConcern()
* @see https://mongodb.com/docs/manual/reference/command/insert/
* @param list