Skip to content

Commit 1cc953a

Browse files
authored
PHPLIB-980: Make uri parameter for MongoDB\Client::__construct nullable (#986)
1 parent 8a32752 commit 1cc953a

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

docs/reference/method/MongoDBClient__construct.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Definition
1919

2020
.. code-block:: php
2121

22-
function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
22+
function __construct(?string $uri = null, array $uriOptions = [], array $driverOptions = [])
2323

2424
This constructor has the following parameters:
2525

src/Client.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444

4545
class Client
4646
{
47+
public const DEFAULT_URI = 'mongodb://127.0.0.1/';
48+
4749
/** @var array */
4850
private static $defaultTypeMap = [
4951
'array' => BSONArray::class,
@@ -91,14 +93,14 @@ class Client
9193
* @see https://mongodb.com/docs/manual/reference/connection-string/
9294
* @see https://php.net/manual/en/mongodb-driver-manager.construct.php
9395
* @see https://php.net/manual/en/mongodb.persistence.php#mongodb.persistence.typemaps
94-
* @param string $uri MongoDB connection string
95-
* @param array $uriOptions Additional connection string options
96-
* @param array $driverOptions Driver-specific options
96+
* @param string|null $uri MongoDB connection string. If none is provided, this defaults to self::DEFAULT_URI.
97+
* @param array $uriOptions Additional connection string options
98+
* @param array $driverOptions Driver-specific options
9799
* @throws InvalidArgumentException for parameter/option parsing errors
98100
* @throws DriverInvalidArgumentException for parameter/option parsing errors in the driver
99101
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
100102
*/
101-
public function __construct(string $uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
103+
public function __construct(?string $uri = null, array $uriOptions = [], array $driverOptions = [])
102104
{
103105
$driverOptions += ['typeMap' => self::$defaultTypeMap];
104106

@@ -116,7 +118,7 @@ public function __construct(string $uri = 'mongodb://127.0.0.1/', array $uriOpti
116118

117119
$driverOptions['driver'] = $this->mergeDriverInfo($driverOptions['driver'] ?? []);
118120

119-
$this->uri = $uri;
121+
$this->uri = $uri ?? self::DEFAULT_URI;
120122
$this->typeMap = $driverOptions['typeMap'];
121123

122124
unset($driverOptions['typeMap']);

0 commit comments

Comments
 (0)