<commit_before><?php

namespace Reporter;

class Reporter
{
	protected $remote_content;
	protected $display_output = true;
	protected $logfile;
	protected $notifier;

	public function __construct() {}

	public function run($config)
	{
		if (!$this->parseConfig($config)) {
			trigger_error('There were errors parsing the config file', E_USER_ERROR);
		}

		// Only initiate Notifier if php_mailer_location is set
		if (isset($config['php_mailer_location']) && $config['php_mailer_location']) {
			$this->notifier = new \Reporter\Notifier($config);
		}

		$test_files = $this->getTestFiles($config);
		foreach ($test_files as $test_file) { 
			$this->processTestFile($test_file, $config);
		}
	}

	protected function getTestFiles($config) {
		$test_files = array();
		if ($config['test_file']) {
			if (!$this->validateTestFilename($config['test_file'], $config)) {
				trigger_error('Filename ' . escapeshellcmd($config['test_file']) . ' did not validate', E_USER_ERROR);
			}
			if (is_readable($config['test_file'])) {
				$test_files[] = $config['test_file'];
			} elseif (is_readable($config['include_base'] . $config['test_folder'] . '/' . $config['test_file'])) {
				$test_files[] = $config['include_base'] . $config['test_folder'] . '/' . $config['test_file'];
			} else {
				trigger_error('Could not read specified test file: ' . escapeshellcmd($config['test_file']), E_USER_ERROR);
			}
		}
		else {
			$test_file_candidates = glob($config['include_base'] . $config['test_folder']  . "/*." . $config['test_file_extension']);
			foreach ($test_file_candidates as $test_file) {
				if (is_readable($test_file)) {
					$test_files[] = $test_file;
				} else {
					trigger_error('Could not read specified test file: ' . escapeshellcmd($test_file), E_USER_WARNING);
				}	
			}
		}
		return $test_files;
	}

	protected function parseConfig($config)
	{
		if (isset($config['logfile']) && $logfile = $config['logfile']) {
			if ($handle = fopen($logfile, 'w')) {
				$this->logfile = $logfile;
			} else {
				trigger_error('Could not open logfile for writing', E_USER_ERROR);
			}	
		}

		if (isset($config['display_output'])) {
			$this->display_output = (bool) $config['display_output'];
		}

		return !(bool) error_get_last();
	}

	protected function processTestFile($filepath, $config)
	{
		if ($contents = self::retrieveTestFileContents($filepath)) {
			if (($test_config = self::parseTestFileContents($contents)) !== false) {
				$result_set = new \Reporter\ResultSet();
				$this->runTestFile($test_config, $result_set);
				if (self::testNotificationLevelMet($test_config, $result_set) && $this->notifier && $this->notifier->sendResults($result_set, $test_config)) {
					echo $this->formatOutput('Results Emailed.');
				}
			} else {
				trigger_error('Test file ' .  escapeshellcmd($filepath) . ' is not a valid json file and could not be parsed.', E_USER_ERROR);
			}
		} else {
			trigger_error('Cound not retrieve contents of ' . escapeshellcmd($filepath), E_USER_ERROR);
		}
	}

	protected function validateTestFilename($filepath, $config)
	{
		$path_parts = pathinfo($filepath);
		return $path_parts['extension'] == $config['test_file_extension'];
	}

	protected static function retrieveTestFileContents($filepath)
	{
		$contents = file_get_contents($filepath);
		return $contents;
	}

	protected static function parseTestFileContents($contents)
	{
		if ($config = json_decode($contents)) {
			return $config;
		} 
		return false;
	}
<commit_msg>Refactoring<commit_after><?php

namespace Reporter;

class Reporter
{
	protected $remote_content;
	protected $display_output = true;
	protected $logfile;
	protected $notifier;

	public function __construct() {}

	public function run($config)
	{
		if (!$this->parseConfig($config)) {
			trigger_error('There were errors parsing the config file', E_USER_ERROR);
		}

		// Only initiate Notifier if php_mailer_location is set
		if (isset($config['php_mailer_location']) && $config['php_mailer_location']) {
			$this->notifier = new \Reporter\Notifier($config);
		}

		$test_files = $this->getTestFiles($config);

		foreach ($test_files as $test_file) {
			if ($individual_test_config = $this->processTestFile($test_file)) {

				$result_set = new \Reporter\ResultSet();
				$this->runTestFile($individual_test_config, $result_set);

				$notification_expected = self::testNotificationLevelMet($test_config, $result_set) && 
					$this->notifier;

				if ($notification_expected && $this->notifier->sendResults($result_set, $test_config)) {
					echo $this->formatOutput('Results Emailed.');
				}
			}
		}
	}

	protected function getTestFiles($config) {
		$test_files = array();
		if ($config['test_file']) {
			if (!$this->validateTestFilename($config['test_file'], $config)) {
				trigger_error('Filename ' . escapeshellcmd($config['test_file']) . ' did not validate', E_USER_ERROR);
			}
			if (is_readable($config['test_file'])) {
				$test_files[] = $config['test_file'];
			} elseif (is_readable($config['include_base'] . $config['test_folder'] . '/' . $config['test_file'])) {
				$test_files[] = $config['include_base'] . $config['test_folder'] . '/' . $config['test_file'];
			} else {
				trigger_error('Could not read specified test file: ' . escapeshellcmd($config['test_file']), E_USER_ERROR);
			}
		}
		else {
			$test_file_candidates = glob($config['include_base'] . $config['test_folder']  . "/*." . $config['test_file_extension']);
			foreach ($test_file_candidates as $test_file) {
				if (is_readable($test_file)) {
					$test_files[] = $test_file;
				} else {
					trigger_error('Could not read specified test file: ' . escapeshellcmd($test_file), E_USER_WARNING);
				}	
			}
		}
		return $test_files;
	}

	protected function parseConfig($config)
	{
		if (isset($config['logfile']) && $logfile = $config['logfile']) {
			if ($handle = fopen($logfile, 'w')) {
				$this->logfile = $logfile;
			} else {
				trigger_error('Could not open logfile for writing', E_USER_ERROR);
			}	
		}

		if (isset($config['display_output'])) {
			$this->display_output = (bool) $config['display_output'];
		}

		return !(bool) error_get_last();
	}

	protected function processTestFile($filepath)
	{
		if ($contents = self::retrieveTestFileContents($filepath)) {
			if (($test_config = self::parseTestFileContents($contents)) !== false) {
				return $test_config;
			}
			trigger_error('Test file ' .  escapeshellcmd($filepath) . ' is not a valid json file and could not be parsed.', E_USER_ERROR);
		}
		trigger_error('Cound not retrieve contents of ' . escapeshellcmd($filepath), E_USER_ERROR);
	}

	protected function validateTestFilename($filepath, $config)
	{
		$path_parts = pathinfo($filepath);
		return $path_parts['extension'] == $config['test_file_extension'];
	}

	protected static function retrieveTestFileContents($filepath)
	{
		$contents = file_get_contents($filepath);
		return $contents;
	}

	protected static function parseTestFileContents($contents)
	{
		if ($config = json_decode($contents)) {
			return $config;
		} 
		return false;
	}

{'fraction_non_alphanumeric': 0.15841161400512382, 'fraction_numerical': 0.0, 'mean_word_length': 4.722312703583062, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '4464783', 'n_tokens_mistral': 2567, 'n_tokens_neox': 2259, 'n_words': 438}