<commit_before><?php

namespace Doctrine\Tests\EventListener;

use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadata;

class CacheMetadataListener
{

    /**
     * Tracks which entities we have already forced caching enabled on. This is
     * important to avoid some potential infinite-recursion issues.
     * @var array
     */
    protected $enabledItems = array();

    /**
     * @param \Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs $event
     */
    public function loadClassMetadata(LoadClassMetadataEventArgs $event)
    {
        $metadata = $event->getClassMetadata();
        $em = $event->getObjectManager();

        /** @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
        if (strstr($metadata->name, 'Doctrine\Tests\Models\Cache')) {
            return;
        }

        if( ! $em instanceof EntityManager){
            return;
        }

        $this->enableCaching($metadata, $em);
    }

    /**
     * @param ClassMetadata $metadata
     * @param EntityManager $em
     */
    protected function enableCaching(ClassMetadata $metadata, EntityManager $em){

        if(array_key_exists($metadata->getName(), $this->enabledItems)){
            return; // Already handled in the past
        }

        $cache    = array(
            'usage' => ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE
        );

        if ($metadata->isVersioned) {
            return;
        }

        $metadata->enableCache($cache);

        $this->enabledItems[$metadata->getName()] = $metadata;

        /*
         * Only enable association-caching when the target has already been
         * given caching settings
         */
        foreach ($metadata->associationMappings as $mapping) {

            $targetMeta = $em->getClassMetadata($mapping['targetEntity']);
            $this->enableCaching($targetMeta, $em);

            if(array_key_exists($targetMeta->getName(), $this->enabledItems)){
                $metadata->enableAssociationCache($mapping['fieldName'], $cache);
            }
        }
    }
}
<commit_msg>Remove now-superfluous EntityManager check<commit_after><?php

namespace Doctrine\Tests\EventListener;

use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\ClassMetadata;

class CacheMetadataListener
{

    /**
     * Tracks which entities we have already forced caching enabled on. This is
     * important to avoid some potential infinite-recursion issues.
     * @var array
     */
    protected $enabledItems = array();

    /**
     * @param \Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs $event
     */
    public function loadClassMetadata(LoadClassMetadataEventArgs $event)
    {
        $metadata = $event->getClassMetadata();
        $em = $event->getObjectManager();

        /** @var $metadata \Doctrine\ORM\Mapping\ClassMetadata */
        if (strstr($metadata->name, 'Doctrine\Tests\Models\Cache')) {
            return;
        }

        $this->enableCaching($metadata, $em);
    }

    /**
     * @param ClassMetadata $metadata
     * @param EntityManager $em
     */
    protected function enableCaching(ClassMetadata $metadata, EntityManager $em){

        if(array_key_exists($metadata->getName(), $this->enabledItems)){
            return; // Already handled in the past
        }

        $cache    = array(
            'usage' => ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE
        );

        if ($metadata->isVersioned) {
            return;
        }

        $metadata->enableCache($cache);

        $this->enabledItems[$metadata->getName()] = $metadata;

        /*
         * Only enable association-caching when the target has already been
         * given caching settings
         */
        foreach ($metadata->associationMappings as $mapping) {

            $targetMeta = $em->getClassMetadata($mapping['targetEntity']);
            $this->enableCaching($targetMeta, $em);

            if(array_key_exists($targetMeta->getName(), $this->enabledItems)){
                $metadata->enableAssociationCache($mapping['fieldName'], $cache);
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.11889891279204257, 'fraction_numerical': 0.0, 'mean_word_length': 2.576509511993383, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '2141128', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1114, 'n_words': 230}