Skip to content

PHPLIB-1679 Call DocumentCodec::encode() only on objects #1687

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 3 commits into
base: v2.0
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
5 changes: 3 additions & 2 deletions src/Operation/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use function current;
use function is_array;
use function is_bool;
use function is_object;
use function key;
use function MongoDB\is_document;
use function MongoDB\is_first_key_operator;
Expand Down Expand Up @@ -305,7 +306,7 @@
case self::INSERT_ONE:
// $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) {
if ($codec && is_object($args[0])) {
$operations[$i][$type][0] = $codec->encode($args[0]);
}

Expand Down Expand Up @@ -340,7 +341,7 @@
throw new InvalidArgumentException(sprintf('Missing second argument for $operations[%d]["%s"]', $i, $type));
}

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

Expand Down Expand Up @@ -390,13 +391,13 @@

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

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

View workflow job for this annotation

GitHub Actions / Psalm

MixedArrayAccess

src/Operation/BulkWrite.php:394:84: MixedArrayAccess: Cannot access array value on mixed variable $args (see https://psalm.dev/051)

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]);

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

View workflow job for this annotation

GitHub Actions / Psalm

MixedArrayAccess

src/Operation/BulkWrite.php:400:95: MixedArrayAccess: Cannot access array value on mixed variable $args (see https://psalm.dev/051)

if ((! is_document($args[1]) || ! is_first_key_operator($args[1])) && ! is_pipeline($args[1])) {
throw new InvalidArgumentException(sprintf('Expected update operator(s) or non-empty pipeline for $operations[%d]["%s"][1]', $i, $type));
Expand Down
7 changes: 3 additions & 4 deletions src/Operation/FindOneAndReplace.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/Operation/FindOneAndReplace.php

View workflow job for this annotation

GitHub Actions / Psalm

UnusedBaselineEntry

src/Operation/FindOneAndReplace.php:0:0: UnusedBaselineEntry: Baseline for issue "PossiblyInvalidArgument" has 1 extra entry. (see https://psalm.dev/316)
/*
* Copyright 2015-present MongoDB, Inc.
*
Expand All @@ -25,6 +25,7 @@

use function array_key_exists;
use function is_integer;
use function is_object;
use function MongoDB\is_document;
use function MongoDB\is_first_key_operator;
use function MongoDB\is_pipeline;
Expand Down Expand Up @@ -170,11 +171,9 @@

private function validateReplacement(array|object $replacement, ?DocumentCodec $codec): array|object
{
if (isset($codec)) {
if ($codec && is_object($replacement)) {
$replacement = $codec->encode($replacement);
}

if (! is_document($replacement)) {
} elseif (! is_document($replacement)) {
throw InvalidArgumentException::expectedDocumentType('$replacement', $replacement);
}

Expand Down
9 changes: 4 additions & 5 deletions src/Operation/InsertMany.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/Operation/InsertMany.php

View workflow job for this annotation

GitHub Actions / Psalm

UnusedBaselineEntry

src/Operation/InsertMany.php:0:0: UnusedBaselineEntry: Baseline for issue "PossiblyInvalidArgument" has 1 extra entry. (see https://psalm.dev/316)
/*
* Copyright 2015-present MongoDB, Inc.
*
Expand Down Expand Up @@ -29,6 +29,7 @@

use function array_is_list;
use function is_bool;
use function is_object;
use function MongoDB\is_document;
use function sprintf;

Expand Down Expand Up @@ -189,11 +190,9 @@
}

foreach ($documents as $i => $document) {
if ($codec) {
$document = $documents[$i] = $codec->encode($document);
}

if (! is_document($document)) {
if ($codec && is_object($document)) {
$documents[$i] = $codec->encode($document);
} elseif (! is_document($document)) {
throw InvalidArgumentException::expectedDocumentType(sprintf('$documents[%d]', $i), $document);
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/Operation/InsertOne.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/Operation/InsertOne.php

View workflow job for this annotation

GitHub Actions / Psalm

UnusedBaselineEntry

src/Operation/InsertOne.php:0:0: UnusedBaselineEntry: Baseline for issue "PossiblyInvalidArgument" has 1 extra entry. (see https://psalm.dev/316)
/*
* Copyright 2015-present MongoDB, Inc.
*
Expand Down Expand Up @@ -28,6 +28,7 @@
use MongoDB\InsertOneResult;

use function is_bool;
use function is_object;
use function MongoDB\is_document;

/**
Expand Down Expand Up @@ -156,11 +157,9 @@

private function validateDocument(array|object $document, ?DocumentCodec $codec): array|object
{
if ($codec) {
if ($codec && is_object($document)) {
$document = $codec->encode($document);
}

if (! is_document($document)) {
} elseif (! is_document($document)) {
throw InvalidArgumentException::expectedDocumentType('$document', $document);
}

Expand Down
7 changes: 3 additions & 4 deletions src/Operation/ReplaceOne.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/Operation/ReplaceOne.php

View workflow job for this annotation

GitHub Actions / Psalm

UnusedBaselineEntry

src/Operation/ReplaceOne.php:0:0: UnusedBaselineEntry: Baseline for issue "PossiblyInvalidArgument" has 1 extra entry. (see https://psalm.dev/316)
/*
* Copyright 2015-present MongoDB, Inc.
*
Expand All @@ -24,6 +24,7 @@
use MongoDB\Exception\UnsupportedException;
use MongoDB\UpdateResult;

use function is_object;
use function MongoDB\is_document;
use function MongoDB\is_first_key_operator;
use function MongoDB\is_pipeline;
Expand Down Expand Up @@ -119,11 +120,9 @@

private function validateReplacement(array|object $replacement, ?DocumentCodec $codec): array|object
{
if ($codec) {
if ($codec && is_object($replacement)) {
$replacement = $codec->encode($replacement);
}

if (! is_document($replacement)) {
} elseif (! is_document($replacement)) {
throw InvalidArgumentException::expectedDocumentType('$replacement', $replacement);
}

Expand Down
Loading