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
Open
Show file tree
Hide file tree
Changes from 1 commit
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
104 changes: 22 additions & 82 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -449,107 +449,47 @@
</MixedMethodCall>
</file>
<file src="src/Operation/BulkWrite.php">
<LessSpecificReturnStatement>
<code><![CDATA[$operations]]></code>
</LessSpecificReturnStatement>
<MixedArgument>
<code><![CDATA[$args]]></code>
<code><![CDATA[$args]]></code>
<code><![CDATA[$args]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[2]]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]['upsert']]]></code>
<code><![CDATA[$args[2]['upsert']]]></code>
<code><![CDATA[$args[2]['upsert']]]></code>
<code><![CDATA[$args[2]['upsert']]]></code>
</MixedArrayAccess>
<MixedArrayAssignment>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]['limit']]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]['multi']]]></code>
<code><![CDATA[$args[2]['multi']]]></code>
<code><![CDATA[$operations[$i][$type][0]]]></code>
<code><![CDATA[$operations[$i][$type][0]]]></code>
<code><![CDATA[$operations[$i][$type][0]]]></code>
<code><![CDATA[$operations[$i][$type][0]]]></code>
<code><![CDATA[$operations[$i][$type][1]]]></code>
<code><![CDATA[$operations[$i][$type][1]]]></code>
<code><![CDATA[$operations[$i][$type][1]]]></code>
<code><![CDATA[$operations[$i][$type][2]]]></code>
<code><![CDATA[$operations[$i][$type][2]]]></code>
</MixedArrayAssignment>
<MixedAssignment>
<code><![CDATA[$args]]></code>
<code><![CDATA[$args]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$args[2]]]></code>
<code><![CDATA[$insertedIds[$i]]]></code>
<code><![CDATA[$operations[$i][$type][0]]]></code>
<code><![CDATA[$operations[$i][$type][0]]]></code>
<code><![CDATA[$operations[$i][$type][0]]]></code>
<code><![CDATA[$operations[$i][$type][1]]]></code>
<code><![CDATA[$operations[$i][$type][1]]]></code>
<code><![CDATA[$operations[$i][$type][2]]]></code>
<code><![CDATA[$operations[$i][$type][2]]]></code>
<code><![CDATA[$options[$option]]]></code>
<code><![CDATA[$options['session']]]></code>
<code><![CDATA[$options['writeConcern']]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[isInTransaction]]></code>
</MixedMethodCall>
<MixedOperand>
<code><![CDATA[$args[2]]]></code>
<MoreSpecificReturnType>
<code><![CDATA[list<OperationType>]]></code>
</MoreSpecificReturnType>
<NullArgument>
<code><![CDATA[$type]]></code>
</NullArgument>
<PossiblyInvalidArgument>
<code><![CDATA[$args[0]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
</PossiblyInvalidArgument>
<PossiblyNullArgument>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
</PossiblyNullArgument>
<PossiblyUndefinedArrayOffset>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[1]]]></code>
<code><![CDATA[$args[2]]]></code>
</MixedOperand>
</PossiblyUndefinedArrayOffset>
</file>
<file src="src/Operation/ClientBulkWriteCommand.php">
<MixedMethodCall>
Expand Down
3 changes: 2 additions & 1 deletion src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
use function is_array;
use function strlen;

/** @psalm-import-type OperationType from BulkWrite */
class Collection
{
private const DEFAULT_TYPE_MAP = [
Expand Down Expand Up @@ -254,8 +255,8 @@
* Executes multiple write operations.
*
* @see BulkWrite::__construct() for supported options
* @param array[] $operations List of write operations
* @param list<OperationType> $operations List of write operations
* @param array $options Command options

Check failure on line 259 in src/Collection.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 15 spaces after parameter type; 3 found
* @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)
Expand Down
18 changes: 11 additions & 7 deletions src/Operation/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
* Operation for executing multiple write operations.
*
* @see \MongoDB\Collection::bulkWrite()
*
* @psalm-type Document = object|array
* @psalm-type OperationType = array{deleteMany: array{0: Document, 1?: array}}|array{deleteOne: array{0: Document, 1?: array}}|array{insertOne: array{0: Document}}|array{replaceOne: array{0: Document, 1: Document, 2?: array}}|array{updateMany: array{0: Document, 1: Document, 2?: array}}|array{updateOne: array{0: Document, 1: Document, 2?: array}}
*/
final class BulkWrite
{
Expand All @@ -55,7 +58,7 @@
public const UPDATE_MANY = 'updateMany';
public const UPDATE_ONE = 'updateOne';

/** @var array[] */
/** @psalm-var list<OperationType> */
private array $operations;

private array $options;
Expand Down Expand Up @@ -132,10 +135,11 @@
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array[] $operations List of write operations
* @param array $options Command options
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $operations List of write operations

Check failure on line 140 in src/Operation/BulkWrite.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 2 spaces after parameter type; 1 found

Check failure on line 140 in src/Operation/BulkWrite.php

View workflow job for this annotation

GitHub Actions / phpcs

Expected 5 spaces after parameter name; 1 found
* @psalm-param list<OperationType> $operations
* @param array $options Command options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct(private string $databaseName, private string $collectionName, array $operations, array $options = [])
Expand Down Expand Up @@ -276,8 +280,8 @@
}

/**
* @param array[] $operations
* @return array[]
* @psalm-param list<OperationType> $operations
* @psalm-return list<OperationType>
*/
private function validateOperations(array $operations, ?DocumentCodec $codec, Encoder $builderEncoder): array
{
Expand Down
9 changes: 9 additions & 0 deletions tests/Operation/BulkWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public function testMultipleOperationsInOneElement(): void
]);
}

public function testEmptyOperation(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Expected one element in $operation[0], actually: 0');
new BulkWrite($this->getDatabaseName(), $this->getCollectionName(), [
[],
]);
}

public function testUnknownOperation(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down
Loading