<commit_before><?php
declare(strict_types=1);

/**
 * This file is part of phpDocumentor.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2010-2018 <NAME> / Naenius (http://www.naenius.com)
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
 * @link      http://phpdoc.org
 */

namespace phpDocumentor\Descriptor\Cache;

use phpDocumentor\Descriptor\FileDescriptor;
use phpDocumentor\Descriptor\ProjectDescriptor;
use phpDocumentor\Descriptor\ProjectDescriptor\Settings;
use Stash\Interfaces\PoolInterface;
use Stash\Item;

/**
 * Maps a projectDescriptor to and from a cache instance.
 */
final class ProjectDescriptorMapper
{
    const FILE_PREFIX = 'phpDocumentor/projectDescriptor/files/';

    const FILE_LIST = 'phpDocumentor/projectDescriptor/filelist';

    const KEY_SETTINGS = 'phpDocumentor/projectDescriptor/settings';

    /** @var PoolInterface */
    private $cache;

    /**
     * Initializes this mapper with the given cache instance.
     */
    public function __construct(PoolInterface $cache)
    {
        $this->cache = $cache;
    }

    /**
     * Returns the Project Descriptor from the cache.
     */
    public function populate(ProjectDescriptor $projectDescriptor): void
    {
        $this->loadCacheItemAsSettings($projectDescriptor);

        $fileList = $this->cache->getItem(self::FILE_LIST)->get();
        if ($fileList !== null) {
            /** @var Item $item */
            foreach ($this->cache->getItems($fileList) as $item) {
                $file = $item->get();

                if ($file instanceof FileDescriptor) {
                    $projectDescriptor->getFiles()->set($file->getPath(), $file);
                }
            }
        }
    }

    /**
     * Stores a Project Descriptor in the Cache.
     */
    public function save(ProjectDescriptor $projectDescriptor): void
    {
        $fileListItem = $this->cache->getItem(self::FILE_LIST);
        $currentFileList = $fileListItem->get();
        $fileListItem->lock();

        // store the settings for this Project Descriptor
        $item = $this->cache->getItem(self::KEY_SETTINGS);
        $item->lock();
        $this->cache->saveDeferred($item->set($projectDescriptor->getSettings()));

        // store cache items
        $fileKeys = [];
        foreach ($projectDescriptor->getFiles() as $file) {
            $key = self::FILE_PREFIX . md5($file->getPath());
            $fileKeys[] = $key;
            $item = $this->cache->getItem($key);
            $item->lock();
            $this->cache->saveDeferred($item->set($file));
        }

        $this->cache->saveDeferred($fileListItem->set($fileKeys));
        $this->cache->commit();

        if ($currentFileList !== null) {
            // remove any keys that are no longer used.
            $invalidatedKeys = array_diff($currentFileList, $fileKeys);
            if ($invalidatedKeys) {
                $this->cache->deleteItems($invalidatedKeys);
            }
        }
    }

    /**
     * Removes all files in cache that do not occur in the given FileSet Collection.
     *
     * @todo restore this?
     */
    public function garbageCollect($collection)
    {
//        $projectRoot = $collection->getProjectRoot();
//        $filenames = $collection->getFilenames();
//
//        foreach ($filenames as &$name) {
//            // the cache key contains a path relative to the project root; here we expect absolute paths.
//            $name = self::FILE_PREFIX . md5(substr($name, strlen($projectRoot)));
//        }
//
//        foreach ($this->getCache() as $item) {
//            if (substr($item, 0, strlen(self::FILE_PREFIX)) === self::FILE_PREFIX &&
//                  !in_array($item, $filenames, true)
//            ) {
//                $this->getCache()->removeItem($item);
//            }
//        }
    }

    private function loadCacheItemAsSettings(ProjectDescriptor $projectDescriptor): void
    {
        $item = $this->cache->getItem(self::KEY_SETTINGS);
        if ($item->isHit()) {
            $settings = $item->get();
            $projectDescriptor->setSettings($settings);
        }
    }
}
<commit_msg>Use PSR interface<commit_after><?php
declare(strict_types=1);

/**
 * This file is part of phpDocumentor.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author    <NAME> <<EMAIL>>
 * @copyright 2010-2018 <NAME> / Naenius (http://www.naenius.com)
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
 * @link      http://phpdoc.org
 */

namespace phpDocumentor\Descriptor\Cache;

use phpDocumentor\Descriptor\FileDescriptor;
use phpDocumentor\Descriptor\ProjectDescriptor;
use Psr\Cache\CacheItemPoolInterface;
use Stash\Item;

/**
 * Maps a projectDescriptor to and from a cache instance.
 */
final class ProjectDescriptorMapper
{
    const FILE_PREFIX = 'phpDocumentor/projectDescriptor/files/';

    const FILE_LIST = 'phpDocumentor/projectDescriptor/filelist';

    const KEY_SETTINGS = 'phpDocumentor/projectDescriptor/settings';

    /** @var CacheItemPoolInterface */
    private $cache;

    /**
     * Initializes this mapper with the given cache instance.
     */
    public function __construct(CacheItemPoolInterface $cache)
    {
        $this->cache = $cache;
    }

    /**
     * Returns the Project Descriptor from the cache.
     */
    public function populate(ProjectDescriptor $projectDescriptor): void
    {
        $this->loadCacheItemAsSettings($projectDescriptor);

        $fileList = $this->cache->getItem(self::FILE_LIST)->get();
        if ($fileList !== null) {
            /** @var Item $item */
            foreach ($this->cache->getItems($fileList) as $item) {
                $file = $item->get();

                if ($file instanceof FileDescriptor) {
                    $projectDescriptor->getFiles()->set($file->getPath(), $file);
                }
            }
        }
    }

    /**
     * Stores a Project Descriptor in the Cache.
     */
    public function save(ProjectDescriptor $projectDescriptor): void
    {
        $fileListItem = $this->cache->getItem(self::FILE_LIST);
        $currentFileList = $fileListItem->get();

        // store the settings for this Project Descriptor
        $item = $this->cache->getItem(self::KEY_SETTINGS);
        $this->cache->saveDeferred($item->set($projectDescriptor->getSettings()));

        // store cache items
        $fileKeys = [];
        foreach ($projectDescriptor->getFiles() as $file) {
            $key = self::FILE_PREFIX . md5($file->getPath());
            $fileKeys[] = $key;
            $item = $this->cache->getItem($key);
            $this->cache->saveDeferred($item->set($file));
        }

        $this->cache->saveDeferred($fileListItem->set($fileKeys));
        $this->cache->commit();

        if ($currentFileList !== null) {
            // remove any keys that are no longer used.
            $invalidatedKeys = array_diff($currentFileList, $fileKeys);
            if ($invalidatedKeys) {
                $this->cache->deleteItems($invalidatedKeys);
            }
        }
    }

    /**
     * Removes all files in cache that do not occur in the given FileSet Collection.
     *
     * @todo restore this?
     */
    public function garbageCollect($collection)
    {
//        $projectRoot = $collection->getProjectRoot();
//        $filenames = $collection->getFilenames();
//
//        foreach ($filenames as &$name) {
//            // the cache key contains a path relative to the project root; here we expect absolute paths.
//            $name = self::FILE_PREFIX . md5(substr($name, strlen($projectRoot)));
//        }
//
//        foreach ($this->getCache() as $item) {
//            if (substr($item, 0, strlen(self::FILE_PREFIX)) === self::FILE_PREFIX &&
//                  !in_array($item, $filenames, true)
//            ) {
//                $this->getCache()->removeItem($item);
//            }
//        }
    }

    private function loadCacheItemAsSettings(ProjectDescriptor $projectDescriptor): void
    {
        $item = $this->cache->getItem(self::KEY_SETTINGS);
        if ($item->isHit()) {
            $settings = $item->get();
            $projectDescriptor->setSettings($settings);
        }
    }
}

{'fraction_non_alphanumeric': 0.14078478002378123, 'fraction_numerical': 0.002853745541022592, 'mean_word_length': 2.35634477254589, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '2403186', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2286, 'n_words': 519}