<commit_before><?php namespace Lit\CachedFastRoute;

use FastRoute\Dispatcher;
use Lit\Core\AbstractRouter;
use Lit\Core\Interfaces\IStubResolver;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;

class FastRouteRouter extends AbstractRouter
{
    /**
     * @var CachedDispatcher
     */
    protected $cachedDispatcher;
    /**
     * @var mixed
     */
    protected $methodNotAllowed;

    public function __construct(
        CachedDispatcher $cachedDispatcher,
        IStubResolver $stubResolver,
        $notFound,
        $methodNotAllowed = null
    ) {
        parent::__construct($stubResolver, $notFound);
        $this->cachedDispatcher = $cachedDispatcher;
        $this->methodNotAllowed = $methodNotAllowed;
    }


    protected function findStub(ServerRequestInterface $request)
    {
        $path = $request->getUri()->getPath();
        $method = $request->getMethod();

        $routeInfo = $this->cachedDispatcher->dispatch($method, $path);

        return $this->stub($routeInfo);
    }

    protected function stub($routeInfo)
    {
        switch ($routeInfo[0]) {
            case Dispatcher::NOT_FOUND:
                return $this->notFound;
                break;
            case Dispatcher::METHOD_NOT_ALLOWED:
                if (!empty($this->methodNotAllowed)) {
                    return [$this->methodNotAllowed, [$routeInfo[1]]];
                } else {
                    return $this->notFound;
                }
                break;
            case Dispatcher::FOUND:
                list(, $stub, $vars) = $routeInfo;

                if (empty($vars)) {
                    return $stub;
                } else {
                    return $this->proxy($stub, $vars);
                }
                break;

            default:
                throw new \Exception(__METHOD__ . '/' . __LINE__);
        }
    }

    protected function proxy($stub, $vars)
    {
        return function (ServerRequestInterface $request, Response $response, $next) use ($stub, $vars) {
            foreach ($vars as $key => $val) {
                $request = $request->withAttribute($key, $val);
            }
            $middleware = $this->resolve($stub);

            return call_user_func($middleware, $request, $response, $next);
        };
    }
}
<commit_msg>remove break && use comment to shutup php-cs<commit_after><?php namespace Lit\CachedFastRoute;

use FastRoute\Dispatcher;
use Lit\Core\AbstractRouter;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;

class FastRouteRouter extends AbstractRouter
{
    /**
     * @var CachedDispatcher
     */
    protected $cachedDispatcher;
    /**
     * @var mixed
     */
    protected $methodNotAllowed;

    public function __construct(
        CachedDispatcher $cachedDispatcher,
        IStubResolver $stubResolver,
        $notFound,
        $methodNotAllowed = null
    ) {
        parent::__construct($stubResolver, $notFound);
        $this->cachedDispatcher = $cachedDispatcher;
        $this->methodNotAllowed = $methodNotAllowed;
    }


    protected function findStub(ServerRequestInterface $request)
    {
        $path = $request->getUri()->getPath();
        $method = $request->getMethod();

        $routeInfo = $this->cachedDispatcher->dispatch($method, $path);

        return $this->stub($routeInfo);
    }

    protected function stub($routeInfo)
    {
        switch ($routeInfo[0]) {
            case Dispatcher::NOT_FOUND:
                return $this->notFound;
            // @codingStandardsIgnoreStart
            case Dispatcher::METHOD_NOT_ALLOWED:
                // @codingStandardsIgnoreEnd
                if (!empty($this->methodNotAllowed)) {
                    return [$this->methodNotAllowed, [$routeInfo[1]]];
                } else {
                    return $this->notFound;
                }
            // @codingStandardsIgnoreStart
            case Dispatcher::FOUND:
                // @codingStandardsIgnoreEnd
                list(, $stub, $vars) = $routeInfo;

                if (empty($vars)) {
                    return $stub;
                } else {
                    return $this->proxy($stub, $vars);
                }

            default:
                throw new \Exception(__METHOD__ . '/' . __LINE__);
        }
    }

    protected function proxy($stub, $vars)
    {
        return function (ServerRequestInterface $request, Response $response, $next) use ($stub, $vars) {
            foreach ($vars as $key => $val) {
                $request = $request->withAttribute($key, $val);
            }
            $middleware = $this->resolve($stub);

            return call_user_func($middleware, $request, $response, $next);
        };
    }
}

{'fraction_non_alphanumeric': 0.1178699221216586, 'fraction_numerical': 0.0008419280151547042, 'mean_word_length': 1.9135499693439608, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '1968003', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1258, 'n_words': 253}