Skip to content

Commit f4a3b44

Browse files
committed
Update $operation by reference
1 parent 8e37d53 commit f4a3b44

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

psalm-baseline.xml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -449,30 +449,24 @@
449449
</MixedMethodCall>
450450
</file>
451451
<file src="src/Operation/BulkWrite.php">
452-
<LessSpecificReturnStatement>
453-
<code><![CDATA[$operations]]></code>
454-
</LessSpecificReturnStatement>
455452
<MixedArgument>
456453
<code><![CDATA[$args[1]]]></code>
457454
<code><![CDATA[$args[1]]]></code>
458455
</MixedArgument>
459456
<MixedAssignment>
460457
<code><![CDATA[$args[1]]]></code>
461458
<code><![CDATA[$insertedIds[$i]]]></code>
462-
<code><![CDATA[$operations[$i][$type][0]]]></code>
463-
<code><![CDATA[$operations[$i][$type][0]]]></code>
464-
<code><![CDATA[$operations[$i][$type][0]]]></code>
465-
<code><![CDATA[$operations[$i][$type][1]]]></code>
459+
<code><![CDATA[$operation[$type][0]]]></code>
460+
<code><![CDATA[$operation[$type][0]]]></code>
461+
<code><![CDATA[$operation[$type][0]]]></code>
462+
<code><![CDATA[$operation[$type][1]]]></code>
466463
<code><![CDATA[$options[$option]]]></code>
467464
<code><![CDATA[$options['session']]]></code>
468465
<code><![CDATA[$options['writeConcern']]]></code>
469466
</MixedAssignment>
470467
<MixedMethodCall>
471468
<code><![CDATA[isInTransaction]]></code>
472469
</MixedMethodCall>
473-
<MoreSpecificReturnType>
474-
<code><![CDATA[list<OperationType>]]></code>
475-
</MoreSpecificReturnType>
476470
<NullArgument>
477471
<code><![CDATA[$type]]></code>
478472
</NullArgument>

src/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ public function aggregate(array|Pipeline $pipeline, array $options = []): Cursor
255255
* Executes multiple write operations.
256256
*
257257
* @see BulkWrite::__construct() for supported options
258-
* @param list<OperationType> $operations List of write operations
259-
* @param array $options Command options
258+
* @psalm-param list<OperationType> $operations List of write operations
259+
* @param array $options Command options
260260
* @throws UnsupportedException if options are not supported by the selected server
261261
* @throws InvalidArgumentException for parameter/option parsing errors
262262
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)

src/Operation/BulkWrite.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ final class BulkWrite
137137
*
138138
* @param string $databaseName Database name
139139
* @param string $collectionName Collection name
140-
* @param array $operations List of write operations
140+
* @param array $operations List of write operations
141141
* @psalm-param list<OperationType> $operations
142142
* @param array $options Command options
143143
* @throws InvalidArgumentException for parameter/option parsing errors
@@ -285,7 +285,7 @@ private function createExecuteOptions(): array
285285
*/
286286
private function validateOperations(array $operations, ?DocumentCodec $codec, Encoder $builderEncoder): array
287287
{
288-
foreach ($operations as $i => $operation) {
288+
foreach ($operations as $i => &$operation) {
289289
if (! is_array($operation)) {
290290
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]', $i), $operation, 'array');
291291
}
@@ -310,14 +310,14 @@ private function validateOperations(array $operations, ?DocumentCodec $codec, En
310310
// $args[0] was already validated above. Since DocumentCodec::encode will always return a Document
311311
// instance, there is no need to re-validate the returned value here.
312312
if ($codec) {
313-
$operations[$i][$type][0] = $codec->encode($args[0]);
313+
$operation[$type][0] = $codec->encode($args[0]);
314314
}
315315

316316
break;
317317

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

322322
if (! isset($args[1])) {
323323
$args[1] = [];
@@ -333,19 +333,19 @@ private function validateOperations(array $operations, ?DocumentCodec $codec, En
333333
throw InvalidArgumentException::expectedDocumentType(sprintf('$operations[%d]["%s"][1]["collation"]', $i, $type), $args[1]['collation']);
334334
}
335335

336-
$operations[$i][$type][1] = $args[1];
336+
$operation[$type][1] = $args[1];
337337

338338
break;
339339

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

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

347347
if ($codec) {
348-
$operations[$i][$type][1] = $codec->encode($args[1]);
348+
$operation[$type][1] = $codec->encode($args[1]);
349349
}
350350

351351
if (! is_document($args[1])) {
@@ -388,19 +388,19 @@ private function validateOperations(array $operations, ?DocumentCodec $codec, En
388388
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
389389
}
390390

391-
$operations[$i][$type][2] = $args[2];
391+
$operation[$type][2] = $args[2];
392392

393393
break;
394394

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

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

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

405405
if ((! is_document($args[1]) || ! is_first_key_operator($args[1])) && ! is_pipeline($args[1])) {
406406
throw new InvalidArgumentException(sprintf('Expected update operator(s) or non-empty pipeline for $operations[%d]["%s"][1]', $i, $type));
@@ -437,7 +437,7 @@ private function validateOperations(array $operations, ?DocumentCodec $codec, En
437437
throw InvalidArgumentException::invalidType(sprintf('$operations[%d]["%s"][2]["upsert"]', $i, $type), $args[2]['upsert'], 'boolean');
438438
}
439439

440-
$operations[$i][$type][2] = $args[2];
440+
$operation[$type][2] = $args[2];
441441

442442
break;
443443

0 commit comments

Comments
 (0)