<commit_before><?php


namespace Kibo\Phast\Cache\File;


use Kibo\Phast\Common\ObjectifiedFunctions;

class DiskCleanup extends ProbabilisticExecutor {

    /**
     * @var integer
     */
    private $maxSize;

    /**
     * @var float
     */
    private $portionToFree;

    public function __construct(array $config, ObjectifiedFunctions $functions = null) {
        $this->maxSize = $config['diskCleanup']['maxSize'];
        $this->probability = $config['diskCleanup']['probability'];
        $this->portionToFree = $config['diskCleanup']['portionToFree'];
        parent::__construct($config, $functions);
    }

    protected function execute() {
        list ($usedSpace, $files) = $this->calculateUsedSpace();
        $neededSpace = round($this->portionToFree * $this->maxSize);
        $bytesToDelete = $usedSpace - $this->maxSize + $neededSpace;
        $deletedBytes = 0;
        /** @var \SplFileInfo $file */
        foreach ($files as $file) {
            $deletedBytes += $file->getSize();
            @unlink($file->getRealPath());
            if ($deletedBytes >= $bytesToDelete) {
                break;
            }
        }
    }

    private function calculateUsedSpace() {
        $size = 0;
        $files = [];
        /** @var \SplFileInfo $file */
        foreach ($this->getCacheFiles($this->cacheRoot) as $file) {
            $size += $file->getSize();
            $files[] = $file;
        }
        return [$size, $files];
    }

}<commit_msg>Do not delete a file if not needed<commit_after><?php


namespace Kibo\Phast\Cache\File;


use Kibo\Phast\Common\ObjectifiedFunctions;

class DiskCleanup extends ProbabilisticExecutor {

    /**
     * @var integer
     */
    private $maxSize;

    /**
     * @var float
     */
    private $portionToFree;

    public function __construct(array $config, ObjectifiedFunctions $functions = null) {
        $this->maxSize = $config['diskCleanup']['maxSize'];
        $this->probability = $config['diskCleanup']['probability'];
        $this->portionToFree = $config['diskCleanup']['portionToFree'];
        parent::__construct($config, $functions);
    }

    protected function execute() {
        list ($usedSpace, $files) = $this->calculateUsedSpace();
        $neededSpace = round($this->portionToFree * $this->maxSize);
        $bytesToDelete = $usedSpace - $this->maxSize + $neededSpace;
        $deletedBytes = 0;
        /** @var \SplFileInfo $file */
        foreach ($files as $file) {
            if ($deletedBytes >= $bytesToDelete) {
                break;
            }
            $deletedBytes += $file->getSize();
            @unlink($file->getRealPath());
        }
    }

    private function calculateUsedSpace() {
        $size = 0;
        $files = [];
        /** @var \SplFileInfo $file */
        foreach ($this->getCacheFiles($this->cacheRoot) as $file) {
            $size += $file->getSize();
            $files[] = $file;
        }
        return [$size, $files];
    }

}
{'fraction_non_alphanumeric': 0.15240013427324606, 'fraction_numerical': 0.001342732460557234, 'mean_word_length': 2.303769401330377, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '3688704', 'n_tokens_mistral': 890, 'n_tokens_neox': 850, 'n_words': 171}