Fix return type of compound search operators#2836
Conversation
| use MongoDB\BSON\ObjectId; | ||
| use MongoDB\BSON\UTCDateTime; | ||
|
|
||
| interface CompoundSearchOperatorInterface extends SupportsCompoundableSearchOperators |
There was a problem hiding this comment.
If we don't want to repeat all the methods, we could make SupportsCompoundableSearchOperators generic and have a return type declared as Autocomplete&T. But that would not enforce the type as we can do with concrete return types.
There was a problem hiding this comment.
That would mean an @extends tag for each interface implemented by SupportsAutocompleteOperator, which is worse.
/**
* @template T of object
* @extends SupportsAutocompleteOperator<T>
* @extends SupportsEmbeddedDocumentOperator<T>
* @extends SupportsEqualsOperator<T>
* @extends SupportsExistsOperator<T>
* @extends SupportsGeoShapeOperator<T>
* @extends SupportsGeoWithinOperator<T>
* @extends SupportsMoreLikeThisOperator<T>
* @extends SupportsNearOperator<T>
* @extends SupportsPhraseOperator<T>
* @extends SupportsQueryStringOperator<T>
* @extends SupportsRangeOperator<T>
* @extends SupportsRegexOperator<T>
* @extends SupportsTextOperator<T>
* @extends SupportsWildcardOperator<T>
*/
interface SupportsCompoundableSearchOperators extends SupportsAutocompleteOperator, SupportsEmbeddedDocumentOperator, SupportsEqualsOperator, SupportsExistsOperator, SupportsGeoShapeOperator, SupportsGeoWithinOperator, SupportsMoreLikeThisOperator, SupportsNearOperator, SupportsPhraseOperator, SupportsQueryStringOperator, SupportsRangeOperator, SupportsRegexOperator, SupportsTextOperator, SupportsWildcardOperator
{
}/** @template T of object */
interface SupportsAutocompleteOperator
{
/** @return Autocomplete&T */
public function autocomplete(string $path = '', string ...$query): Autocomplete;
}There was a problem hiding this comment.
While I would typically go for a templated interface, I agree that the list of @extends annotations looks ugly. In that case, I'd rather update the method declarations in this interface, especially since we now have the ability to define the return types natively.
| use MongoDB\BSON\ObjectId; | ||
| use MongoDB\BSON\UTCDateTime; | ||
|
|
||
| interface CompoundSearchOperatorInterface extends SupportsCompoundableSearchOperators |
There was a problem hiding this comment.
While I would typically go for a templated interface, I agree that the list of @extends annotations looks ugly. In that case, I'd rather update the method declarations in this interface, especially since we now have the ability to define the return types natively.
Summary
When
Compound::autocomplete()returnsAutocomplete&CompoundSearchOperatorInterfaceit inherits from the methods ofCompoundSearchOperatorInterface. So the methods for search operator must specify that a the returned operator is also an instance ofCompoundSearchOperatorInterface.