<commit_before><?php
namespace Wave\Framework\Application;

use FastRoute\DataGenerator\GroupCountBased;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std;
use Wave\Framework\Exceptions\HttpNotAllowedException;
use Wave\Framework\Exceptions\HttpNotFoundException;
use Wave\Framework\Interfaces\Http\RequestInterface;
use Wave\Framework\Interfaces\Http\ResponseInterface;

class Router
{
    private $prefix;
    private $cache = false;
    private $cacheFolder = '';

    /**
     * @var \FastRoute\RouteCollector
     */
    protected $collector;

    /**
     * @var array
     */
    protected $namedRoutes = [];

    public function __construct(array $options = [])
    {
        foreach ($options as $option => $value) {
            $this->$option = $value;
        }

        if ($this->prefix !== null) {
            $this->setPrefix($this->prefix);
        }

        if ($this->cache === true && is_readable($this->cacheFolder)) {
            throw new \RuntimeException('Cache directory is not readable');
        }

        $this->collector = new RouteCollector(
            new Std(),
            new GroupCountBased()
        );
    }

    private function setPrefix($prefix)
    {
        if ($prefix !== null && substr($prefix, 0, 1) !== '/') {
            throw new \LogicException('Prefix must begin with "/"');
        }

        $this->prefix = $prefix;
    }

    public function get($pattern, array $callback, $name = null)
    {
        $this->addRoute('get', $pattern, $callback, $name);

        return $this;
    }

    public function post($pattern, array $callback, $name = null)
    {
        $this->addRoute('post', $pattern, $callback, $name);

        return $this;
    }

    public function put($pattern, array $callback, $name = null)
    {
        $this->addRoute('put', $pattern, $callback, $name);

        return $this;
    }

    public function patch($pattern, array $callback, $name = null)
    {
        $this->addRoute('patch', $pattern, $callback, $name);

        return $this;
    }

    public function delete($pattern, array $callback, $name = null)
    {
        $this->addRoute('delete', $pattern, $callback, $name);

        return $this;
    }

    public function options($pattern, array $callback, $name = null)
    {
        $this->addRoute('options', $pattern, $callback, $name);

        return $this;
    }

    public function group($callback, array $options = [])
    {
        $oldPrefix = $this->prefix;
        if (array_key_exists('prefix', $options)) {
            $this->setPrefix($options['prefix']);
        }

        call_user_func($callback, $this);
        $this->setPrefix($oldPrefix);

        return $this;
    }

    private function addRoute($method, $pattern, array $callback, $name = null)
    {
        $this->collector->addRoute(strtoupper($method), (string) $this->prefix . $pattern, $callback);

        if ($name !== null) {
            $this->namedRoutes[$name] = $this->prefix . $pattern;
        }
    }

    public function route($name, array $args = [])
    {
        if (!array_key_exists($name, $this->namedRoutes)) {
            throw new \InvalidArgumentException(
                sprintf('Route with name "%s" does not exist', $name)
            );
        }

        if (!is_array($this->namedRoutes[$name])) {
            $pattern = $this->namedRoutes[$name];
            $patterns = [];
            preg_match_all('#\{([A-Z\}:\[\]|+_-])+#i', $pattern, $matches);

            foreach ($matches[0] as $match) {
                if (strpos($match, ':') !== false) {<commit_msg>Removed unused options from the Router<commit_after><?php
namespace Wave\Framework\Application;

use FastRoute\DataGenerator\GroupCountBased;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std;
use Wave\Framework\Exceptions\HttpNotAllowedException;
use Wave\Framework\Exceptions\HttpNotFoundException;
use Wave\Framework\Interfaces\Http\RequestInterface;
use Wave\Framework\Interfaces\Http\ResponseInterface;

class Router
{
    /**
     * @var \FastRoute\RouteCollector
     */
    protected $collector;
    /**
     * @var array
     */
    protected $namedRoutes = [];
    private $prefix;

    public function __construct(array $options = [])
    {
        foreach ($options as $option => $value) {
            $this->$option = $value;
        }

        if ($this->prefix !== null) {
            $this->setPrefix($this->prefix);
        }

        $this->collector = new RouteCollector(
            new Std(),
            new GroupCountBased()
        );
    }

    private function setPrefix($prefix)
    {
        if ($prefix !== null && substr($prefix, 0, 1) !== '/') {
            throw new \LogicException('Prefix must begin with "/"');
        }

        $this->prefix = $prefix;
    }

    public function get($pattern, array $callback, $name = null)
    {
        $this->addRoute('get', $pattern, $callback, $name);

        return $this;
    }

    private function addRoute($method, $pattern, array $callback, $name = null)
    {
        $this->collector->addRoute(strtoupper($method), (string)$this->prefix . $pattern, $callback);

        if ($name !== null) {
            $this->namedRoutes[$name] = $this->prefix . $pattern;
        }
    }

    public function post($pattern, array $callback, $name = null)
    {
        $this->addRoute('post', $pattern, $callback, $name);

        return $this;
    }

    public function put($pattern, array $callback, $name = null)
    {
        $this->addRoute('put', $pattern, $callback, $name);

        return $this;
    }

    public function patch($pattern, array $callback, $name = null)
    {
        $this->addRoute('patch', $pattern, $callback, $name);

        return $this;
    }

    public function delete($pattern, array $callback, $name = null)
    {
        $this->addRoute('delete', $pattern, $callback, $name);

        return $this;
    }

    public function options($pattern, array $callback, $name = null)
    {
        $this->addRoute('options', $pattern, $callback, $name);

        return $this;
    }

    public function group($callback, array $options = [])
    {
        $oldPrefix = $this->prefix;
        if (array_key_exists('prefix', $options)) {
            $this->setPrefix($options['prefix']);
        }

        call_user_func($callback, $this);
        $this->setPrefix($oldPrefix);

        return $this;
    }

    public function route($name, array $args = [])
    {
        if (!array_key_exists($name, $this->namedRoutes)) {
            throw new \InvalidArgumentException(
                sprintf('Route with name "%s" does not exist', $name)
            );
        }

        if (!is_array($this->namedRoutes[$name])) {
            $pattern = $this->namedRoutes[$name];
            $patterns = [];
            preg_match_all('#\{([A-Z\}:\[\]|+_-])+#i', $pattern, $matches);

            foreach ($matches[0] as $match) {
                if (strpos($match, ':') !== false) {
{'fraction_non_alphanumeric': 0.15575653172552398, 'fraction_numerical': 0.0008613264427217916, 'mean_word_length': 2.3559730250481694, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '7546044', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1995, 'n_words': 450}