Type fix in ORM\PersistentCollection #677

Closed
wants to merge 1 commit into
from

2 participants

@michaldudek

The ORM\PersistentCollection class accepts anything as 3rd argument to its constructor while it should be either array or Collection object. The $this->coll phpdoc says that it's a Collection while the constructor's phpdoc says it's an array.

The class assumes Collection but there isn't any type check at all. I have added conversion of array into ArrayCollection in the constructor as in some (very unclear) cases it likes to die with a fatal error:

Fatal error: Call to a member function add() on a non-object in Doctrine/ORM/PersistentCollection.php on line 169

@Ocramius
Doctrine member

@michaldudek PersistentCollection is an internal class that is instantiated only with collections as parameters - you should probably typehint Doctrine\Common\Collections\Collection on it and that's it

@michaldudek

Well, for some reason it's instantiated with arrays as well:

Catchable Fatal Error: Argument 3 passed to Doctrine\ORM\PersistentCollection::__construct() must implement interface Doctrine\Common\Collections\Collection, array given, called in /[...]vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/ObjectHydrator.php on line 182 and defined in /[...]vendor/doctrine/orm/lib/Doctrine/ORM/PersistentCollection.php line 122

This is what happens when I typehint Collection.

@Ocramius
Doctrine member

@michaldudek maybe initialized a collection as array instead of ArrayCollection somewhere - doesn't need to be fixed here, rather than in the hydrator

@michaldudek

OK, found it indeed somewhere else... A setter in the entity that got an array built manually...

Anyhow, would be good to have the typehint in the constuctor here indeed, even if only to get a little bit nicer debugging (catchable fatal error) to get backtrace rather than just the script dying with fatal error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment