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
PHPLIB-1122: Support Document and PackedArray objects in public APIs #1077
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a lot of tests being added, and it looks like you've covered all of the new use cases for packed arrays and documents. I left a few suggestions that should also help with getting psalm to play nice with these changes.
/* Nested documents and arrays are intentionally left as BSON. We avoid | ||
* iterator_to_array() since Document and PackedArray iteration returns | ||
* all values as MongoDB\BSON\Value instances. */ | ||
return $document->toPHP([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember playing around with psalm return types based on the $typeMap
parameter, but IIRC there were some issues comparing against the array keys. I've created PHPC-2227 to track adding stubs to psalm to ensure Psalm understands these classes.
In the meantime, consider adding these two errors to the baseline or storing the result of the toPHP
call and using @var
to let Psalm know that this will be an array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I found a few more errors as well and fixed all but one (see the commit for the regenerated baseline).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take my approval for what it is worth. SGTM.
51ba28d
to
80d88d5
Compare
Excluding tests, most changes were concentrated in functions.php. A document_to_array() function was introduced to make it easier to access document values as arrays. This is mainly used by other utility functions (e.g. checking for dollar prefixed keys) but also ended up being used in the Find operation for handling the "modifiers" option. Adds tests for using Document and PackedArray objects in operations. Beyond handling of document/array types, this also filled gaps in test coverage for: * Validation for IndexInput key/name options * Passing a MongoDB\Driver\Command directly to DatabaseCommand * is_last_pipeline_operator_write()
Processing a Document and PackedArray will yield an array, so we can avoid a redundant Serializable check by using elseif.
…operator() This introduces a dependency on symfony/polyfill-php73 and bumps both polyfill packages to the latest minor version for consistency. The doc block for is_first_key_operator() was revised to note its additional purpose for validating pipeline stages.
This suppresses a new error in BulkWrite pertaining to a variable previously checked by is_first_key_operator() and is_pipeline(). ``` ERROR: MixedArgument - src/Operation/BulkWrite.php:341:45 - Argument 2 of MongoDB\Driver\BulkWrite::update cannot be mixed, expecting array<array-key, mixed>|object (see https://psalm.dev/030) $bulk->update($args[0], $args[1], $args[2]); The type of $args[1] is sourced from here - vendor/vimeo/psalm/stubs/CoreGenericFunctions.phpstub:154:12 * @return (TArray is array<empty,empty> ? false : (TArray is non-empty-array ? TValue : TValue|false)) ``` Other changes to the baseline are removals for obsolete errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I've rebased this PR on master, and as long as the psalm check is happy, I'm happy. While the baseline contains lots of changes, I believe most of those are from a previous generation on PHP 8+ (I believe I last regenerated the baseline on PHP 8.2).
CI failures are related to CSFLE QEv2 protocol changes, and two timeouts in CSFLE tests on Debian 9.2 (unrelated to this PR). I'll proceed with merging. |
https://jira.mongodb.org/browse/PHPLIB-1122
Excluding tests, most changes were concentrated in functions.php. A document_to_array() function was introduced to make it easier to access document values as arrays. This is mainly used by other utility functions (e.g. checking for dollar prefixed keys) but also ended up being used in the Find operation for handling the "modifiers" option.
Adds tests for using Document and PackedArray objects in operations.
Beyond handling of document/array types, this also filled gaps in test coverage for:
Addressed
Most changes were made to internal functions, which are used by many operations/classes. The following is a summary of test coverage changes in FunctionsTest:
document_to_array()
: various types for$document
argument. This is used by many other internal functions and the Find operation.is_first_key_operator()
: various types for$document
argument. This is used by operations that peform updates (e.g. BulkWrite, FindOneAndUpdate, Update).is_last_pipeline_operator_write()
: testing more inputs and using a variety of types for stage documents. This is used by Aggregate (operation and helpers) and indirectly by the Watch operation.is_mapreduce_output_inline()
: various types for$out
argument. This is used by MapReduce.is_pipeline()
: various types for$pipeline
argument and stage documents within. This is used for validating update values forupdate
andfindAndModify
commands.The following is a summary of added test coverage for various operations/classes. When an argument/option is listed without additional details, that means we've added test coverage for expressing the document in various forms (i.e. array, stdClass, Serializable, Document):
$filter
$filter
key
option. Additionally filled gaps in test coverage for validatingkey
andname
options (unrelated to documents)key
optionname
option fromkey
option$command
. Additionally added test coverage for passing aMongoDB\Driver\Command
object constructed from various document types.$filter
$filter
$filter
$filter
,modifiers
option$filter
,modifiers
optionfilter
andquery
options$filter
$filter
,$replacement
$filter
,$update
$replacement
validation$update
validation$documents
elements and_id
generation/access$document
and_id
generation/access$replacement
validation$update
validation based onmulti
option$filter
,$update
$filter
,$replacement
$filter
,$update
$filter
,$update
$update
validation$update
validationOmitted
The following are notable parts of PHPLIB that were not touched by this PR:
apply_type_map_to_document()
: only used in GridFS on stdClass objects