<commit_before><?php

namespace Fluoresce\DoctrineUndeletable\EventListener;

use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Fluoresce\DoctrineUndeletable\Exception\UndeletableObjectException;

/**
 * @author <NAME> <<EMAIL>>
 */
class UndeletableSubscriber implements EventSubscriber
{
    /**
     * @var string
     */
    const ANNOTATION_CLASS = 'Fluoresce\\DoctrineUndeletable\\Mapping\\Undeletable';

    /**
     * Annotation reader
     *
     * @var Reader
     */
    protected $reader;

    /**
     * @param Reader $reader
     */
    public function __construct(Reader $reader)
    {
        $this->reader = $reader;
    }

    /**
     * {@inheritdoc}
     */
    public function getSubscribedEvents()
    {
        return array(
            'onFlush',
        );
    }

    /**
     * If it’s a Undeletable object, throw an exception
     *
     * @param OnFlushEventArgs $args
     *
     * @return void
     * @throws UndeletableObjectException
     */
    public function onFlush(OnFlushEventArgs $args)
    {
        $uow = $args->getEntityManager()->getUnitOfWork();

        foreach ($uow->getScheduledEntityDeletions() as $object) {
            if ($this->classIsUndeletable(get_class($object))) {
                throw new UndeletableObjectException();
            }
        }
    }

    /**
     * Check whether a class is flagged as undeletable
     *
     * @param string $class
     *
     * @return bool
     */
    protected function classIsUndeletable($class)
    {
        static $cache;

        if (!array_key_exists($class, $cache)) {
            $refl = new \ReflectionClass($class);
            $cache['class'] = (null !== $this->reader->getClassAnnotation($refl, self::ANNOTATION_CLASS));
        }

        return $cache['class'];
    }
}<commit_msg>Fix bug in annotation cache<commit_after><?php

namespace Fluoresce\DoctrineUndeletable\EventListener;

use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Fluoresce\DoctrineUndeletable\Exception\UndeletableObjectException;

/**
 * @author <NAME> <<EMAIL>>
 */
class UndeletableSubscriber implements EventSubscriber
{
    /**
     * @var string
     */
    const ANNOTATION_CLASS = 'Fluoresce\\DoctrineUndeletable\\Mapping\\Undeletable';

    /**
     * Annotation reader
     *
     * @var Reader
     */
    protected $reader;

    /**
     * @param Reader $reader
     */
    public function __construct(Reader $reader)
    {
        $this->reader = $reader;
    }

    /**
     * {@inheritdoc}
     */
    public function getSubscribedEvents()
    {
        return array(
            'onFlush',
        );
    }

    /**
     * If it’s a Undeletable object, throw an exception
     *
     * @param OnFlushEventArgs $args
     *
     * @return void
     * @throws UndeletableObjectException
     */
    public function onFlush(OnFlushEventArgs $args)
    {
        $uow = $args->getEntityManager()->getUnitOfWork();

        foreach ($uow->getScheduledEntityDeletions() as $object) {
            if ($this->classIsUndeletable(get_class($object))) {
                throw new UndeletableObjectException();
            }
        }
    }

    /**
     * Check whether a class is flagged as undeletable
     *
     * @param string $class
     *
     * @return bool
     */
    protected function classIsUndeletable($class)
    {
        static $cache = array();

        if (!array_key_exists($class, $cache)) {
            $refl = new \ReflectionClass($class);
            $cache['class'] = (null !== $this->reader->getClassAnnotation($refl, self::ANNOTATION_CLASS));
        }

        return $cache['class'];
    }
}
{'fraction_non_alphanumeric': 0.1219448777951118, 'fraction_numerical': 0.0, 'mean_word_length': 2.359825327510917, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, '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': '4109899', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1147, 'n_words': 219}