<commit_before><?php

namespace Reporter;

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

	public function __construct($config) 
	{
		$this->config = $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($this->config);
		}
	}

	public function run() 
	{
		if ($this->config['test_file']) {
			if (is_readable($this->config['test_file'])) {
				$this->processTestFile($this->config['test_file']);
			} elseif (is_readable($this->config['include_base'] . $this->config['test_folder'] . '/' . $this->config['test_file'])) {
				$this->processTestFile($this->config['include_base'] . $this->config['test_folder'] . '/' . $this->config['test_file']);
			} else {
				trigger_error('Could not read specified test file: ' . escapeshellcmd($this->config['test_file']), E_USER_ERROR);
			}

		} else {
			$this->output('Running all tests');
		}
	}

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

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

		return !(bool) error_get_last();
	}

	protected function processTestFile($filepath) 
	{
		if ($this->validateTestFilename($filepath)) {
			if ($contents = $this->retrieveTestFileContents($filepath)) {
				if (($test_config = $this->parseTestFileContents($contents)) !== false) {
					$result_set = new \Reporter\ResultSet();
					$this->runTestFile($test_config, $result_set);
					$this->notifier->sendResults($result_set, $test_config);
				} 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);
			}
		} else {
			trigger_error('Filename ' . escapeshellcmd($filepath) . ' did not validate', E_USER_ERROR);
		}
	}

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

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

	protected function parseTestFileContents($contents) 
	{<commit_msg>Making helper mehtods static<commit_after><?php

namespace Reporter;

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

	public function __construct($config) 
	{
		$this->config = $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($this->config);
		}
	}

	public function run() 
	{
		if ($this->config['test_file']) {
			if (is_readable($this->config['test_file'])) {
				$this->processTestFile($this->config['test_file']);
			} elseif (is_readable($this->config['include_base'] . $this->config['test_folder'] . '/' . $this->config['test_file'])) {
				$this->processTestFile($this->config['include_base'] . $this->config['test_folder'] . '/' . $this->config['test_file']);
			} else {
				trigger_error('Could not read specified test file: ' . escapeshellcmd($this->config['test_file']), E_USER_ERROR);
			}

		} else {
			$this->output('Running all tests');
		}
	}

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

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

		return !(bool) error_get_last();
	}

	protected function processTestFile($filepath) 
	{
		if ($this->validateTestFilename($filepath)) {
			if ($contents = self::retrieveTestFileContents($filepath)) {
				if (($test_config = self::parseTestFileContents($contents)) !== false) {
					$result_set = new \Reporter\ResultSet();
					$this->runTestFile($test_config, $result_set);
					$this->notifier->sendResults($result_set, $test_config);
				} 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);
			}
		} else {
			trigger_error('Filename ' . escapeshellcmd($filepath) . ' did not validate', E_USER_ERROR);
		}
	}

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

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

	protected static function parseTestFileContents($contents) 
	{
{'fraction_non_alphanumeric': 0.16334450520373964, 'fraction_numerical': 0.0, 'mean_word_length': 4.692771084337349, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, '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': '1950707', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1805, 'n_words': 345}