public function registerEntity(&$entity)
    {
        $this->unstashEntity($entity);

        $className    = $this->getClassName($entity);
        $objectHashId = $this->getObjectHashId($entity);
        $entityId     = $this->getEntityId($entity);

        if (!isset($this->entities[$className])) {
            $this->entities[$className] = [];
        }

        if (isset($this->entities[$className][$entityId])) {
            // Change the reference of the input entity to the one that's already registered
            $entity = $this->entities[$className][$entityId];
        } else {
            // Register this entity
            $this->changeTracker->startTracking($entity);
            $this->entities[$className][$entityId] = $entity;
            $this->entityStates[$objectHashId]     = EntityStates::REGISTERED;
        }
    }