<commit_before><?php
/*
 * This file is part of Graze DAL
 *
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @see  http://github.com/graze/dal/blob/master/LICENSE
 */
namespace Graze\Dal\Adapter\ActiveRecord;

use Graze\Dal\Adapter\ActiveRecordAdapter;
use SplObjectStorage;

class UnitOfWork
{
    const POLICY_IMPLICIT = 0;
    const POLICY_EXPLICIT = 1;

    protected $adapter;
    protected $config;
    protected $mappers = [];
    protected $persisted;
    protected $persisters = [];
    protected $records = [];
    protected $trackingPolicy;

    /**
     * @param ActiveRecordAdapter $adapter
     * @param ConfigurationInterface $config
     * @param integer $trackingPolicy
     */
    public function __construct(
        ActiveRecordAdapter $adapter,
        ConfigurationInterface $config,
        $trackingPolicy = self::POLICY_IMPLICIT
    ) {
        $this->adapter = $adapter;
        $this->config = $config;
        $this->trackingPolicy = (integer) $trackingPolicy;

        $this->persisted = new SplObjectStorage();
    }

    /**
     * @param object $entity
     */
    public function commit($entity = null)
    {
        foreach ($this->persisted as $persisted) {
            if (!$entity || $entity === $persisted) {
                $this->getPersisterByEntity($persisted)->save($persisted);

                if ($entity) {
                    $this->persisted->detach($persisted);
                    return;
                }
            }
        }

        $this->persisted->removeAll($this->persisted);
    }

    /**
     * @param object $entity
     */
    public function persist($entity)
    {
        $this->persisted->attach($entity);
    }

    /**
     * @param object $entity
     */
    public function refresh($entity)
    {
        $this->getPersisterByEntity($entity)->refresh($entity);
    }

    /**
     * @param object $entity
     */
    public function remove($entity)
    {
        $this->getPersisterByEntity($entity)->delete($entity);
    }

    /**
     * @param object $entity
     */
    public function persistByTrackingPolicy($entity)
    {
        if (self::POLICY_IMPLICIT === $this->trackingPolicy) {
            $this->persist($entity);
        }
    }

    /**
     * @param object $entity
     */
    public function getEntityRecord($entity)
    {
        $hash = $this->config->getIdentityGenerator()->generate($entity);

        return isset($this->records[$hash]) ? $this->records[$hash] : null;
    }

    /**
     * @param object $entity
     * @param object $record
     */
    public function setEntityRecord($entity, $record)
    {
        $hash = $this->config->getIdentityGenerator()->generate($entity);

        $this->records[$hash] = $record;
    }

    /**
     * @param object $entity
     * @param object $record
     */
    public function removeEntityRecord($entity)
    {
        $hash = $this->config->getIdentityGenerator()->generate($entity);

        if (isset($this->records[$hash])) {
            unset($this->records[$hash]);
        }
    }

    /**
     * @return MapperInterface
     */
    public function getMapper($name)
    {
        if (!isset($this->mappers[$name])) {
            $this->mappers[$name] = $this->config->buildMapper($name, $this);
        }

        return $this->mappers[$name];
    }

    /**
     * @return PersisterInterface
     */
    public function getPersister($name)
    {
        if (!isset($this->persisters[$name])) {
            $this->persisters[$name] = $this->config->buildPersister($name, $this);
        }

        return $this->persisters[$name];
    }

    /**
     * @param object $entity
     * @return PersisterInterface
     */
    protected function getPersisterByEntity($entity)
    {
        return $this->getPersister($this->config->getEntityName($entity));
    }
}
<commit_msg>No need to check if the hash exists before unsetting<commit_after><?php
/*
 * This file is part of Graze DAL
 *
 * Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @see  http://github.com/graze/dal/blob/master/LICENSE
 */
namespace Graze\Dal\Adapter\ActiveRecord;

use Graze\Dal\Adapter\ActiveRecordAdapter;
use SplObjectStorage;

class UnitOfWork
{
    const POLICY_IMPLICIT = 0;
    const POLICY_EXPLICIT = 1;

    protected $adapter;
    protected $config;
    protected $mappers = [];
    protected $persisted;
    protected $persisters = [];
    protected $records = [];
    protected $trackingPolicy;

    /**
     * @param ActiveRecordAdapter $adapter
     * @param ConfigurationInterface $config
     * @param integer $trackingPolicy
     */
    public function __construct(
        ActiveRecordAdapter $adapter,
        ConfigurationInterface $config,
        $trackingPolicy = self::POLICY_IMPLICIT
    ) {
        $this->adapter = $adapter;
        $this->config = $config;
        $this->trackingPolicy = (integer) $trackingPolicy;

        $this->persisted = new SplObjectStorage();
    }

    /**
     * @param object $entity
     */
    public function commit($entity = null)
    {
        foreach ($this->persisted as $persisted) {
            if (!$entity || $entity === $persisted) {
                $this->getPersisterByEntity($persisted)->save($persisted);

                if ($entity) {
                    $this->persisted->detach($persisted);
                    return;
                }
            }
        }

        $this->persisted->removeAll($this->persisted);
    }

    /**
     * @param object $entity
     */
    public function persist($entity)
    {
        $this->persisted->attach($entity);
    }

    /**
     * @param object $entity
     */
    public function refresh($entity)
    {
        $this->getPersisterByEntity($entity)->refresh($entity);
    }

    /**
     * @param object $entity
     */
    public function remove($entity)
    {
        $this->getPersisterByEntity($entity)->delete($entity);
    }

    /**
     * @param object $entity
     */
    public function persistByTrackingPolicy($entity)
    {
        if (self::POLICY_IMPLICIT === $this->trackingPolicy) {
            $this->persist($entity);
        }
    }

    /**
     * @param object $entity
     */
    public function getEntityRecord($entity)
    {
        $hash = $this->config->getIdentityGenerator()->generate($entity);

        return isset($this->records[$hash]) ? $this->records[$hash] : null;
    }

    /**
     * @param object $entity
     * @param object $record
     */
    public function setEntityRecord($entity, $record)
    {
        $hash = $this->config->getIdentityGenerator()->generate($entity);

        $this->records[$hash] = $record;
    }

    /**
     * @param object $entity
     * @param object $record
     */
    public function removeEntityRecord($entity)
    {
        $hash = $this->config->getIdentityGenerator()->generate($entity);
        unset($this->records[$hash]);
    }

    /**
     * @return MapperInterface
     */
    public function getMapper($name)
    {
        if (!isset($this->mappers[$name])) {
            $this->mappers[$name] = $this->config->buildMapper($name, $this);
        }

        return $this->mappers[$name];
    }

    /**
     * @return PersisterInterface
     */
    public function getPersister($name)
    {
        if (!isset($this->persisters[$name])) {
            $this->persisters[$name] = $this->config->buildPersister($name, $this);
        }

        return $this->persisters[$name];
    }

    /**
     * @param object $entity
     * @return PersisterInterface
     */
    protected function getPersisterByEntity($entity)
    {
        return $this->getPersister($this->config->getEntityName($entity));
    }
}

{'fraction_non_alphanumeric': 0.14318124527350642, 'fraction_numerical': 0.0015124779430299975, 'mean_word_length': 2.2600657354149547, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 98, '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': '962061', 'n_tokens_mistral': 2320, 'n_tokens_neox': 2236, 'n_words': 443}