Skip to content
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

PHPLIB-1627: BulkWriteCommand API #1630

Draft
wants to merge 28 commits into
base: v2.x
Choose a base branch
from
Draft

Conversation

jmikola
Copy link
Member

@jmikola jmikola commented Mar 4, 2025

@jmikola jmikola force-pushed the 2.x-bulkwrite branch 4 times, most recently from d9351cc to 33952f7 Compare March 24, 2025 15:50
jmikola added 2 commits April 1, 2025 16:06
Also renames the operation class to ClientBulkWriteCommand to avoid aliasing in Client.
return $this;
}

public function withCollection(Collection $collection): self
Copy link
Member

Choose a reason for hiding this comment

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

You can add the upcoming #[NoDiscard] attribute on this method, as it returns a new instance. @alcaeus approved the RFC, so he would agree.

This would prevent people from thinking this is mutating the object.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should that also be done for createWithCollection(), since it also returns a new instance? #[NoDiscard] seems like it'd be appropriate for any factory method.

Copy link
Member Author

Choose a reason for hiding this comment

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

I assume this will also require baselining the UndefinedAttributeClass error in Psalm.

Copy link
Member

Choose a reason for hiding this comment

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

We'll add it later: this new attribute will be in the next version of PHP, in November. We can expect it in the polyfill symfony/polyfill#522

public function insertOne(array|object $document, mixed &$id = null): self
{
if ($this->codec) {
$document = $this->codec->encode($document);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument Note

Argument 1 of MongoDB\Codec\DocumentCodec::encode expects object, but possibly different type array<array-key, mixed>|object provided
Copy link
Member

Choose a reason for hiding this comment

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

The type in DocumentCodec should be relaxed to @psalm-template ObjectType of object|array

* @psalm-template ObjectType of object

$filter = $this->builderEncoder->encodeIfSupported($filter);

if ($this->codec) {
$replacement = $this->codec->encode($replacement);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument Note

Argument 1 of MongoDB\Codec\DocumentCodec::encode expects object, but possibly different type array<array-key, mixed>|object provided
*/
public function execute(Server $server): BulkWriteCommandResult
{
$inTransaction = isset($this->options['session']) && $this->options['session']->isInTransaction();

Check notice

Code scanning / Psalm

MixedMethodCall Note

Cannot determine the type of $this->options['session'] when calling method isInTransaction

$options = array_filter($this->options, fn ($value) => isset($value));

return $server->executeBulkWriteCommand($this->bulkWriteCommand, $options);

Check notice

Code scanning / Psalm

MixedReturnStatement Note

Could not infer a return type
public function insertOne(array|object $document, mixed &$id = null): self
{
if ($this->codec) {
$document = $this->codec->encode($document);
Copy link
Member

Choose a reason for hiding this comment

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

The type in DocumentCodec should be relaxed to @psalm-template ObjectType of object|array

* @psalm-template ObjectType of object

}

// Capture the document's _id, which may have been generated, in an optional output variable
/** @var mixed */
Copy link
Member

Choose a reason for hiding this comment

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

This @var annotation should not be necessary as you added the stub for BulkWriteCommand

return $this;
}

public function withCollection(Collection $collection): self
Copy link
Member

Choose a reason for hiding this comment

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

We'll add it later: this new attribute will be in the next version of PHP, in November. We can expect it in the polyfill symfony/polyfill#522

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants