<commit_before><?php
namespace Cohesion\Route;

use \Cohesion\Config\Configurable;
use \Cohesion\Config\Config;

class Route implements Configurable {

    protected $uri;
    protected $config;

    protected $redirect;
    protected $className;
    protected $functionName;
    protected $params;

    public function __construct(Config $config, $uri = null) {
        $this->uri = $uri;
        $this->config = $config;
        if (!$redirect = $this->getRedirect()) {
            try {
                $this->setByDefaultRoute();
            } catch (RouteException $e) {
                // No need to do anything. Should always be checking if the className is set
            }
        }
    }

    protected function setByDefaultRoute() {
        $components = explode('/', ltrim(preg_replace('/\/+/', '/', $this->uri), '/'));
        $defaultClassName = $this->constructClassName($this->config->get('class.default'));
        $className = $defaultClassName;
        $functionName = null;
        $params = array();
        foreach ($components as $component) {
            if (!$functionName) {
                if ($className == $defaultClassName && $component) {
                    $checkClassName = $this->constructClassName($component);
                    if (class_exists($checkClassName)) {
                        $className = $checkClassName;
                        continue;
                    }
                }
                $checkFunctionName = $this->constructFunctionName($component);
                if (method_exists($className, $checkFunctionName)) {
                    $functionName = $checkFunctionName;
                } else if ($className != $defaultClassName) {
                    $functionName = $this->constructFunctionName($this->config->get('function.default'));
                    if (method_exists($className, $functionName)) {
                        $params[] = $component;
                    } else {
                        throw new RouteException("$className doesn't have an $functionName function");
                    }
                }
            } else {
                $params[] = $component;
            }
        }
        if (!$functionName) {
            $functionName = $this->constructFunctionName($this->config->get('function.default'));
            if ($className == $defaultClassName) {
                if ($components[0] != '') {
                    $params = $components;
                }
            }
        }
        $reflection = new \ReflectionMethod($className, $functionName);
        $minParams = $reflection->getNumberOfRequiredParameters();
        $maxParams = $reflection->getNumberOfParameters();
        if (count($params) < $minParams) {
            throw new RouteException("$className->$functionName requires at least $minParams parameters");
        } else if (count($params) > $maxParams) {
            throw new RouteException("$className->$functionName only accepts up to $maxParams parameters");
        }

        $this->className = $className;
        $this->functionName = $functionName;
        $this->params = $params;
    }

    public function getRedirect() {
        if (!isset($this->redirect)) {
            $redirects = $this->config->get('redirects');
            foreach ($redirects as $regex => $location) {
                if (preg_match("!$regex!", $this->uri)) {
                    $this->redirect = $location;
                    return $location;
                }
            }
        }
        return $this->redirect;
    }

    public function getUri() {
        return $this->uri;
    }

    public function getClassName() {
        return $this->className;
    }

    public function getFunctionName() {
        return $this->functionName;
    }

    public function getParameterValues() {
        return $this->params;
    }

    protected function constructClassName($name) {
        $words = explode('-', $name);
        $name = '';
        foreach ($words as $word) {
            $name .= ucfirst($word);
        }
        return $this->config->get('class.prefix') . $name . $this->config->get('class.suffix');
    }

    protected function constructFunctionName($name) {
        $prefix = $this->config->get('function.prefix');
        $words = explode('-', $name);
        $name = '';
        if (!$prefix) {
            $name = ucfirst(array_shift($words));
        }
        foreach ($words as $word) {
            $name .= ucfirst($word);
        }
        return $prefix . $name . $this->config->get('function.suffix');
    }
}

class RouteException extends \RuntimeException {}
<commit_msg>add NotFound error when the default function doesn't exists<commit_after><?php
namespace Cohesion\Route;

use \Cohesion\Config\Configurable;
use \Cohesion\Config\Config;

class Route implements Configurable {

    protected $uri;
    protected $config;

    protected $redirect;
    protected $className;
    protected $functionName;
    protected $params;

    public function __construct(Config $config, $uri = null) {
        $this->uri = $uri;
        $this->config = $config;
        if (!$redirect = $this->getRedirect()) {
            try {
                $this->setByDefaultRoute();
            } catch (RouteException $e) {
                // No need to do anything. Should always be checking if the className is set
            }
        }
    }

    protected function setByDefaultRoute() {
        $components = explode('/', ltrim(preg_replace('/\/+/', '/', $this->uri), '/'));
        $defaultClassName = $this->constructClassName($this->config->get('class.default'));
        $className = $defaultClassName;
        $functionName = null;
        $params = array();
        foreach ($components as $component) {
            if (!$functionName) {
                if ($className == $defaultClassName && $component) {
                    $checkClassName = $this->constructClassName($component);
                    if (class_exists($checkClassName)) {
                        $className = $checkClassName;
                        continue;
                    }
                }
                $checkFunctionName = $this->constructFunctionName($component);
                if (method_exists($className, $checkFunctionName)) {
                    $functionName = $checkFunctionName;
                } else if ($className != $defaultClassName) {
                    $functionName = $this->constructFunctionName($this->config->get('function.default'));
                    if (method_exists($className, $functionName)) {
                        $params[] = $component;
                    } else {
                        throw new RouteException("$className doesn't have a $functionName function");
                    }
                }
            } else {
                $params[] = $component;
            }
        }
        if (!$functionName) {
            $defaultFunction = $this->constructFunctionName($this->config->get('function.default'));
            if (method_exists($className, $defaultFunction)) {
                $functionName = $defaultFunction;
                if ($className == $defaultClassName) {
                    if ($components[0] != '') {
                        $params = $components;
                    }
                }
            } else {
                throw new RouteException("$className doesn't have an $functionName function");
            }
        }
        $reflection = new \ReflectionMethod($className, $functionName);
        $minParams = $reflection->getNumberOfRequiredParameters();
        $maxParams = $reflection->getNumberOfParameters();
        if (count($params) < $minParams) {
            throw new RouteException("$className->$functionName requires at least $minParams parameters");
        } else if (count($params) > $maxParams) {
            throw new RouteException("$className->$functionName only accepts up to $maxParams parameters");
        }

        $this->className = $className;
        $this->functionName = $functionName;
        $this->params = $params;
    }

    public function getRedirect() {
        if (!isset($this->redirect)) {
            $redirects = $this->config->get('redirects');
            foreach ($redirects as $regex => $location) {
                if (preg_match("!$regex!", $this->uri)) {
                    $this->redirect = $location;
                    return $location;
                }
            }
        }
        return $this->redirect;
    }

    public function getUri() {
        return $this->uri;
    }

    public function getClassName() {
        return $this->className;
    }

    public function getFunctionName() {
        return $this->functionName;
    }

    public function getParameterValues() {
        return $this->params;
    }

    protected function constructClassName($name) {
        $words = explode('-', $name);
        $name = '';
        foreach ($words as $word) {
            $name .= ucfirst($word);
        }
        return $this->config->get('class.prefix') . $name . $this->config->get('class.suffix');
    }

    protected function constructFunctionName($name) {
        $prefix = $this->config->get('function.prefix');
        $words = explode('-', $name);
        $name = '';
        if (!$prefix) {
            $name = ucfirst(array_shift($words));
        }
        foreach ($words as $word) {
            $name .= ucfirst($word);
        }
        return $prefix . $name . $this->config->get('function.suffix');
    }
}

class RouteException extends \RuntimeException {}

{'fraction_non_alphanumeric': 0.13330543057444805, 'fraction_numerical': 0.00020927069163963587, 'mean_word_length': 1.8480333730631704, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 89, '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': '725421', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2323, 'n_words': 524}