<commit_before><?php

class EngineBlock_Application_ErrorHandler
{
    protected $_application;
    protected $_exitHandlers = array();

    /**
     * @var EngineBlock_Application_Error|null
     */
    private $lastErrorHandled;

    public function __construct(EngineBlock_ApplicationSingleton $application)
    {
        $this->_application = $application;
    }

    /**
     *
     * @todo this naively requires that you won't push error handlers while executing the exit handler
     *
     * @param $fn
     * @param $exitHandler
     */
    public function withExitHandler($fn, $exitHandler)
    {
        $this->pushExitHandler($exitHandler);
        $fn();
        $this->popExitHandler();
    }

    public function pushExitHandler($fn)
    {
        array_push($this->_exitHandlers, $fn);
        return $this;
    }

    public function popExitHandler()
    {
        array_pop($this->_exitHandlers);
        return $this;
    }

    public function exception(Exception $e)
    {
        foreach ($this->_exitHandlers as $exitHandler) {
            $exitHandler($e);
        }

        $this->_application->reportError($e);

        $message = 'A exceptional condition occurred, it has been logged and sent to the administrator.';
        if ($this->_application->getConfiguration()->debug) {
            $message .= PHP_EOL . '<br /><br /> ERROR: ' . PHP_EOL;
            $message .= '<br /><strong style="color: red"><pre>' . var_export($e, true) . '</pre></strong>';
        }
        die($message);
    }

    public function error($errorNumber, $errorMessage, $errorFile, $errorLine)
    {
        if (!(error_reporting() & $errorNumber)) {
            // This error code is not included in error_reporting
            // Execute PHP internal error handler
            return false;
        }

        $this->lastErrorHandled = null;
        $error = new EngineBlock_Application_Error($errorNumber, $errorMessage, $errorFile, $errorLine);

        $exception = new EngineBlock_Exception(
            sprintf('%s [%s:%d]', $error->getMessage(), $error->getFile(), $error->getLine()),
            EngineBlock_Exception::CODE_ERROR
        );
        try {
            foreach ($this->_exitHandlers as $exitHandler) {
                $exitHandler($exception, $error->toArray());
            }

            $this->_application->reportError($exception);
            $this->lastErrorHandled = $error;
        }
        catch (Exception $e) {<commit_msg>Small grammar correction.<commit_after><?php

class EngineBlock_Application_ErrorHandler
{
    protected $_application;
    protected $_exitHandlers = array();

    /**
     * @var EngineBlock_Application_Error|null
     */
    private $lastErrorHandled;

    public function __construct(EngineBlock_ApplicationSingleton $application)
    {
        $this->_application = $application;
    }

    /**
     *
     * @todo this naively requires that you won't push error handlers while executing the exit handler
     *
     * @param $fn
     * @param $exitHandler
     */
    public function withExitHandler($fn, $exitHandler)
    {
        $this->pushExitHandler($exitHandler);
        $fn();
        $this->popExitHandler();
    }

    public function pushExitHandler($fn)
    {
        array_push($this->_exitHandlers, $fn);
        return $this;
    }

    public function popExitHandler()
    {
        array_pop($this->_exitHandlers);
        return $this;
    }

    public function exception(Exception $e)
    {
        foreach ($this->_exitHandlers as $exitHandler) {
            $exitHandler($e);
        }

        $this->_application->reportError($e);

        $message = 'An exceptional condition occurred, it has been logged and sent to the administrator.';
        if ($this->_application->getConfiguration()->debug) {
            $message .= PHP_EOL . '<br /><br /> ERROR: ' . PHP_EOL;
            $message .= '<br /><strong style="color: red"><pre>' . var_export($e, true) . '</pre></strong>';
        }
        die($message);
    }

    public function error($errorNumber, $errorMessage, $errorFile, $errorLine)
    {
        if (!(error_reporting() & $errorNumber)) {
            // This error code is not included in error_reporting
            // Execute PHP internal error handler
            return false;
        }

        $this->lastErrorHandled = null;
        $error = new EngineBlock_Application_Error($errorNumber, $errorMessage, $errorFile, $errorLine);

        $exception = new EngineBlock_Exception(
            sprintf('%s [%s:%d]', $error->getMessage(), $error->getFile(), $error->getLine()),
            EngineBlock_Exception::CODE_ERROR
        );
        try {
            foreach ($this->_exitHandlers as $exitHandler) {
                $exitHandler($exception, $error->toArray());
            }

            $this->_application->reportError($exception);
            $this->lastErrorHandled = $error;
        }
        catch (Exception $e) {
{'fraction_non_alphanumeric': 0.12841253791708795, 'fraction_numerical': 0.0, 'mean_word_length': 2.350948509485095, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 57, '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': '3010968', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1310, 'n_words': 306}