Skip to content

Fix return type of compound search operators#2836

Merged
GromNaN merged 2 commits intodoctrine:2.12.xfrom
GromNaN:PHPORM-395
Sep 26, 2025
Merged

Fix return type of compound search operators#2836
GromNaN merged 2 commits intodoctrine:2.12.xfrom
GromNaN:PHPORM-395

Conversation

@GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 25, 2025

Q A
Type bug
BC Break no
Fixed issues PHPORM-395

Summary

When Compound::autocomplete() returns Autocomplete&CompoundSearchOperatorInterface it inherits from the methods of CompoundSearchOperatorInterface. So the methods for search operator must specify that a the returned operator is also an instance of CompoundSearchOperatorInterface.

use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;

interface CompoundSearchOperatorInterface extends SupportsCompoundableSearchOperators
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member Author

@GromNaN GromNaN Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@GromNaN GromNaN changed the base branch from 2.13.x to 2.12.x September 25, 2025 18:56
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;

interface CompoundSearchOperatorInterface extends SupportsCompoundableSearchOperators
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@GromNaN GromNaN merged commit 7abdc70 into doctrine:2.12.x Sep 26, 2025
21 checks passed
@GromNaN GromNaN deleted the PHPORM-395 branch September 26, 2025 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants