Skip to content
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

Update function map for ext-mongodb 1.20 #3520

Open
wants to merge 1 commit into
base: 1.12.x
Choose a base branch
from

Conversation

alcaeus
Copy link
Contributor

@alcaeus alcaeus commented Oct 1, 2024

This updates the function map to include methods introduced in ext-mongodb 1.20.

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants