<commit_before><?php
/**
 *
 */

namespace Mvc5\Route\Match;

use Mvc5\Arg;
use Mvc5\Request\Request;
use Mvc5\Route\Route;

class Path
{
    /**
     *
     */
    use Plugin\Params;

    /**
     * @param Route $route
     * @param Request $request
     * @param int $offset
     * @param callable $next
     * @return Request
     */
    protected function match(Route $route, Request $request, $offset, callable $next)
    {
        $match = $this->search($route->regex(), $request->path(), $offset);

        if (!$match) {
            return null;
        }

        $offset += strlen($match[0]);
        $matched = $offset == strlen($request->path());

        $request[Arg::CONTROLLER] = $route->controller();
        $request[Arg::MATCHED] = $offset;
        $request[Arg::PARAMS] = $this->params($match, $route->defaults() + $request->params());
        $request[Arg::ROUTE] = $matched ? $route : null;

        return $matched ? $next($route, $request) : ($route->children() ? $request : null);
    }

    /**
     * @param $regex
     * @param $path
     * @param $offset
     * @return array|null
     */
    protected function search($regex, $path, $offset)
    {
        return !$regex || !preg_match('(\G' . $regex . ')', $path, $match, null, (int) $offset) ? null : $match;
    }

    /**
     * @param Route $route
     * @param Request $request
     * @param callable $next
     * @return Request
     */
    function __invoke(Route $route, Request $request, callable $next)
    {
        return $this->match($route, $request, $request[Arg::MATCHED], $next);
    }
}<commit_msg>tidy up<commit_after><?php
/**
 *
 */

namespace Mvc5\Route\Match;

use Mvc5\Arg;
use Mvc5\Request\Request;
use Mvc5\Route\Route;

class Path
{
    /**
     *
     */
    use Plugin\Params;

    /**
     * @param Route $route
     * @param Request $request
     * @param int $offset
     * @param callable $next
     * @return Request
     */
    protected function match(Route $route, Request $request, $offset, callable $next)
    {
        if (!preg_match('(\G' . $route->regex() . ')', $request->path(), $match, null, (int) $offset)) {
            return null;
        }

        $offset += strlen($match[0]);
        $matched = $offset == strlen($request->path());

        $request[Arg::CONTROLLER] = $route->controller();
        $request[Arg::MATCHED] = $offset;
        $request[Arg::PARAMS] = $this->params($match, $route->defaults() + $request->params());
        $request[Arg::ROUTE] = $matched ? $route : null;

        return $matched ? $next($route, $request) : ($route->children() ? $request : null);
    }

    /**
     * @param Route $route
     * @param Request $request
     * @param callable $next
     * @return Request
     */
    function __invoke(Route $route, Request $request, callable $next)
    {
        return $this->match($route, $request, $request[Arg::MATCHED], $next);
    }
}
{'fraction_non_alphanumeric': 0.1798486932599725, 'fraction_numerical': 0.0034387895460797797, 'mean_word_length': 2.4630952380952382, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, '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': '5076336', 'n_tokens_mistral': 939, 'n_tokens_neox': 924, 'n_words': 209}