-
-
Notifications
You must be signed in to change notification settings - Fork 191
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
should the return type of ReadableCollection:filter() & ReadableCollection:map() not be static ? #372
Comments
I don't think that the interface should force implementations to return an instance of the same class which is what |
that's the main point really... maybe an union type |
So let's fix that. PR welcome.
Which is essentially the same as |
|
because the return type in step 2 is a |
Well, a PersistentCollection will definitely not return Anything you add in that collection obtained from the filtering would not be added to the original collection (that's also true when using an ArrayCollection as the original one btw). So I fail to see what is the use case for adding elements in a collection being filtered. |
@stof Doctrine MongoDB Bundle uses Of course those collections are not meant to replace the original persistent collections, as the filter operations always returns a new instance. But this way you can grab a persistent collection, filter it, add some items and persist them again (via an explicit operation of course). All the code is still working technically, but breaking in type-checks with collections v2 because of Btw. the same applies to Maybe it is sufficient to add the return types in the Collection class? /**
* ...
* @method Collection<TKey, T> filter(Closure $p)
* @method Collection<TKey, T> map(Closure $p)
*/
interface Collection extends ReadableCollection, ArrayAccess
{ |
From what I gathered the main issue was removing the Add that to the fact that the implementing classes ( /**
* {@inheritDoc}
*/ which then means that they are now inheriting the docs from the How to solve that? I'd say the best solution would be to explicitly declare the return type in the implementing classes and not just inherit whatever comes by. And in the declaring classes one could use Shall I prepare a PR? In the meantime I have solved the issue I got with PHPStan in my code like this: /**
* @return Collection<array-key, SubEntity>
*/
public function getCollection(): Collection
{
/** @var Collection<array-key, SubEntity> $f */
$f = $this->subEntity->filter(function(SubEntity|null $subEntity = null) {
if (! $subEntity instanceof SubEntity) {
return false;
}
return true;
});
return $f;
} I'd love to avoid this hacky temporary variable but it solves the issue. |
Actually, in the 3.x branch the implementation of collections/src/AbstractLazyCollection.php Lines 180 to 191 in bdd8b62
whereas the implementation of collections/src/ArrayCollection.php Lines 315 to 324 in bdd8b62
Should their doc-block not be kinda the same? |
Well, ArrayCollection gives an extra guarantee that filtering it still returns an ArrayCollection. |
We could in the Collection Interface. But we can for sure in the |
Well, Collection could enforce returning |
@heiglandreas filtering an AbstractLazyCollection does not return an AbstractLazyCollection most of the time but an ArrayCollection. |
It will definitely return a |
@heiglandreas and that is not what |
I was not explicitly talking about |
hi,
This is more a question than an issue...
Got the feeling the return types of
ReadableCollection:filter()
&ReadableCollection:map()
should bestatic
instead of the currentReadableCollection
This is a simplified version of what I'm doing:
And PhpStan complains about this.... because, as it is,
$collection->filter()
will return aReadableCollection
not aCollection
, which is obviously what is to be expected IMHO.By the way, when looking at the implementation of
ArrayCollection
, the defined return type isstatic
.But I might be completely wrong about this...
Therefor asking the question before bluntly stating this is an issue...
quite possible the return type might need a change for other methods, did not take the time to review everything...
would definitely submit a PR if the above question is indeed an issue
The text was updated successfully, but these errors were encountered: