<commit_before><?php
/**
 * AnimeDb package
 *
 * @package   AnimeDb
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2011, <NAME>
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
 */

namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver;

use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver;

/**
 * File driver
 *
 * @package AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver
 * @author  <NAME> <<EMAIL>>
 */
class File implements Driver
{
    /**
     * Filename
     *
     * @var string
     */
    protected $filename;

    /**
     * List cache times
     *
     * @var array|null
     */
    protected $list = null;

    /**
     * Construct
     *
     * @param string $filename
     */
    public function __construct($filename)
    {
        $this->filename = $filename;
    }

    /**
     * Get time for key
     *
     * @param string $key
     *
     * @return \DateTime|null
     */
    public function get($key)
    {
        return clone $this->getList()[$key];
    }

    /**
     * Set time for key
     *
     * @param string $key
     * @param \DateTime $time
     *
     * @return boolean
     */
    public function set($key, \DateTime $time)
    {
        $this->getList()[$key] = clone $time;
        return true;
    }

    /**
     * Get a list of keys or dates and chooses the max date
     *
     * @param array $params
     *
     * @return \DateTime
     */
    public function getMax(array $params)
    {
        if (!$params) {
            throw new \InvalidArgumentException('Unknown key list');
        }
        foreach ($params as $key => $value) {
            if (!($value instanceof \DateTime)) {
                $params[$key] = $this->get($value);
            }
        }
        return max($params);
    }

    /**
     * Get list
     *
     * @return array
     */
    protected function & getList()
    {
        if (is_null($this->list)) {
            $this->load();
        }
        return $this->list;
    }

    /**
     * Load list
     */
    protected function load()
    {
        if (file_exists($this->filename)) {
            $this->list = include $this->filename;
            foreach ($this->list as $key => $time) {
                $this->list[$key] = new \DateTime($time);
            }
        } else {
            $this->list = [];
        }
    }

    /**
     * Save list
     */
    protected function save()
    {
        $list = [];
        /* @var $time \DateTime */
        foreach ($this->list as $key => $time) {
            $list[$key] = $time->format(\DateTime::W3C);
        }
        file_put_contents($this->filename, "<?php\nreturn ".var_export($list, true).';');
    }

    /**
     * Destruct
     */
    public function __destruct()
    {
        $this->save();
    }
}<commit_msg>save list if need<commit_after><?php
/**
 * AnimeDb package
 *
 * @package   AnimeDb
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2011, <NAME>
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
 */

namespace AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver;

use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver;

/**
 * File driver
 *
 * @package AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Driver
 * @author  <NAME> <<EMAIL>>
 */
class File implements Driver
{
    /**
     * Filename
     *
     * @var string
     */
    protected $filename;

    /**
     * List cache times
     *
     * @var array|null
     */
    protected $list = null;

    /**
     * List is save
     *
     * @var boolean
     */
    protected $save = false;

    /**
     * Construct
     *
     * @param string $filename
     */
    public function __construct($filename)
    {
        $this->filename = $filename;
    }

    /**
     * Get time for key
     *
     * @param string $key
     *
     * @return \DateTime|null
     */
    public function get($key)
    {
        return clone $this->getList()[$key];
    }

    /**
     * Set time for key
     *
     * @param string $key
     * @param \DateTime $time
     *
     * @return boolean
     */
    public function set($key, \DateTime $time)
    {
        $this->getList()[$key] = clone $time;
        $this->save = false;
        return true;
    }

    /**
     * Get a list of keys or dates and chooses the max date
     *
     * @param array $params
     *
     * @return \DateTime
     */
    public function getMax(array $params)
    {
        if (!$params) {
            throw new \InvalidArgumentException('Unknown key list');
        }
        foreach ($params as $key => $value) {
            if (!($value instanceof \DateTime)) {
                $params[$key] = $this->get($value);
            }
        }
        return max($params);
    }

    /**
     * Save list times if need
     *
     * @return boolean
     */
    public function save()
    {
        if ($this->save) {
            return true;
        }
        $list = [];
        /* @var $time \DateTime */
        foreach ($this->list as $key => $time) {
            $list[$key] = $time->format(\DateTime::W3C);
        }
        $result = file_put_contents($this->filename, "<?php\nreturn ".var_export($list, true).';');
        $this->save = $result !== false;
        return $result !== false;
    }

    /**
     * Get list
     *
     * @return array
     */
    protected function & getList()
    {
        if (is_null($this->list)) {
            $this->load();
        }
        return $this->list;
    }

    /**
     * Load list
     */
    protected function load()
    {
        if (file_exists($this->filename)) {
            $this->list = include $this->filename;
            foreach ($this->list as $key => $time) {
                $this->list[$key] = new \DateTime($time);
            }
        } else {
            $this->list = [];
        }
        $this->save = true;
    }

    /**
     * Destruct
     */
    public function __destruct()
    {
        $this->save();
    }
}
{'fraction_non_alphanumeric': 0.15111185983827494, 'fraction_numerical': 0.0026954177897574125, 'mean_word_length': 1.5959772627896809, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 61, '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': '2014403', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1797, 'n_words': 409}