Something like this might be easier to read :
        
```php
if ($value instanceof Collection) {
    $values = array();

    foreach ($value as $valueEntry) {
        if ( ! is_object($valueEntry) || ! $this->_em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($valueEntry))) {
            return $value;
        }

        $singleValue = $this->_em->getUnitOfWork()->getSingleIdentifierValue($valueEntry);

        if ($singleValue === null) {
            throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
        }

        $values[] = $singleValue;
    }

    return $values;
}
```