Skip to content

Update function map for ext-mongodb 1.20 #3520

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

Closed
wants to merge 1 commit into from
Closed
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: 15 additions & 3 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6790,11 +6790,14 @@
'MongoDB\BSON\ObjectId::jsonSerialize' => ['mixed'],
'MongoDB\BSON\ObjectIdInterface::getTimestamp' => ['int'],
'MongoDB\BSON\ObjectIdInterface::__toString' => ['string'],
'MongoDB\BSON\PackedArray::fromJSON' => ['MongoDB\BSON\PackedArray', 'json'=>'string'],
'MongoDB\BSON\PackedArray::fromPHP' => ['MongoDB\BSON\PackedArray', 'value'=>'array'],
'MongoDB\BSON\PackedArray::get' => ['mixed', 'index'=>'int'],
'MongoDB\BSON\PackedArray::getIterator' => ['MongoDB\BSON\Iterator'],
'MongoDB\BSON\PackedArray::has' => ['bool', 'index'=>'int'],
'MongoDB\BSON\PackedArray::toPHP' => ['object|array', 'typeMap='=>'?array'],
'MongoDB\BSON\PackedArray::toCanonicalExtendedJSON' => ['string'],
'MongoDB\BSON\PackedArray::toRelaxedExtendedJSON' => ['string'],
'MongoDB\BSON\PackedArray::offsetExists' => ['bool', 'offset'=>'mixed'],
'MongoDB\BSON\PackedArray::offsetGet' => ['mixed', 'offset'=>'mixed'],
'MongoDB\BSON\PackedArray::offsetSet' => ['void', 'offset'=>'mixed', 'value'=>'mixed'],
Expand Down Expand Up @@ -6828,8 +6831,9 @@
'MongoDB\BSON\TimestampInterface::getTimestamp' => ['int'],
'MongoDB\BSON\TimestampInterface::getIncrement' => ['int'],
'MongoDB\BSON\TimestampInterface::__toString' => ['string'],
'MongoDB\BSON\UTCDateTime::__construct' => ['void', 'milliseconds='=>'DateTimeInterface|string|int|float|null'],
'MongoDB\BSON\UTCDateTime::__construct' => ['void', 'milliseconds='=>'DateTimeInterface|MongoDB\BSON\Int64|string|int|float|null'],
'MongoDB\BSON\UTCDateTime::toDateTime' => ['DateTime'],
'MongoDB\BSON\UTCDateTime::toDateTimeImmutable' => ['DateTimeImmutable'],
'MongoDB\BSON\UTCDateTime::__toString' => ['string'],
'MongoDB\BSON\UTCDateTime::serialize' => ['string'],
'MongoDB\BSON\UTCDateTime::unserialize' => ['void', 'data'=>'string'],
Expand Down Expand Up @@ -6860,7 +6864,7 @@
'MongoDB\Driver\ClientEncryption::rewrapManyDataKey' => ['object', 'filter'=>'object|array', 'options='=>'?array'],
'MongoDB\Driver\Command::__construct' => ['void', 'document'=>'object|array', 'commandOptions='=>'?array'],
'MongoDB\Driver\Cursor::current' => ['object|array|null'],
'MongoDB\Driver\Cursor::getId' => ['MongoDB\Driver\CursorId'],
'MongoDB\Driver\Cursor::getId' => ['MongoDB\Driver\CursorId|MongoDB\BSON\Int64', 'asInt64='=>'bool'],
Copy link
Contributor

@staabm staabm Oct 1, 2024

Choose a reason for hiding this comment

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

this reads like the return type would depend on the given parameter $asInt64.
if this is the case, this method would benefit from a stub, similar to e.g.

/**
* @param ($allow_string is false ? object : object|string) $object_or_class
* @param string $class
* @param bool $allow_string
* @return ($allow_string is false ? ($object_or_class is object ? bool : false) : bool)
*/
function is_subclass_of($object_or_class, string $class, $allow_string = true): bool{}

that way phpstan can know when Cursor::getId is invoked with true, it will return MongoDB\BSON\Int64

Copy link

Choose a reason for hiding this comment

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

In ext-mongodb v2.0, the return type will change to be always Int64. This parameter provides a migration path from v1.x to v2.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@staabm to elaborate on what @GromNaN said, here are the signatures in various versions of ext-mongodb:

  • Before 1.20: public function getId(): CursorId
  • 1.20 and newer: public function getId(bool $asInt64): Int64|CursorId with the conditional return type based on the value of $asInt64
  • 2.0: public function getId(): Int64

What's the preferred way for handling these signature differences in PHPStan? There are different function maps for different versions of PHP, but I don't see a mechanism similar to that. It's a problem that already exists, as when we add functionality to a new extension version, PHPStan will apply those maps even to older versions of the extension. It usually isn't a big issue with minor releases, but since 2.0 will drop some methods from the map, this would cause issues for users that continue to use the 1.x releases.

'MongoDB\Driver\Cursor::getServer' => ['MongoDB\Driver\Server'],
'MongoDB\Driver\Cursor::isDead' => ['bool'],
'MongoDB\Driver\Cursor::key' => ['?int'],
Expand All @@ -6872,7 +6876,7 @@
'MongoDB\Driver\CursorId::__toString' => ['string'],
'MongoDB\Driver\CursorId::serialize' => ['string'],
'MongoDB\Driver\CursorId::unserialize' => ['void', 'data'=>'string'],
'MongoDB\Driver\CursorInterface::getId' => ['MongoDB\Driver\CursorId'],
'MongoDB\Driver\CursorInterface::getId' => ['MongoDB\Driver\CursorId|MongoDB\BSON\Int64'],
'MongoDB\Driver\CursorInterface::getServer' => ['MongoDB\Driver\Server'],
'MongoDB\Driver\CursorInterface::isDead' => ['bool'],
'MongoDB\Driver\CursorInterface::setTypeMap' => ['void', 'typemap'=>'array'],
Expand Down Expand Up @@ -6913,9 +6917,12 @@
'MongoDB\Driver\Manager::selectServer' => ['MongoDB\Driver\Server', 'readPreference='=>'?MongoDB\Driver\ReadPreference'],
'MongoDB\Driver\Manager::startSession' => ['MongoDB\Driver\Session', 'options='=>'?array'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getCommandName' => ['string'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getDatabaseName' => ['string'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getDurationMicros' => ['int'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getError' => ['Exception'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getHost' => ['string'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getOperationId' => ['string'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getPort' => ['int'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getReply' => ['object'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getRequestId' => ['string'],
'MongoDB\Driver\Monitoring\CommandFailedEvent::getServer' => ['MongoDB\Driver\Server'],
Expand All @@ -6924,7 +6931,9 @@
'MongoDB\Driver\Monitoring\CommandStartedEvent::getCommand' => ['object'],
'MongoDB\Driver\Monitoring\CommandStartedEvent::getCommandName' => ['string'],
'MongoDB\Driver\Monitoring\CommandStartedEvent::getDatabaseName' => ['string'],
'MongoDB\Driver\Monitoring\CommandStartedEvent::getHost' => ['string'],
'MongoDB\Driver\Monitoring\CommandStartedEvent::getOperationId' => ['string'],
'MongoDB\Driver\Monitoring\CommandStartedEvent::getPort' => ['int'],
'MongoDB\Driver\Monitoring\CommandStartedEvent::getRequestId' => ['string'],
'MongoDB\Driver\Monitoring\CommandStartedEvent::getServer' => ['MongoDB\Driver\Server'],
'MongoDB\Driver\Monitoring\CommandStartedEvent::getServiceId' => ['?MongoDB\BSON\ObjectId'],
Expand All @@ -6933,8 +6942,11 @@
'MongoDB\Driver\Monitoring\CommandSubscriber::commandSucceeded' => ['void', 'event'=>'MongoDB\Driver\Monitoring\CommandSucceededEvent'],
'MongoDB\Driver\Monitoring\CommandSubscriber::commandFailed' => ['void', 'event'=>'MongoDB\Driver\Monitoring\CommandFailedEvent'],
'MongoDB\Driver\Monitoring\CommandSucceededEvent::getCommandName' => ['string'],
'MongoDB\Driver\Monitoring\CommandSucceededEvent::getDatabaseName' => ['string'],
'MongoDB\Driver\Monitoring\CommandSucceededEvent::getDurationMicros' => ['int'],
'MongoDB\Driver\Monitoring\CommandSucceededEvent::getHost' => ['string'],
'MongoDB\Driver\Monitoring\CommandSucceededEvent::getOperationId' => ['string'],
'MongoDB\Driver\Monitoring\CommandSucceededEvent::getPort' => ['int'],
'MongoDB\Driver\Monitoring\CommandSucceededEvent::getReply' => ['object'],
'MongoDB\Driver\Monitoring\CommandSucceededEvent::getRequestId' => ['string'],
'MongoDB\Driver\Monitoring\CommandSucceededEvent::getServer' => ['MongoDB\Driver\Server'],
Expand Down
Loading