<commit_before><?php
namespace Sonic\UnitTest;

/**
 * Runner
 *
 * Singleton class for running all the unit tests
 *
 * @category Sonic
 * @package UnitTest
 * @author <NAME>
 */
class Runner
{
    /**
     * @var Runner
     */
    protected static $_instance;

    /**
     * path to the directory containing the tests we are running
     *
     * @var string
     */
    protected $_directory;

    /**
     * single file we are going to process
     *
     * @var SplFileInfo
     */
    protected $_file;

    /**
     * path to directory to store the coverage report in
     *
     * @var string
     */
    protected $_coverage_directory;

    /**
     * stores the current TestCase object and method
     *
     * @var array
     */
    protected $_current;

    /**
     * args that were passed in via command line
     *
     * @var array
     */
    protected $_args = array();

    /**
     * used to store the coverage per file before we create Coverage objects
     *
     * @todo move this out into some sort of Coverage Tracker object
     * @var array
     */
    protected $_coverage = array();

    /**
     * name of bootstrap file
     *
     * @var string
     */
    const BOOTSTRAP = '_bootstrap.php';

    /**
     * constructor
     *
     * @return void
     */
    private function __construct()
    {
        $this->_loadFramework();
    }

    /**
     * gets instance of Runner
     *
     * @return Runner
     */
    public static function getInstance()
    {
        if (!self::$_instance instanceof Runner) {
            self::$_instance = new Runner();
        }
        return self::$_instance;
    }

    /**
     * loads the unit test framework
     *
     * @return void
     */
    protected function _loadFramework()
    {
        include 'Exception.php';
        include 'Coverage.php';
        include 'Result.php';
        include 'Result/Error.php';
        include 'Result/Failure.php';
        include 'Result/Success.php';
        include 'TestCase.php';
        include 'Tracker.php';
    }

    /**
     * starts running the unit tests
     *
     * @param array $args argumnts passed in via command line
     * @return void
     */
    public static function start($args = array())
    {
        $runner = self::getInstance();

        // store the args locally and process them
        $runner->_args = $args;
        $runner->_processArgs($args);

        // set up an error handler so warnings and errors can be caught
        set_error_handler(array($runner, 'handleError'));

        if ($runner->showCoverage()) {
            xdebug_start_code_coverage(XDEBUG_CC_UNUSED);
        }

        if ($runner->hasFile(self::BOOTSTRAP)) {
            $runner->includeFile(self::BOOTSTRAP);
        }

        $runner->runTests($runner->_file);

        $runner->showResults();
    }

    /**
     * processes the command line arguments
     *
     * @param array $args
     * @return void
     */
    protected function _processArgs($args = array())
    {
        if (count($args) == 1) {
            throw new Exception('no test directory specified');
        }

        $dir = array_pop($args);

        $coverage_directory = $this->_getArgValue('--coverage-html', $args);
        if (!$coverage_directory) {
            return;
        }

        if (!file_exists($coverage_directory)) {
            mkdir($coverage_directory);
        }
        $path = $this->_convertDirectoryToPath($coverage_directory);
        $this->coverageDirectory($path);

        // single file
        if (!is_dir($dir) && file_exists($dir)) {
            $path = $this->_convertDirectoryToPath($dir);
            $this->_file = new \SplFileInfo($path);
            $this->directory($this->_file->getPath());
            return;
        }

        if (!is_dir($dir)) {
            throw new Exception('invalid test directory specified: ' . $dir);
        }

        $directory = $this->_convertDirectoryToPath($dir);
        $this->directory($directory);
    }

    /**
     * handles runtime errors that occur while the tests are running
     *
     * @param int $type
     * @param string $message
     * @param string $file
     * @param int $line
     * @return void
     */
    public function handleError($type, $message, $file, $line)<commit_msg>fix this so it actually works<commit_after><?php
namespace Sonic\UnitTest;

/**
 * Runner
 *
 * Singleton class for running all the unit tests
 *
 * @category Sonic
 * @package UnitTest
 * @author <NAME>
 */
class Runner
{
    /**
     * @var Runner
     */
    protected static $_instance;

    /**
     * path to the directory containing the tests we are running
     *
     * @var string
     */
    protected $_directory;

    /**
     * single file we are going to process
     *
     * @var SplFileInfo
     */
    protected $_file;

    /**
     * path to directory to store the coverage report in
     *
     * @var string
     */
    protected $_coverage_directory;

    /**
     * stores the current TestCase object and method
     *
     * @var array
     */
    protected $_current;

    /**
     * args that were passed in via command line
     *
     * @var array
     */
    protected $_args = array();

    /**
     * used to store the coverage per file before we create Coverage objects
     *
     * @todo move this out into some sort of Coverage Tracker object
     * @var array
     */
    protected $_coverage = array();

    /**
     * name of bootstrap file
     *
     * @var string
     */
    const BOOTSTRAP = '_bootstrap.php';

    /**
     * constructor
     *
     * @return void
     */
    private function __construct()
    {
        $this->_loadFramework();
    }

    /**
     * gets instance of Runner
     *
     * @return Runner
     */
    public static function getInstance()
    {
        if (!self::$_instance instanceof Runner) {
            self::$_instance = new Runner();
        }
        return self::$_instance;
    }

    /**
     * loads the unit test framework
     *
     * @return void
     */
    protected function _loadFramework()
    {
        include 'Exception.php';
        include 'Coverage.php';
        include 'Result.php';
        include 'Result/Error.php';
        include 'Result/Failure.php';
        include 'Result/Success.php';
        include 'TestCase.php';
        include 'Tracker.php';
    }

    /**
     * starts running the unit tests
     *
     * @param array $args argumnts passed in via command line
     * @return void
     */
    public static function start($args = array())
    {
        $runner = self::getInstance();

        // store the args locally and process them
        $runner->_args = $args;
        $runner->_processArgs($args);

        // set up an error handler so warnings and errors can be caught
        set_error_handler(array($runner, 'handleError'));

        if ($runner->showCoverage()) {
            xdebug_start_code_coverage(XDEBUG_CC_UNUSED);
        }

        if ($runner->hasFile(self::BOOTSTRAP)) {
            $runner->includeFile(self::BOOTSTRAP);
        }

        $runner->runTests($runner->_file);

        $runner->showResults();
    }

    /**
     * processes the command line arguments
     *
     * @param array $args
     * @return void
     */
    protected function _processArgs($args = array())
    {
        if (count($args) == 1) {
            throw new Exception('no test directory specified');
        }

        $dir = array_pop($args);

        if (!is_dir($dir) && !file_exists($dir)) {
            throw new Exception('invalid test directory specified: ' . $dir);
        }

        // single file
        if (!is_dir($dir) && file_exists($dir)) {
            $path = $this->_convertDirectoryToPath($dir);
            $this->_file = new \SplFileInfo($path);
            $this->directory($this->_file->getPath());
        }

        if (is_dir($dir)) {
            $directory = $this->_convertDirectoryToPath($dir);
            $this->directory($directory);
        }

        $coverage_directory = $this->_getArgValue('--coverage-html', $args);
        if (!$coverage_directory) {
            return;
        }

        if (!file_exists($coverage_directory)) {
            mkdir($coverage_directory);
        }
        $path = $this->_convertDirectoryToPath($coverage_directory);
        $this->coverageDirectory($path);
    }

    /**
     * handles runtime errors that occur while the tests are running
     *
     * @param int $type
     * @param string $message
     * @param string $file
     * @param int $line
     * @return void
     */
    public function handleError($type, $message, $file, $line)
{'fraction_non_alphanumeric': 0.12158406669754517, 'fraction_numerical': 0.0002315886984715146, 'mean_word_length': 1.9012428619415518, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 43, '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': '2859403', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2370, 'n_words': 650}