-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: Collection Management
-
None
-
None
-
PHP Drivers
-
None
-
None
-
None
-
None
-
None
-
None
PHPLIB-1970 removed the misleading @deprecated annotations from getCappedMax(), getCappedSize() and isCapped(). Those helpers stay valuable because they are typed and avoid the undefined-key pitfall of getOptions()['capped'] ?? false.
The same need applies to the other options returned by listCollections (the options accepted by createCollection), and to the info section. This ticket proposes adding typed accessors to MongoDB\Model\CollectionInfo.
Proposed methods
Typed option getters
| Method | Source | Notes |
|---|---|---|
| getValidator(): ?array | options.validator | |
| getValidationLevel(): ?string | options.validationLevel | off, strict, moderate |
| getValidationAction(): ?string | options.validationAction | error, warn |
| getCollation(): ?array | options.collation | |
| getStorageEngine(): ?array | options.storageEngine | |
| getIndexOptionDefaults(): ?array | options.indexOptionDefaults | |
| getTimeseries(): ?array | options.timeseries | |
| getExpireAfterSeconds(): ?int | options.expireAfterSeconds | TTL |
| getClusteredIndex(): ?array | options.clusteredIndex | |
| getEncryptedFields(): ?array | options.encryptedFields | CSFLE |
| getChangeStreamPreAndPostImages(): ?array | options.changeStreamPreAndPostImages |
Boolean helpers
- isTimeseries(): bool
- isClustered(): bool
- isEncrypted(): bool
- hasValidator(): bool
Info-section getters
- getUuid(): ?MongoDB\BSON\Binary for info.uuid
Typed array shape for options
Document the options document with a Psalm/PHPStan array shape, so static analysis knows the exact keys and types and the undefined-key pitfall is caught at the type level:
/** * @return array{ * capped?: bool, * size?: int, * max?: int, * validator?: array, * validationLevel?: string, * validationAction?: string, * collation?: array, * storageEngine?: array, * indexOptionDefaults?: array, * timeseries?: array, * expireAfterSeconds?: int, * clusteredIndex?: array, * encryptedFields?: array, * changeStreamPreAndPostImages?: array, * } */ public function getOptions(): array
Compatibility
- Adding methods is not a breaking change, so this can ship in a minor release.
- CollectionInfo is not meant to be extended, so the risk of a subclass name collision is low.
- An alternative is a generic getOption(string $name, mixed $default = null): mixed, but it loses the type safety that motivates the existing helpers.
- Changing getOptions() to return a typed value object would be a breaking change and would need a major release.
Acceptance criteria
- The methods above are added to CollectionInfo.
- getOptions() documents its return value with an array shape.
- Each method is covered by a unit test in tests/Model/CollectionInfoTest.php, including the absent-key case.
- Documentation is updated.
Related to PHPLIB-1970.
- related to
-
PHPLIB-1970 Remove deprecation of capped collection helpers in CollectionInfo
-
- In Progress
-