Skip to content

Improve type of Collection Bulk Write operations #1694

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

Open
wants to merge 2 commits into
base: v2.x
Choose a base branch
from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented May 20, 2025

The Collection::bulkWrite($operations) method requires a complex array structure for the operations list. Describing this type using psalm custom type feature to helps static analysis. I don't think the IDE completion can really leverage this new type.

@GromNaN GromNaN requested a review from a team as a code owner May 20, 2025 11:47
@GromNaN GromNaN requested a review from alcaeus May 20, 2025 11:47
@@ -306,14 +310,14 @@
// $args[0] was already validated above. Since DocumentCodec::encode will always return a Document
// instance, there is no need to re-validate the returned value here.
if ($codec) {
$operations[$i][$type][0] = $codec->encode($args[0]);
$operation[$type][0] = $codec->encode($args[0]);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument Note

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

break;

case self::DELETE_MANY:
case self::DELETE_ONE:
$operations[$i][$type][0] = $builderEncoder->encodeIfSupported($args[0]);
$operation[$type][0] = $builderEncoder->encodeIfSupported($args[0]);

Check notice

Code scanning / Psalm

MixedAssignment Note

Unable to determine the type of this assignment

break;

case self::REPLACE_ONE:
$operations[$i][$type][0] = $builderEncoder->encodeIfSupported($args[0]);
$operation[$type][0] = $builderEncoder->encodeIfSupported($args[0]);

Check notice

Code scanning / Psalm

MixedAssignment Note

Unable to determine the type of this assignment

if (! isset($args[1]) && ! array_key_exists(1, $args)) {
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
}

if ($codec) {
$operations[$i][$type][1] = $codec->encode($args[1]);
$operation[$type][1] = $codec->encode($args[1]);

Check notice

Code scanning / Psalm

PossiblyInvalidArgument Note

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

break;

case self::UPDATE_MANY:
case self::UPDATE_ONE:
$operations[$i][$type][0] = $builderEncoder->encodeIfSupported($args[0]);
$operation[$type][0] = $builderEncoder->encodeIfSupported($args[0]);

Check notice

Code scanning / Psalm

MixedAssignment Note

Unable to determine the type of this assignment

if (! isset($args[1]) && ! array_key_exists(1, $args)) {
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
}

$operations[$i][$type][1] = $args[1] = $builderEncoder->encodeIfSupported($args[1]);
$operation[$type][1] = $args[1] = $builderEncoder->encodeIfSupported($args[1]);

Check notice

Code scanning / Psalm

MixedAssignment Note

Unable to determine the type of this assignment

if (! isset($args[1]) && ! array_key_exists(1, $args)) {
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
}

$operations[$i][$type][1] = $args[1] = $builderEncoder->encodeIfSupported($args[1]);
$operation[$type][1] = $args[1] = $builderEncoder->encodeIfSupported($args[1]);

Check notice

Code scanning / Psalm

MixedAssignment Note

Unable to determine the type of this assignment
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.

2 participants