<commit_before><?php

declare(strict_types=1);

namespace Brick\App\ObjectPacker;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Proxy\Proxy;

/**
 * Doctrine implementation of the ObjectPacker.
 */
class DoctrinePacker implements ObjectPacker
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    /**
     * Class constructor.
     *
     * @param \Doctrine\ORM\EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * {@inheritdoc}
     */
    public function pack($object) : ?PackedObject
    {
        $uow = $this->em->getUnitOfWork();

        if (! $uow->isInIdentityMap($object)) {
            return null;
        }

        $identity = $uow->getEntityIdentifier($object);

        if (count($identity) === 0) {
            return null;
        }

        return new PackedObject($this->getClass($object), $identity);
    }

    /**
     * {@inheritdoc}
     */
    public function unpack(PackedObject $packedObject)
    {
        return $this->em->getReference($packedObject->getClass(), $packedObject->getData());
    }

    /**
     * @param object $entity
     *
     * @return string
     */
    private function getClass($entity) : string
    {
        return ($entity instanceof Proxy) ? get_parent_class($entity) : get_class($entity);
    }
}<commit_msg>Skip transient (non-entity) classes in DoctrinePacker::unpack()<commit_after><?php

declare(strict_types=1);

namespace Brick\App\ObjectPacker;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Proxy\Proxy;

/**
 * Doctrine implementation of the ObjectPacker.
 */
class DoctrinePacker implements ObjectPacker
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    /**
     * Class constructor.
     *
     * @param \Doctrine\ORM\EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * {@inheritdoc}
     */
    public function pack($object) : ?PackedObject
    {
        $uow = $this->em->getUnitOfWork();

        if (! $uow->isInIdentityMap($object)) {
            return null;
        }

        $identity = $uow->getEntityIdentifier($object);

        if (count($identity) === 0) {
            return null;
        }

        return new PackedObject($this->getClass($object), $identity);
    }

    /**
     * {@inheritdoc}
     */
    public function unpack(PackedObject $packedObject)
    {
        $class = $packedObject->getClass();

        if ($this->em->getMetadataFactory()->isTransient($class)) {
            return null;
        }

        return $this->em->getReference($class, $packedObject->getData());
    }

    /**
     * @param object $entity
     *
     * @return string
     */
    private function getClass($entity) : string
    {
        return ($entity instanceof Proxy) ? get_parent_class($entity) : get_class($entity);
    }
}
{'fraction_non_alphanumeric': 0.14354066985645933, 'fraction_numerical': 0.001367053998632946, 'mean_word_length': 2.2777155655095185, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4805546', 'n_tokens_mistral': 895, 'n_tokens_neox': 876, 'n_words': 165}