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

Use CursorInterface type instead of Cursor #2546

Closed
wants to merge 1 commit into from

Conversation

soyuka
Copy link
Contributor

@soyuka soyuka commented Aug 9, 2023

Q A
Type improvement
BC Break no
Fixed issues #2395

Summary

I still have the same issue, I want to move from execute() to getAggregation()->getIterator() but this class is completely locked in and we have no way to unit test this:

https://github.com/api-platform/core/blob/92a81f024541054b9322e7457b75c721261e14e0/src/Doctrine/Odm/State/ItemProvider.php#L74

The assertion above is fine I can disable it, but in fact I wonder why you wouldn't do instanceof CursorInterface, what if someone implements another Cursor? We can't use the Aggregation type, nor the AggregationBuilder (as the signature of getAggregation forces the use of the final class Aggregation) nor the DocumentRepository, which forces the use of the AggregationBuilder...

Can you help providing a solution for library maintainers? Thanks!

@malarzm
Copy link
Member

malarzm commented Aug 9, 2023

The assertion above is fine I can disable it, but in fact I wonder why you wouldn't do instanceof CursorInterface, what if someone implements another Cursor?

My guess is that at the time a Cursor was the only thing the underlying library could have returned. But that'd be best confirmed by somebody with proper driver/library knowledge like @alcaeus or @jmikola :)

@soyuka
Copy link
Contributor Author

soyuka commented Aug 10, 2023

Actually I'd love to change the assert as well:

https://github.com/doctrine/mongodb-odm/blob/d3c7dce09d5a49989e1f216b93b70cd67d43de61/lib/Doctrine/ODM/MongoDB/Aggregation/Aggregation.php#L57C1-L59

lmk if these changes are possible, thanks.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

My guess is that at the time a Cursor was the only thing the underlying library could have returned.

I'm not sure how much this question pertains to the Aggregate operation itself, but I can point out that it historically returned a Traversable. That was recently changed in mongodb/mongo-php-library@5aff53f since a non-cursor response is no longer possible on MongoDB 3.6+ (required by the current driver/library version).

Also, nothing has historically implemented CursorInterface (apart from MongoDB\Driver\Cursor), but that will change once codecs are implemented (see: mongodb/mongo-php-library#1140).

With that in mind, I think it may make sense to use CursorInterface instead of Cursor in ODM, although there's likely no rush as alternative implementations won't be returned until ODM (or any other library) starts making use of codecs, which would be a separate initiative.

@alcaeus can speak more to this once he's back from holiday next week.

@@ -58,7 +59,7 @@ public function getIterator(): Iterator
return $this->prepareIterator($cursor);
}

private function prepareIterator(Cursor $cursor): Iterator
private function prepareIterator(CursorInterface $cursor): Iterator
Copy link
Member

Choose a reason for hiding this comment

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

I will note that while Cursor does implement both CursorInterface and Iterator, CursorInterface itself is only Traversable. Depending how ODM uses the $cursor object here, this change might conflict with static analysis checks.

@alcaeus
Copy link
Member

alcaeus commented Aug 16, 2023

With that in mind, I think it may make sense to use CursorInterface instead of Cursor in ODM, although there's likely no rush as alternative implementations won't be returned until ODM (or any other library) starts making use of codecs, which would be a separate initiative.

Yes, absolutely. We haven't had a reason for making this change, but this reason is coming with the addition of codecs in PHPLIB, which will result in return values that are not MongoDB\Driver\Cursor but will offer its API. Please note that for historic reasons, CursorInterface does not implement Iterator, but the Cursor class does. The correct replacement for any Cursor type would thus be CursorInterface & Iterator.

@soyuka
Copy link
Contributor Author

soyuka commented Sep 5, 2023

I did the change, do you think that we could also get rid of the assert($cursor instanceof Cursor); ?

@alcaeus
Copy link
Member

alcaeus commented Sep 6, 2023

I did the change, do you think that we could also get rid of the assert($cursor instanceof Cursor); ?

Yes - the assertion can be removed. IIRC this was there for static analysis reasons.

@soyuka
Copy link
Contributor Author

soyuka commented Sep 6, 2023

bumped and rebased against 2.6, lmk if it needs to target something else

@divine
Copy link

divine commented Oct 10, 2023

Hello,

Can this be pushed forward and reviewed please? It’s me who reported this issue with the APP in api-platform/core#5705.

MongoDB works great for our project but this depreciation notices is a bit scary to use in a stable framework.

cc @alcaeus @jmikola @malarzm

Thanks!


return $this->prepareIterator($cursor);
}

private function prepareIterator(Cursor $cursor): Iterator
private function prepareIterator(CursorInterface&Iterator $cursor): Iterator
Copy link
Member

Choose a reason for hiding this comment

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

FWIW branch 2.6.x requires php ^8.0 while this is valid starting with 8.1. But maybe we could bump requirement to 8.1, 8.0 usage in 2.5.x is rather minimal: https://packagist.org/packages/doctrine/mongodb-odm/php-stats#2.5

Copy link
Member

Choose a reason for hiding this comment

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

No objections to bumping to PHP 8.1 from my end.

Copy link
Member

Choose a reason for hiding this comment

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

Bumped in #2556

@jmikola jmikola requested a review from alcaeus October 10, 2023 22:10
@alcaeus
Copy link
Member

alcaeus commented Oct 11, 2023

The change itself looks good, but static analysis is not too happy. We might have to leave the assertions in place to quiet down PHPStan.

@malarzm malarzm mentioned this pull request Oct 23, 2023
@soyuka
Copy link
Contributor Author

soyuka commented Oct 25, 2023

sure, should I update this?

@alcaeus
Copy link
Member

alcaeus commented Nov 8, 2023

@soyuka I fixed issues, but since you used the 2.5.x branch in your fork I didn't want to force-push to it. I created #2579 instead.

@alcaeus alcaeus closed this Nov 8, 2023
@alcaeus alcaeus removed their request for review November 8, 2023 12:50
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.

5 participants