<commit_before><?php

namespace PHPCI\Logging;

use Psr\Log\LoggerInterface;

class Handler
{
    /**
     * @var array
     */
    protected $levels = array(
        E_WARNING           => 'Warning',
        E_NOTICE            => 'Notice',
        E_USER_ERROR        => 'User Error',
        E_USER_WARNING      => 'User Warning',
        E_USER_NOTICE       => 'User Notice',
        E_STRICT            => 'Runtime Notice',
        E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
        E_DEPRECATED        => 'Deprecated',
        E_USER_DEPRECATED   => 'User Deprecated',
    );

    /**
     * @var LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerInterface $logger = null)
    {
        $this->logger = $logger;
    }

    public static function register(LoggerInterface $logger = null)
    {
        $handler = new static($logger);

        set_error_handler(array($handler, 'handleError'));
        register_shutdown_function(array($handler, 'handleFatalError'));

        set_exception_handler(array($handler, 'handleException'));
    }

    /**
     * @param integer $level
     * @param string  $message
     * @param string  $file
     * @param integer $line
     *
     * @throws \ErrorException
     */
    public function handleError($level, $message, $file, $line)
    {
        if (error_reporting() & $level) {

            $exception_level = isset($this->levels[$level]) ? $this->levels[$level] : $level;

            throw new \ErrorException(
                sprintf('%s: %s in %s line %d', $exception_level, $message, $file, $line),
                0,
                $level,
                $file,
                $line
            );
        }
    }

    /**
     * @throws \ErrorException
     */
    public function handleFatalError()
    {
        $fatal_error = error_get_last();

        try {
            if (($e = error_get_last()) !== null) {
                $e = new \ErrorException(
                    sprintf(
                        '%s: %s in %s line %d',
                        $fatal_error['type'],
                        $fatal_error['message'],
                        $fatal_error['file'],
                        $fatal_error['line']
                    ),
                    0,
                    $fatal_error['type'],
                    $fatal_error['file'],
                    $fatal_error['line']
                );
                $this->log($e);
            }
        }
        catch (\Exception $e) {
            $e = new \ErrorException(
                sprintf(
                    '%s: %s in %s line %d',
                    $fatal_error['type'],
                    $fatal_error['message'],
                    $fatal_error['file'],
                    $fatal_error['line']
                ),
                0,
                $fatal_error['type'],
                $fatal_error['file'],
                $fatal_error['line']
            );
            $this->log($e);
        }
    }

    /**
     * @param \Exception $exception
     */
    public function handleException(\Exception $exception)
    {<commit_msg>OH THE HORROR, a mis-spaced try-catch.<commit_after><?php

namespace PHPCI\Logging;

use Psr\Log\LoggerInterface;

class Handler
{
    /**
     * @var array
     */
    protected $levels = array(
        E_WARNING           => 'Warning',
        E_NOTICE            => 'Notice',
        E_USER_ERROR        => 'User Error',
        E_USER_WARNING      => 'User Warning',
        E_USER_NOTICE       => 'User Notice',
        E_STRICT            => 'Runtime Notice',
        E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
        E_DEPRECATED        => 'Deprecated',
        E_USER_DEPRECATED   => 'User Deprecated',
    );

    /**
     * @var LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerInterface $logger = null)
    {
        $this->logger = $logger;
    }

    public static function register(LoggerInterface $logger = null)
    {
        $handler = new static($logger);

        set_error_handler(array($handler, 'handleError'));
        register_shutdown_function(array($handler, 'handleFatalError'));

        set_exception_handler(array($handler, 'handleException'));
    }

    /**
     * @param integer $level
     * @param string  $message
     * @param string  $file
     * @param integer $line
     *
     * @throws \ErrorException
     */
    public function handleError($level, $message, $file, $line)
    {
        if (error_reporting() & $level) {

            $exception_level = isset($this->levels[$level]) ? $this->levels[$level] : $level;

            throw new \ErrorException(
                sprintf('%s: %s in %s line %d', $exception_level, $message, $file, $line),
                0,
                $level,
                $file,
                $line
            );
        }
    }

    /**
     * @throws \ErrorException
     */
    public function handleFatalError()
    {
        $fatal_error = error_get_last();

        try {
            if (($e = error_get_last()) !== null) {
                $e = new \ErrorException(
                    sprintf(
                        '%s: %s in %s line %d',
                        $fatal_error['type'],
                        $fatal_error['message'],
                        $fatal_error['file'],
                        $fatal_error['line']
                    ),
                    0,
                    $fatal_error['type'],
                    $fatal_error['file'],
                    $fatal_error['line']
                );
                $this->log($e);
            }
        } catch (\Exception $e) {
            $e = new \ErrorException(
                sprintf(
                    '%s: %s in %s line %d',
                    $fatal_error['type'],
                    $fatal_error['message'],
                    $fatal_error['file'],
                    $fatal_error['line']
                ),
                0,
                $fatal_error['type'],
                $fatal_error['file'],
                $fatal_error['line']
            );
            $this->log($e);
        }
    }

    /**
     * @param \Exception $exception
     */
    public function handleException(\Exception $exception)
    {
{'fraction_non_alphanumeric': 0.12806146950536257, 'fraction_numerical': 0.0009604610212902193, 'mean_word_length': 1.3568464730290457, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '3678555', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1654, 'n_words': 339}