PersistentCollection::removeElement() returns object or NULL instead of boolean #5071

Closed
opened 2026-01-22 14:57:42 +01:00 by admin · 2 comments
Owner

Originally created by @holtkamp on GitHub (Mar 29, 2016).

Originally assigned to: @alcaeus on GitHub.

When using strict typing in PHP 7.0.4: declare (strict_types = 1); , I noticed that an error is returned for userland code like

public function removeUser(User $user) : bool
{
    return $this->users->removeElement($user);
}

This results in an error message like:

Return value of Project\Entity::removeUser() must be of the type boolean, object returned

I had a look at all removeElement() functions used in the project and I suspect this section

Will try to come up with a test but already wanted to register this issue so I will not forget it.

UPDATE
It appears that this can be tested in ExtraLazyCollectionTest::testRemoveElementManyToMany():

        $result = $user->groups->removeElement($group);
        $this->assertTrue($result);

However, this made me wonder, why does this part returns NULL while the Collection interface indicates it should return a boolean? Is returning the element/null on purpose?

Originally created by @holtkamp on GitHub (Mar 29, 2016). Originally assigned to: @alcaeus on GitHub. When using strict typing in PHP 7.0.4: `declare (strict_types = 1);` , I noticed that an error is returned for userland code like ``` php public function removeUser(User $user) : bool { return $this->users->removeElement($user); } ``` This results in an error message like: ``` Return value of Project\Entity::removeUser() must be of the type boolean, object returned ``` I had a look at all `removeElement()` functions used in the project and I suspect [this section](https://github.com/doctrine/doctrine2/blob/b055d78ea19835fab563dfc234d4804a9d04966a/lib/Doctrine/ORM/PersistentCollection.php#L378-L380) Will try to come up with a test but already wanted to register this issue so I will not forget it. **UPDATE** It appears that this can be tested in [ExtraLazyCollectionTest::testRemoveElementManyToMany()](https://github.com/doctrine/doctrine2/blob/b055d78ea19835fab563dfc234d4804a9d04966a/tests/Doctrine/Tests/ORM/Functional/ExtraLazyCollectionTest.php#L639): ``` php $result = $user->groups->removeElement($group); $this->assertTrue($result); ``` However, this made me wonder, why does [this part](https://github.com/doctrine/doctrine2/blob/b055d78ea19835fab563dfc234d4804a9d04966a/lib/Doctrine/ORM/PersistentCollection.php#L382) returns `NULL` while the [Collection interface indicates](https://github.com/doctrine/collections/blob/392b8dd9e04c073e832fcda99270ec5a8f5e30cd/lib/Doctrine/Common/Collections/Collection.php#L98) it should return a `boolean`? Is returning the element/null on purpose?
admin added the Bug label 2026-01-22 14:57:42 +01:00
admin closed this issue 2026-01-22 14:57:42 +01:00
Author
Owner

@holtkamp commented on GitHub (Jul 12, 2017):

Anything else I can do to resolve this issue?

@holtkamp commented on GitHub (Jul 12, 2017): Anything else I can do to resolve this issue?
Author
Owner

@Ocramius commented on GitHub (Jul 13, 2017):

@holtkamp see #6550

@Ocramius commented on GitHub (Jul 13, 2017): @holtkamp see #6550
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5071