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 all commits
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
18 changes: 4 additions & 14 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@
<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>
Expand Down Expand Up @@ -726,9 +730,6 @@
<MixedAssignment>
<code><![CDATA[$options['fields']]]></code>
</MixedAssignment>
<PossiblyInvalidArgument>
<code><![CDATA[$replacement]]></code>
</PossiblyInvalidArgument>
</file>
<file src="src/Operation/FindOneAndUpdate.php">
<MixedAssignment>
Expand All @@ -745,9 +746,6 @@
<MixedMethodCall>
<code><![CDATA[isInTransaction]]></code>
</MixedMethodCall>
<PossiblyInvalidArgument>
<code><![CDATA[$document]]></code>
</PossiblyInvalidArgument>
</file>
<file src="src/Operation/InsertOne.php">
<MixedAssignment>
Expand All @@ -759,9 +757,6 @@
<MixedMethodCall>
<code><![CDATA[isInTransaction]]></code>
</MixedMethodCall>
<PossiblyInvalidArgument>
<code><![CDATA[$document]]></code>
</PossiblyInvalidArgument>
</file>
<file src="src/Operation/ListIndexes.php">
<MixedAssignment>
Expand Down Expand Up @@ -789,11 +784,6 @@
<code><![CDATA[isInTransaction]]></code>
</MixedMethodCall>
</file>
<file src="src/Operation/ReplaceOne.php">
<PossiblyInvalidArgument>
<code><![CDATA[$replacement]]></code>
</PossiblyInvalidArgument>
</file>
<file src="src/Operation/Update.php">
<MixedArgument>
<code><![CDATA[$this->options['writeConcern']]]></code>
Expand Down
10 changes: 10 additions & 0 deletions src/Operation/BulkWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Exception\UnsupportedValueException;

use function array_is_list;
use function array_key_exists;
use function count;
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 @@ -306,6 +308,10 @@
// $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 (! is_object($args[0])) {

Check notice

Code scanning / Psalm

MixedArrayAccess Note

Cannot access array value on mixed variable $args
throw UnsupportedValueException::invalidEncodableValue($args[0]);

Check notice

Code scanning / Psalm

MixedArrayAccess Note

Cannot access array value on mixed variable $args
}

$operations[$i][$type][0] = $codec->encode($args[0]);
}

Expand Down Expand Up @@ -341,6 +347,10 @@
}

if ($codec) {
if (! is_object($args[1])) {

Check notice

Code scanning / Psalm

MixedArrayAccess Note

Cannot access array value on mixed variable $args
throw UnsupportedValueException::invalidEncodableValue($args[1]);

Check notice

Code scanning / Psalm

MixedArrayAccess Note

Cannot access array value on mixed variable $args
}

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

Expand Down
12 changes: 8 additions & 4 deletions src/Operation/FindOneAndReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Exception\UnsupportedValueException;

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 +172,13 @@ public function getCommandDocument(): array

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

if (! is_document($replacement)) {
$replacement = $codec->encode($replacement);
} elseif (! is_document($replacement)) {
throw InvalidArgumentException::expectedDocumentType('$replacement', $replacement);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Operation/InsertMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Exception\UnsupportedValueException;
use MongoDB\InsertManyResult;

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 @@ -190,10 +192,12 @@ private function validateDocuments(array $documents, ?DocumentCodec $codec): arr

foreach ($documents as $i => $document) {
if ($codec) {
$document = $documents[$i] = $codec->encode($document);
}
if (! is_object($document)) {
throw UnsupportedValueException::invalidEncodableValue($document);
}

if (! is_document($document)) {
$documents[$i] = $codec->encode($document);
} elseif (! is_document($document)) {
throw InvalidArgumentException::expectedDocumentType(sprintf('$documents[%d]', $i), $document);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/Operation/InsertOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Exception\UnsupportedValueException;
use MongoDB\InsertOneResult;

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

/**
Expand Down Expand Up @@ -157,10 +159,12 @@ private function createExecuteOptions(): array
private function validateDocument(array|object $document, ?DocumentCodec $codec): array|object
{
if ($codec) {
$document = $codec->encode($document);
}
if (! is_object($document)) {
throw UnsupportedValueException::invalidEncodableValue($document);
}

if (! is_document($document)) {
$document = $codec->encode($document);
} elseif (! is_document($document)) {
throw InvalidArgumentException::expectedDocumentType('$document', $document);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Operation/ReplaceOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedException;
use MongoDB\Exception\UnsupportedValueException;
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 @@ -120,10 +122,12 @@ public function execute(Server $server): UpdateResult
private function validateReplacement(array|object $replacement, ?DocumentCodec $codec): array|object
{
if ($codec) {
$replacement = $codec->encode($replacement);
}
if (! is_object($replacement)) {
throw UnsupportedValueException::invalidEncodableValue($replacement);
}

if (! is_document($replacement)) {
$replacement = $codec->encode($replacement);
} elseif (! is_document($replacement)) {
throw InvalidArgumentException::expectedDocumentType('$replacement', $replacement);
}

Expand Down
27 changes: 26 additions & 1 deletion tests/Collection/CodecCollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use MongoDB\Collection;
use MongoDB\Driver\BulkWrite;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Exception\UnsupportedValueException;
use MongoDB\Model\BSONDocument;
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\Tests\Fixtures\Codec\TestDocumentCodec;
Expand Down Expand Up @@ -264,6 +265,12 @@ public function testFindOneAndReplaceWithCodecAndTypemap(): void
$this->collection->findOneAndReplace(['_id' => 1], TestObject::createForFixture(1), $options);
}

public function testFindOneAndReplaceWithArray(): void
{
$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue([]));
$this->collection->findOneAndReplace(['_id' => 1], ['foo' => 'bar']);
}

public static function provideFindOptions(): Generator
{
yield 'Default codec' => [
Expand Down Expand Up @@ -413,6 +420,12 @@ public function testInsertMany($expected, $options): void
$this->assertEquals($expected, $this->collection->find([], $options)->toArray());
}

public function testInsertManyWithArray(): void
{
$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue([]));
$this->collection->insertMany([['foo' => 'bar']]);
}

public static function provideInsertOneOptions(): Generator
{
yield 'Default codec' => [
Expand Down Expand Up @@ -452,6 +465,12 @@ public function testInsertOne($expected, $options): void
$this->assertEquals($expected, $this->collection->findOne([], $options));
}

public function testInsertOneWithArray(): void
{
$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue([]));
$this->collection->insertOne(['foo' => 'bar']);
}

public static function provideReplaceOneOptions(): Generator
{
$replacedObject = TestObject::createDecodedForFixture(1);
Expand Down Expand Up @@ -499,7 +518,13 @@ public function testReplaceOneWithCodecAndTypemap(): void
];

$this->expectExceptionObject(InvalidArgumentException::cannotCombineCodecAndTypeMap());
$this->collection->replaceOne(['_id' => 1], ['foo' => 'bar'], $options);
$this->collection->replaceOne(['_id' => 1], (object) ['foo' => 'bar'], $options);
}

public function testReplaceOneWithArray(): void
{
$this->expectExceptionObject(UnsupportedValueException::invalidEncodableValue([]));
$this->collection->replaceOne(['_id' => 1], ['foo' => 'bar']);
}

/**
Expand Down