<commit_before><?php
namespace Tonis\Router;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class Router
{
    /** @var Route|null */
    private $lastMatch;
    /** @var RouteCollection */
    private $routeCollection;
    /** @var Rule\RuleInterface[] */
    private $rules;

    public function __construct()
    {
        $this->routeCollection = new RouteCollection;
        $this->rules = [
            new Rule\Secure,
            new Rule\Method(),
            new Rule\Path()
        ];
    }

    /**
     * Middleware
     *
     * Sets the handler as an attribute on the $request if a route matches.
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface $response
     * @param callable|null $next
     * @return callable|ResponseInterface|ServerRequestInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
    {
        $match = $this->match($request);
        if ($match instanceof RouteMatch) {
            if (null !== $next) {
                return $next($request->withAttribute('route.handler', $match->getRoute()->getHandler()), $response);
            }
        }
        return $response;
    }

    /**
     * @param string $name
     * @param array $params
     * @return string
     * @throws Exception\RouteDoesNotExistException
     */
    public function assemble($name, array $params = [])
    {
        $route = $this->routeCollection[$name];<commit_msg>Refactor __invoke() slightly<commit_after><?php
namespace Tonis\Router;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

final class Router
{
    /** @var Route|null */
    private $lastMatch;
    /** @var RouteCollection */
    private $routeCollection;
    /** @var Rule\RuleInterface[] */
    private $rules;

    public function __construct()
    {
        $this->routeCollection = new RouteCollection;
        $this->rules = [
            new Rule\Secure,
            new Rule\Method(),
            new Rule\Path()
        ];
    }

    /**
     * Middleware
     *
     * Sets the handler as an attribute on the $request if a route matches.
     *
     * @param ServerRequestInterface $request
     * @param ResponseInterface $response
     * @param callable|null $next
     * @return callable|ResponseInterface|ServerRequestInterface
     */
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
    {
        $match = $this->match($request);
        if ($match instanceof RouteMatch) {
            if (null !== $next) {
                $request = $request
                    ->withAttribute('route.handler', $match->getRoute()->getHandler())
                    ->withAttribute('route.params', $match->getParams());
            }
        }
        return $next ? $next($request, $response) : $response;
    }

    /**
     * @param string $name
     * @param array $params
     * @return string
     * @throws Exception\RouteDoesNotExistException
     */
    public function assemble($name, array $params = [])
    {
        $route = $this->routeCollection[$name];
{'fraction_non_alphanumeric': 0.12195863746958638, 'fraction_numerical': 0.0, 'mean_word_length': 2.397727272727273, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '4773759', 'n_tokens_mistral': 885, 'n_tokens_neox': 874, 'n_words': 212}