<commit_before><?php

declare(strict_types=1);

namespace SensioLabs\Deptrac\AstRunner\AstParser\NikicPhpParser;

use PhpParser\JsonDecoder;
use SensioLabs\Deptrac\Console\Application;

class CacheableFileParser implements FileParserInterface
{
    private $parser;
    private $cache;
    private $cacheFile;
    private $loaded = false;
    private $jsonDecoder;

    public function __construct(FileParser $parser)
    {
        $this->parser = $parser;
        $this->cache = [];
        $this->cacheFile = getcwd().'/.deptrac.cache';
        $this->jsonDecoder = new JsonDecoder();
    }

    public function parse(\SplFileInfo $file): array
    {
        $this->load();

        $realPath = $file->getRealPath();
        $hash = sha1_file($realPath);

        if (isset($this->cache[$realPath]) && $hash === $this->cache[$realPath]['hash']) {
            return $this->cache[$realPath]['ast'];
        }

        $ast = $this->parser->parse($file);
        $this->cache[$realPath] = ['hash' => $hash, 'ast' => $ast];

        return $ast;
    }

    public function load(): void
    {
        if ($this->loaded) {
            return;
        }

        if (!file_exists($this->cacheFile) || !is_readable($this->cacheFile)) {
            return;
        }

        $cache = json_decode(file_get_contents($this->cacheFile), true);

        $this->loaded = true;

        if (Application::VERSION !== $cache['version']) {
            return;
        }

        $this->cache = array_map(
            function (array $data) {
                $data['ast'] = $this->jsonDecoder->decode($data['ast']);

                return $data;
            },
            $cache['payload']
        );
    }

    public function write(): void
    {
        if (!is_writable(\dirname($this->cacheFile))) {
            return;
        }

        file_put_contents(
            $this->cacheFile,
            json_encode(
                [
                    'version' => Application::VERSION,
                    'payload' => array_map(
                        function (array $data) {
                            $data['ast'] = json_encode($data['ast']);

                            return $data;
                        },
                        $this->cache
                    ),
                ]
            )
        );
    }
}<commit_msg>remove old/not parsed files from cache<commit_after><?php

declare(strict_types=1);

namespace SensioLabs\Deptrac\AstRunner\AstParser\NikicPhpParser;

use PhpParser\JsonDecoder;
use SensioLabs\Deptrac\Console\Application;

class CacheableFileParser implements FileParserInterface
{
    private $parser;
    private $cache;
    private $cacheFile;
    private $loaded = false;
    private $jsonDecoder;
    private $parsedFiles = [];

    public function __construct(FileParser $parser)
    {
        $this->parser = $parser;
        $this->cache = [];
        $this->cacheFile = getcwd().'/.deptrac.cache';
        $this->jsonDecoder = new JsonDecoder();
    }

    public function parse(\SplFileInfo $file): array
    {
        $this->load();

        $realPath = $file->getRealPath();
        $hash = sha1_file($realPath);

        $this->parsedFiles[$realPath] = true;

        if (isset($this->cache[$realPath]) && $hash === $this->cache[$realPath]['hash']) {
            return $this->cache[$realPath]['ast'];
        }

        $ast = $this->parser->parse($file);
        $this->cache[$realPath] = ['hash' => $hash, 'ast' => $ast];

        return $ast;
    }

    public function load(): void
    {
        if ($this->loaded) {
            return;
        }

        if (!file_exists($this->cacheFile) || !is_readable($this->cacheFile)) {
            return;
        }

        $cache = json_decode(file_get_contents($this->cacheFile), true);

        $this->loaded = true;

        if (Application::VERSION !== $cache['version']) {
            return;
        }

        $this->cache = array_map(
            function (array $data) {
                $data['ast'] = $this->jsonDecoder->decode($data['ast']);

                return $data;
            },
            $cache['payload']
        );
    }

    public function write(): void
    {
        if (!is_writable(\dirname($this->cacheFile))) {
            return;
        }

        $cache = array_filter(
            $this->cache,
            function ($key) {
                return isset($this->parsedFiles[$key]);
            },
            ARRAY_FILTER_USE_KEY
        );

        $payload = array_map(
            function (array $data) {
                $data['ast'] = json_encode($data['ast']);

                return $data;
            },
            $cache
        );

        file_put_contents(
            $this->cacheFile,
            json_encode(
                [
                    'version' => Application::VERSION,
                    'payload' => $payload,
                ]
            )
        );
    }
}
{'fraction_non_alphanumeric': 0.14715447154471545, 'fraction_numerical': 0.0008130081300813008, 'mean_word_length': 1.6979166666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': '1461978', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1362, 'n_words': 243}