Skip to content

PHPLIB-1185: Add new methods to get database and collection instances #1494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function __debugInfo()
*/
public function __get(string $databaseName)
{
return $this->selectDatabase($databaseName);
return $this->getDatabase($databaseName);
}

/**
Expand Down Expand Up @@ -247,6 +247,37 @@ public function dropDatabase(string $databaseName, array $options = [])
return $operation->execute($server);
}

/**
* Returns a collection instance.
*
* If the collection does not exist in the database, it is not created when
* invoking this method.
*
* @see Collection::__construct() for supported options
* @throws InvalidArgumentException for parameter/option parsing errors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose you want the doc block for the note about "does not exist", but the parameter annotations here have no value. The code is self-documenting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it helps static analysis to tell which kind of exception can be thrown?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, but since there is no throws within this method I would assume that tooling would inherit from doc blocks of any referenced methods, which would have @throws. I'll defer to you guys, though.

Copy link
Member Author

@alcaeus alcaeus Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left @throws in for the time being, since they are included in all other methods as well. I'm on the fence about the usefulness of them, I can see them being useful when coupled with static analysis, but since we don't have anything to enforce correctness of @throws statements, we may reconsider having them for methods that don't directly throw any exceptions.

That said, I did remove the @param tags as the parameter names are self-explanatory.

*/
public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection
{
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];

return new Collection($this->manager, $databaseName, $collectionName, $options);
}

/**
* Returns a database instance.
*
* If the database does not exist on the server, it is not created when
* invoking this method.
*
* @see Database::__construct() for supported options
*/
public function getDatabase(string $databaseName, array $options = []): Database
{
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];

return new Database($this->manager, $databaseName, $options);
}

/**
* Return the Manager.
*
Expand Down Expand Up @@ -354,9 +385,7 @@ final public function removeSubscriber(Subscriber $subscriber): void
*/
public function selectCollection(string $databaseName, string $collectionName, array $options = [])
{
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];

return new Collection($this->manager, $databaseName, $collectionName, $options);
return $this->getCollection($databaseName, $collectionName, $options);
}

/**
Expand All @@ -370,9 +399,7 @@ public function selectCollection(string $databaseName, string $collectionName, a
*/
public function selectDatabase(string $databaseName, array $options = [])
{
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder];

return new Database($this->manager, $databaseName, $options);
return $this->getDatabase($databaseName, $options);
}

/**
Expand Down
34 changes: 24 additions & 10 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function __debugInfo()
*/
public function __get(string $collectionName)
{
return $this->selectCollection($collectionName);
return $this->getCollection($collectionName);
}

/**
Expand Down Expand Up @@ -422,6 +422,28 @@ public function dropCollection(string $collectionName, array $options = [])
return $operation->execute($server);
}

/**
* Returns a collection instance.
*
* If the collection does not exist in the database, it is not created when
* invoking this method.
*
* @see Collection::__construct() for supported options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function getCollection(string $collectionName, array $options = []): Collection
{
$options += [
'builderEncoder' => $this->builderEncoder,
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
'typeMap' => $this->typeMap,
'writeConcern' => $this->writeConcern,
];

return new Collection($this->manager, $this->databaseName, $collectionName, $options);
}

/**
* Returns the database name.
*
Expand Down Expand Up @@ -588,15 +610,7 @@ public function renameCollection(string $fromCollectionName, string $toCollectio
*/
public function selectCollection(string $collectionName, array $options = [])
{
$options += [
'builderEncoder' => $this->builderEncoder,
'readConcern' => $this->readConcern,
'readPreference' => $this->readPreference,
'typeMap' => $this->typeMap,
'writeConcern' => $this->writeConcern,
];

return new Collection($this->manager, $this->databaseName, $collectionName, $options);
return $this->getCollection($collectionName, $options);
}

/**
Expand Down