Skip to content
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

Improve query and aggregator classes #2702

Open
wants to merge 2 commits into
base: 2.10.x
Choose a base branch
from

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Nov 28, 2024

Q A
Type improvement
BC Break no
Fixed issues Fixes #2699 (cc @soyuka)

Summary

To improve handling of query and aggregation classes, a new joint IterableResult interface is introduced. This method includes the execute, getIterator, and getSingleResult methods that are common to both queries and aggregations. In addition, this allows us to keep the actual classes final, while allowing other platforms to mock what's returned from the builders for testing purposes.

Note that while the return type of getAggregation and getQuery in the builders has widened, I've added an @return statement to indicate that the methods themselves still return the same class instances that they returned previously. However, changing the return type is necessary to allow the mocking described above. So while this is technically a BC break, in practice it has no effect, especially as any class extending the builders would still be allowed to use the narrower return type.

@alcaeus alcaeus requested a review from GromNaN November 28, 2024 09:26
@alcaeus alcaeus self-assigned this Nov 28, 2024
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

I think the issue it that Aggregation\Builder::execute() should not be deprecated. This was the perfect method for a mock.
As you can see in GromNaN/api-platform-core#2, the alternative is more verbose, that's not good for DX.

With this solution, you have to rely on the phpdoc to give the exact return type, which is a regression. Can we revisite #2211 instead?

@@ -246,8 +247,10 @@ public function geoNear($x, $y = null): Stage\GeoNear
* Returns an aggregation object for the current pipeline
*
* @param array<string, mixed> $options
*
* @return Aggregation
Copy link
Member

Choose a reason for hiding this comment

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

Noted that this is necessary for static analysis and autocompletion, but we don't use it as return type to allow mocking more easily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Aggregation class is final and can't be mocked to mock Aggregation\Builder
2 participants