-
Couldn't load subscription status.
- Fork 266
Improve type of Collection Bulk Write operations #1694
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
Conversation
Check notice
Code scanning / Psalm
PossiblyInvalidArgument Note
Check notice
Code scanning / Psalm
MixedAssignment Note
Check notice
Code scanning / Psalm
MixedAssignment Note
Check notice
Code scanning / Psalm
PossiblyInvalidArgument Note
Check notice
Code scanning / Psalm
MixedAssignment Note
Check notice
Code scanning / Psalm
MixedAssignment Note
Check notice
Code scanning / Psalm
MixedAssignment Note
8c96884 to
f4a3b44
Compare
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.
Per our conversation yesterday, I think we still need confirmation on whether this can be utilized by the IDE. If not, is this worth adding at all?
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.
Looking at the baseline, it definitely improves the psalm analysis.
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.
PHPStorm has supports autocompletion of array keys thanks to the generic @param annotation on Collection::bulkWrite. The psalm annotation is here to improve static analysis.
f4a3b44 to
95eda6c
Compare
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.
Pull Request Overview
This PR improves type safety for MongoDB Collection bulk write operations by introducing a custom Psalm type definition for the complex array structure required by the bulkWrite() method. The change adds static analysis support to help catch type-related issues at development time.
- Introduces a detailed
OperationTypecustom type that describes all supported bulk write operation formats - Updates method signatures to use the new type annotations for better static analysis
- Refactors the validation loop to use references instead of array indexing for improved performance
- Adds a test case for empty operation validation
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Operation/BulkWrite.php | Defines the OperationType custom type and updates method signatures and validation logic |
| src/Collection.php | Imports and uses the OperationType in the bulkWrite method signature |
| tests/Operation/BulkWriteTest.php | Adds test coverage for empty operation validation |
| psalm-baseline.xml | Updates baseline to reflect improved type analysis with fewer mixed type issues |
Comments suppressed due to low confidence (1)
src/Operation/BulkWrite.php:1
- The
Documenttype is defined twice (lines 49 and 51). Remove the duplicate definition to avoid confusion and maintain clean code.
<?php
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
FWIW, I think this is a good intermediate step until we provide a better solution (e.g. by allowing to pass a MongoDB\BulkWrite instance to Collection::bulkWrite.
fb97ddd to
b37c1c4
Compare
Uh oh!
There was an error while loading. Please reload this page.
The
Collection::bulkWrite($operations)method requires a complex array structure for the operations list. Describing this type using psalm custom type feature to helps static analysis.PHPStorm is able to make the completion only with the full
@paramannotation, not@phpstan-paramnor@psaml-param. And only when I start typing the first letter.image
image
Both Psalm and PHPStan detect when the argument is invalid. The PHPStorm inspection does not detect issues when the array does not match the expected
image@paramtype.