<commit_before><?php

namespace Symlex\Router;

use Silex\Application;
use Twig_Environment;
use Twig_Error_Loader;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

/**
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */
class ErrorRouter
{
    protected $app;
    protected $twig;
    protected $exceptionCodes = array();
    protected $exceptionMessages = array();
    protected $debug = false;

    public function __construct(Application $app, Twig_Environment $twig, array $exceptionCodes, array $exceptionMessages, $debug = false)
    {
        $this->app = $app;
        $this->twig = $twig;

        $this->exceptionCodes = $exceptionCodes;
        $this->exceptionMessages = $exceptionMessages;

        $this->debug = $debug;
    }

    protected function isJsonRequest(Request $request)
    {
        $result = false;

        $headers = $request->headers;

        if (strpos($headers->get('Accept'), 'application/json') !== false) {
            $result = true;
        }

        if (strpos($headers->get('Content-Type'), 'application/json') !== false) {
            $result = true;
        }

        return $result;
    }

    public function route()
    {
        $app = $this->app;
        $exceptionCodes = $this->exceptionCodes;

        $app->error(function (\Exception $e, $code) use ($app, $exceptionCodes) {
            $request = $app['request_stack']->getCurrentRequest();
            $exceptionClass = get_class($e);

            if (isset($exceptionCodes[$exceptionClass])) {
                $code = (int)$exceptionCodes[$exceptionClass];
            } else {
                $code = 500;
            }

            if ($this->isJsonRequest($request)) {
                return $this->jsonError($e, $code);
            } else {
                return $this->htmlError($e, $code);
            }
        });
    }

    protected function getErrorDetails(\Exception $exception, $code)
    {
        if (isset($this->exceptionMessages[$code])) {
            $error = $this->exceptionMessages[$code];
        } else {
            $error = $exception->getMessage();
        }

        if ($this->debug) {
            $message = $exception->getMessage();
<commit_msg>Code clean-up in error router<commit_after><?php

namespace Symlex\Router;

use Silex\Application;
use Twig_Environment;
use Twig_Error_Loader;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

/**
 * @author <NAME> <<EMAIL>>
 * @license MIT
 */
class ErrorRouter
{
    protected $app;
    protected $twig;
    protected $exceptionCodes = array();
    protected $exceptionMessages = array();
    protected $debug = false;

    public function __construct(Application $app, Twig_Environment $twig, array $exceptionCodes, array $exceptionMessages, $debug = false)
    {
        $this->app = $app;
        $this->twig = $twig;

        $this->exceptionCodes = $exceptionCodes;
        $this->exceptionMessages = $exceptionMessages;

        $this->debug = $debug;
    }

    /**
     * @return Request
     */
    protected function getRequest()
    {
        $result = $this->app['request_stack']->getCurrentRequest();

        return $result;
    }

    protected function isJsonRequest()
    {
        $headers = $this->getRequest()->headers;

        $result = false;

        if (strpos($headers->get('Accept'), 'application/json') !== false) {
            $result = true;
        }

        if (strpos($headers->get('Content-Type'), 'application/json') !== false) {
            $result = true;
        }

        return $result;
    }

    public function route()
    {
        $exceptionCodes = $this->exceptionCodes;

        $this->app->error(function (\Exception $e, $code) use ($exceptionCodes) {
            $exceptionClass = get_class($e);

            if (isset($exceptionCodes[$exceptionClass])) {
                $code = (int)$exceptionCodes[$exceptionClass];
            } else {
                $code = 500;
            }

            if ($this->isJsonRequest()) {
                return $this->jsonError($e, $code);
            } else {
                return $this->htmlError($e, $code);
            }
        });
    }

    protected function getErrorDetails(\Exception $exception, $code)
    {
        if (isset($this->exceptionMessages[$code])) {
            $error = $this->exceptionMessages[$code];
        } else {
            $error = $exception->getMessage();
        }

        if ($this->debug) {
            $message = $exception->getMessage();

{'fraction_non_alphanumeric': 0.1426056338028169, 'fraction_numerical': 0.0013204225352112676, 'mean_word_length': 2.239486813970064, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 51, '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': '2821101', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1249, 'n_words': 261}