<commit_before><?php
/**
* @package        SimpleTest
* @subpackage     Extensions
*/
/**
* load coverage data handle
*/
require_once dirname(__FILE__) . '/coverage_data_handler.php';

/**
 * Orchestrates code coverage both in this thread and in subthread under apache
 * Assumes this is running on same machine as apache.
 * @package        SimpleTest
 * @subpackage     Extensions
 */
class CodeCoverage  {
    var $log;
    var $root;
    var $includes;
    var $excludes;
    var $directoryDepth;
    var $maxDirectoryDepth = 20; // reasonable, otherwise arbitrary
    var $title = "Code Coverage";

    # NOTE: This assumes all code shares the same current working directory.
    var $settingsFile = './code-coverage-settings.dat';

    static $instance;

    function writeUntouched() {
        $touched = array_flip($this->getTouchedFiles());
        $untouched = array();
        $this->getUntouchedFiles($untouched, $touched, '.', '.');
        $this->includeUntouchedFiles($untouched);
    }

    function &getTouchedFiles() {
        $handler = new CoverageDataHandler($this->log);
        $touched = $handler->getFilenames();
        return $touched;
    }

    function includeUntouchedFiles($untouched) {
        $handler = new CoverageDataHandler($this->log);
        foreach ($untouched as $file) {
            $handler->writeUntouchedFile($file);
        }
    }

    function getUntouchedFiles(&$untouched, $touched, $parentPath, $rootPath, $directoryDepth = 1) {
        $parent = opendir($parentPath);
        while ($file = readdir($parent)) {
            $path = "$parentPath/$file";
            if (is_dir($path)) {
                if ($file != '.' && $file != '..') {
                    if ($this->isDirectoryIncluded($path, $directoryDepth)) {
                        $this->getUntouchedFiles($untouched, $touched, $path, $rootPath, $directoryDepth + 1);
                    }
                }
            }
            else if ($this->isFileIncluded($path)) {
                $relativePath = CoverageDataHandler::ltrim($rootPath .'/', $path);
                if (!array_key_exists($relativePath, $touched)) {
                    $untouched[] = $relativePath;
                }
            }
        }
        fclose($parent);
    }

    function resetLog() {
        error_log('reseting log');
        $new_file = fopen($this->log, "w");
        if (!$new_file) {
            throw new Exception("Could not create ". $this->log);
        }
        fclose($new_file);
        if (!chmod($this->log, 0666)) {
            throw new Exception("Could not change ownership on file  ". $this->log);
        }
        $handler = new CoverageDataHandler($this->log);
        $handler->createSchema();
    }

    function startCoverage() {
        $this->root = getcwd();
        if(!extension_loaded("xdebug")) {
            throw new Exception("Could not load xdebug extension");
        };
        xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
    }
<commit_msg>code coverage fclose warning fix [patch n°2792131 submitted by <NAME> - dominicc]<commit_after><?php
/**
* @package        SimpleTest
* @subpackage     Extensions
*/
/**
* load coverage data handle
*/
require_once dirname(__FILE__) . '/coverage_data_handler.php';

/**
 * Orchestrates code coverage both in this thread and in subthread under apache
 * Assumes this is running on same machine as apache.
 * @package        SimpleTest
 * @subpackage     Extensions
 */
class CodeCoverage  {
    var $log;
    var $root;
    var $includes;
    var $excludes;
    var $directoryDepth;
    var $maxDirectoryDepth = 20; // reasonable, otherwise arbitrary
    var $title = "Code Coverage";

    # NOTE: This assumes all code shares the same current working directory.
    var $settingsFile = './code-coverage-settings.dat';

    static $instance;

    function writeUntouched() {
        $touched = array_flip($this->getTouchedFiles());
        $untouched = array();
        $this->getUntouchedFiles($untouched, $touched, '.', '.');
        $this->includeUntouchedFiles($untouched);
    }

    function &getTouchedFiles() {
        $handler = new CoverageDataHandler($this->log);
        $touched = $handler->getFilenames();
        return $touched;
    }

    function includeUntouchedFiles($untouched) {
        $handler = new CoverageDataHandler($this->log);
        foreach ($untouched as $file) {
            $handler->writeUntouchedFile($file);
        }
    }

    function getUntouchedFiles(&$untouched, $touched, $parentPath, $rootPath, $directoryDepth = 1) {
        $parent = opendir($parentPath);
        while ($file = readdir($parent)) {
            $path = "$parentPath/$file";
            if (is_dir($path)) {
                if ($file != '.' && $file != '..') {
                    if ($this->isDirectoryIncluded($path, $directoryDepth)) {
                        $this->getUntouchedFiles($untouched, $touched, $path, $rootPath, $directoryDepth + 1);
                    }
                }
            }
            else if ($this->isFileIncluded($path)) {
                $relativePath = CoverageDataHandler::ltrim($rootPath .'/', $path);
                if (!array_key_exists($relativePath, $touched)) {
                    $untouched[] = $relativePath;
                }
            }
        }
        closedir($parent);
    }

    function resetLog() {
        error_log('reseting log');
        $new_file = fopen($this->log, "w");
        if (!$new_file) {
            throw new Exception("Could not create ". $this->log);
        }
        fclose($new_file);
        if (!chmod($this->log, 0666)) {
            throw new Exception("Could not change ownership on file  ". $this->log);
        }
        $handler = new CoverageDataHandler($this->log);
        $handler->createSchema();
    }

    function startCoverage() {
        $this->root = getcwd();
        if(!extension_loaded("xdebug")) {
            throw new Exception("Could not load xdebug extension");
        };
        xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
    }

{'fraction_non_alphanumeric': 0.13519736842105262, 'fraction_numerical': 0.0037828947368421053, 'mean_word_length': 2.2605898123324395, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 38, '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': '6179261', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1709, 'n_words': 388}