Skip to content

Fix signatures of bsonSerialize() methods #1156

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

Merged
merged 2 commits into from
Sep 8, 2023
Merged
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
5 changes: 3 additions & 2 deletions examples/persistable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use MongoDB\BSON\Persistable;
use MongoDB\Client;
use MongoDB\Model\BSONArray;
use stdClass;
use UnexpectedValueException;

use function getenv;
Expand Down Expand Up @@ -34,7 +35,7 @@ public function getId(): ObjectId
return $this->id;
}

public function bsonSerialize(): object
public function bsonSerialize(): stdClass
{
return (object) [
'_id' => $this->id,
Expand Down Expand Up @@ -73,7 +74,7 @@ public function __construct(string $type, string $address)
$this->address = $address;
}

public function bsonSerialize(): object
public function bsonSerialize(): stdClass
{
return (object) [
'type' => $this->type,
Expand Down
31 changes: 31 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@
<code><![CDATA[$clientEncryption->decrypt($document->encryptedField)]]></code>
</MixedArgument>
</file>
<file src="examples/persistable.php">
<LessSpecificReturnStatement>
<code><![CDATA[(object) [
'_id' => $this->id,
'name' => $this->name,
'emails' => $this->emails,
]]]></code>
<code><![CDATA[(object) [
'type' => $this->type,
'address' => $this->address,
]]]></code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code>stdClass</code>
<code>stdClass</code>
</MoreSpecificReturnType>
</file>
<file src="src/ChangeStream.php">
<DeprecatedConstant>
<code>self::CURSOR_NOT_FOUND</code>
Expand Down Expand Up @@ -159,6 +176,9 @@
</MixedArrayOffset>
</file>
<file src="src/Model/IndexInput.php">
<LessSpecificReturnStatement>
<code><![CDATA[(object) $this->index]]></code>
</LessSpecificReturnStatement>
<MixedArgument>
<code>$fieldName</code>
<code><![CDATA[$index['key']]]></code>
Expand All @@ -173,6 +193,17 @@
<MixedReturnStatement>
<code><![CDATA[$this->index['name']]]></code>
</MixedReturnStatement>
<MoreSpecificReturnType>
<code>stdClass</code>
</MoreSpecificReturnType>
</file>
<file src="src/Model/SearchIndexInput.php">
<LessSpecificReturnStatement>
<code><![CDATA[(object) $this->index]]></code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code>stdClass</code>
</MoreSpecificReturnType>
</file>
<file src="src/Operation/Aggregate.php">
<MixedArgument>
Expand Down
3 changes: 2 additions & 1 deletion src/Model/IndexInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use MongoDB\BSON\Serializable;
use MongoDB\Exception\InvalidArgumentException;
use stdClass;

use function is_float;
use function is_int;
Expand Down Expand Up @@ -86,7 +87,7 @@ public function __toString(): string
* @see \MongoDB\Collection::createIndexes()
* @see https://php.net/mongodb-bson-serializable.bsonserialize
*/
public function bsonSerialize(): object
public function bsonSerialize(): stdClass
{
return (object) $this->index;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Model/SearchIndexInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use MongoDB\BSON\Serializable;
use MongoDB\Exception\InvalidArgumentException;
use stdClass;

use function is_string;
use function MongoDB\is_document;
Expand Down Expand Up @@ -66,7 +67,7 @@ public function __construct(array $index)
* @see \MongoDB\Collection::createSearchIndexes()
* @see https://php.net/mongodb-bson-serializable.bsonserialize
*/
public function bsonSerialize(): object
public function bsonSerialize(): stdClass
{
return (object) $this->index;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The psalm inferred type is incorrect. $this->index is an array, so (object) $this->index cannot be anything different that stdClass.
Let's add this warnings to the baseline.

src/Model/SearchIndexInput.php:70:38: MoreSpecificReturnType: The declared return type 'stdClass' for MongoDB\Model\SearchIndexInput::bsonSerialize is more specific than the inferred return type 'object' (see https://psalm.dev/070)

Copy link
Member

@GromNaN GromNaN Sep 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Known issue: vimeo/psalm#10019

}
Expand Down