<commit_before><?php
class PHP_Depend_Util_Cache_Driver_File implements PHP_Depend_Util_Cache_Driver
{
    protected $cacheDir = null;

    public function __construct($cacheDir = '/tmp/pdepend-playground')
    {
        if (false === file_exists($cacheDir)) {
            mkdir($cacheDir, 0775, true);
        }
        $this->cacheDir = $cacheDir;
    }

    public function store($key, $data, $hash = null)
    {
        $file = "{$this->cacheDir}/{$key}.cache";
        file_put_contents($file, serialize(array('hash' => $hash, 'data' => $data)));
    }

    public function restore($key, $hash = null)
    {
        $file = "{$this->cacheDir}/{$key}.cache";
        if (file_exists($file)) {
            return $this->restoreFile($file, $hash);
        }
        return null;
    }

    protected function restoreFile($file, $hash)
    {
        $data = unserialize(file_get_contents($file));
        if ($data['hash'] === $hash) {
            return $data['data'];
        }
        return null;
    }
}<commit_msg># Separate concerns, IO and validation related code.<commit_after><?php
class PHP_Depend_Util_Cache_Driver_File implements PHP_Depend_Util_Cache_Driver
{
    protected $cacheDir = null;

    public function __construct($cacheDir = '/tmp/pdepend-playground')
    {
        if (false === file_exists($cacheDir)) {
            mkdir($cacheDir, 0775, true);
        }
        $this->cacheDir = $cacheDir;
    }

    public function store($key, $data, $hash = null)
    {
        $file = "{$this->cacheDir}/{$key}.cache";
        $this->write($file, serialize(array('hash' => $hash, 'data' => $data)));
    }

    protected function write($file, $data)
    {
        $handle = fopen($file, 'wb');
        flock($handle, LOCK_EX);
        fwrite($handle, $data);
        flock($handle, LOCK_UN);
        fclose($handle);
    }

    public function restore($key, $hash = null)
    {
        $file = "{$this->cacheDir}/{$key}.cache";
        if (file_exists($file)) {
            return $this->restoreFile($file, $hash);
        }
        return null;
    }

    protected function restoreFile($file, $hash)
    {
        $data = unserialize($this->read($file));
        if ($data['hash'] === $hash) {
            return $data['data'];
        }
        return null;
    }

    protected function read($file)
    {
        $handle = fopen($file, 'rb');
        flock($handle, LOCK_EX);

        $data = fread($handle, filesize($file));

        flock($handle, LOCK_UN);
        fclose($handle);

        return $data;
    }
}
{'fraction_non_alphanumeric': 0.16193853427895982, 'fraction_numerical': 0.0031520882584712374, 'mean_word_length': 2.3718459495351927, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '5433743', 'n_tokens_mistral': 799, 'n_tokens_neox': 777, 'n_words': 157}