<commit_before><?php

/*
 * This file is part of the Moss micro-framework
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Moss\Kernel;

/**
 * Moss exception handler
 *
 * @package Moss Kernel
 * @author  <NAME> <<EMAIL>>
 */
class ExceptionHandler
{

    private $details;

    /**
     * Constructor
     *
     * @param bool $verbose
     */
    public function __construct($verbose = false)
    {
        $this->verbose($verbose);
    }

    /**
     * Sets reporting details
     *
     * @param bool $verbose
     *
     * @return $this
     */
    public function verbose($verbose = false)
    {
        $this->details = (bool) $verbose;

        return $this;
    }

    /**
     * Registers handler
     */
    public function register()
    {
        set_exception_handler($this->details ? [$this, 'handlerVerbose'] : [$this, 'handlerTerse']);
    }


    /**
     * Unregisters handler
     */
    public function unregister()
    {
        restore_exception_handler();
    }

    /**
     * Terse exception handler
     * Sends only simplified exception message in plain text
     *
     * @param \Exception $exception
     */
    public function handlerTerse(\Exception $exception)
    {
        if (!headers_sent()) {
            header('HTTP/1.1 500 Internal Server Error', true, 500);
            header('Content-type: text/plain; charset=UTF-8');
        }
        echo sprintf('Bad Moss: %s ( %s at line:%s )', $exception->getMessage(), $exception->getFile(), $exception->getLine());
    }

    /**
     * Verbose exception handler
     * Sends HTML message with file where error occurred and stack trace
     *
     * @param \Exception $exception
     */
    public function handlerVerbose(\Exception $exception)
    {
        if (!headers_sent()) {
            header('HTTP/1.1 500 Internal Server Error', true, 500);
            header('Content-type: text/html; charset=UTF-8');
        }

        echo sprintf(
            '<!DOCTYPE html>
        <html>
        <head>
            <title>Bad Moss: %1$s - %2$s - %3$s:%4$u</title>
            <style>
                body, code { font: medium/1.5em monospace; }

                div:nth-child(4) { position: fixed; right: 0.5em; top: 0.5em; width: 100%; padding: 0.25em 1em; }
                div:nth-child(4) a { text-decoration: none; padding: 0.25em 0.75em; color: #444; background: #aef; border-radius: 1em; }

                table { width: auto; border-collapse: collapse; overflow: hidden; }

                td { vertical-align: top; }
                td:nth-child(1) { position: relative; width: 3em; padding: 0 0.5em 0 0; text-align: right; color: #999; border-right: 1px solid #999; z-index: 1; }
                td:nth-child(2) { position: relative; padding: 0 0 0 0.5em; z-index: 2; }

                td, td span { white-space: nowrap; }
                td span#mark { position: relative; font-weight: bold; color: #f00; }
                td span#mark:after { content: \'.\'; position: absolute; top: -0.2em; left: -2em; width: 10000em; background: #f00; opacity: 0.25; }
            </style>
        </head>
        <body>
            <h1>Bad Moss: %1$s - &quot;%2$s&quot;</h1>

                <div>
                    <h2 id="trace">Trace</h2>
                    %6$s
                </div>
                <div>
                    <h2 id="listing">File: %3$s:%4$u</h2>
                    %5$s
                </div>
                <div>
                    <a href="#trace">Trace</a>
                    <a href="#listing">Listing</a>
                    <a href="#mark">Error line</a>
                </div>
        </body>
        </html>',
            get_class($exception),
            $exception->getMessage(),
            $exception->getFile(),
            $exception->getLine(),
            $this->lineNum('<br />', highlight_file($exception->getFile(), true), $exception->getLine()),
            $this->colorify($exception->getTrace())
        );
    }

    /**
     * Adds line numbers
     *
     * @param string $lineSeparator
     * @param string $source
     * @param int   $mark
     *
     * @return string
     */
    protected function lineNum($lineSeparator, $source, $mark = null)
    {
        $count = count(explode($lineSeparator, $source));
        $tpl = '<span %s>%u</span>';

        $lines = [];
        for ($i = 0; $i < $count; $i++) {
            $lines[] = sprintf($tpl, '', $i + 1);
        }

        $lines[$mark - 1] = sprintf($tpl, 'id="mark"', $mark);

        return sprintf('<table><tr><td>%s</td><td>%s</td></tr></table>', implode($lineSeparator, $lines), $source);
    }

    /**
     * Adds colors
     *
     * @param mixed $var
     *
     * @return string
     */
    public function colorify($var)
    {
        ob_start();

        if (!extension_loaded('xdebug')) {
            xdebug_var_dump($var);
        } else {
            echo '<pre>';
            var_dump($var);
            echo '</pre>';
        }

        return ob_get_clean();
    }
}
<commit_msg>xdebug fix<commit_after><?php

/*
 * This file is part of the Moss micro-framework
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Moss\Kernel;

/**
 * Moss exception handler
 *
 * @package Moss Kernel
 * @author  <NAME> <<EMAIL> <EMAIL>>
 */
class ExceptionHandler
{

    private $details;

    /**
     * Constructor
     *
     * @param bool $verbose
     */
    public function __construct($verbose = false)
    {
        $this->verbose($verbose);
    }

    /**
     * Sets reporting details
     *
     * @param bool $verbose
     *
     * @return $this
     */
    public function verbose($verbose = false)
    {
        $this->details = (bool) $verbose;

        return $this;
    }

    /**
     * Registers handler
     */
    public function register()
    {
        set_exception_handler($this->details ? [$this, 'handlerVerbose'] : [$this, 'handlerTerse']);
    }


    /**
     * Unregisters handler
     */
    public function unregister()
    {
        restore_exception_handler();
    }

    /**
     * Terse exception handler
     * Sends only simplified exception message in plain text
     *
     * @param \Exception $exception
     */
    public function handlerTerse(\Exception $exception)
    {
        if (!headers_sent()) {
            header('HTTP/1.1 500 Internal Server Error', true, 500);
            header('Content-type: text/plain; charset=UTF-8');
        }
        echo sprintf('Bad Moss: %s ( %s at line:%s )', $exception->getMessage(), $exception->getFile(), $exception->getLine());
    }

    /**
     * Verbose exception handler
     * Sends HTML message with file where error occurred and stack trace
     *
     * @param \Exception $exception
     */
    public function handlerVerbose(\Exception $exception)
    {
        if (!headers_sent()) {
            header('HTTP/1.1 500 Internal Server Error', true, 500);
            header('Content-type: text/html; charset=UTF-8');
        }

        echo sprintf(
            '<!DOCTYPE html>
        <html>
        <head>
            <title>Bad Moss: %1$s - %2$s - %3$s:%4$u</title>
            <style>
                body, code { font: medium/1.5em monospace; }

                div:nth-child(4) { position: fixed; right: 0.5em; top: 0.5em; width: 100%; padding: 0.25em 1em; }
                div:nth-child(4) a { text-decoration: none; padding: 0.25em 0.75em; color: #444; background: #aef; border-radius: 1em; }

                table { width: auto; border-collapse: collapse; overflow: hidden; }

                td { vertical-align: top; }
                td:nth-child(1) { position: relative; width: 3em; padding: 0 0.5em 0 0; text-align: right; color: #999; border-right: 1px solid #999; z-index: 1; }
                td:nth-child(2) { position: relative; padding: 0 0 0 0.5em; z-index: 2; }

                td, td span { white-space: nowrap; }
                td span#mark { position: relative; font-weight: bold; color: #f00; }
                td span#mark:after { content: \'.\'; position: absolute; top: -0.2em; left: -2em; width: 10000em; background: #f00; opacity: 0.25; }
            </style>
        </head>
        <body>
            <h1>Bad Moss: %1$s - &quot;%2$s&quot;</h1>

                <div>
                    <h2 id="trace">Trace</h2>
                    %6$s
                </div>
                <div>
                    <h2 id="listing">File: %3$s:%4$u</h2>
                    %5$s
                </div>
                <div>
                    <a href="#trace">Trace</a>
                    <a href="#listing">Listing</a>
                    <a href="#mark">Error line</a>
                </div>
        </body>
        </html>',
            get_class($exception),
            $exception->getMessage(),
            $exception->getFile(),
            $exception->getLine(),
            $this->lineNum('<br />', highlight_file($exception->getFile(), true), $exception->getLine()),
            $this->colorify($exception->getTrace())
        );
    }

    /**
     * Adds line numbers
     *
     * @param string $lineSeparator
     * @param string $source
     * @param int   $mark
     *
     * @return string
     */
    protected function lineNum($lineSeparator, $source, $mark = null)
    {
        $count = count(explode($lineSeparator, $source));
        $tpl = '<span %s>%u</span>';

        $lines = [];
        for ($i = 0; $i < $count; $i++) {
            $lines[] = sprintf($tpl, '', $i + 1);
        }

        $lines[$mark - 1] = sprintf($tpl, 'id="mark"', $mark);

        return sprintf('<table><tr><td>%s</td><td>%s</td></tr></table>', implode($lineSeparator, $lines), $source);
    }

    /**
     * Adds colors
     *
     * @param mixed $var
     *
     * @return string
     */
    public function colorify($var)
    {
        ob_start();

        if (extension_loaded('xdebug')) {
            xdebug_var_dump($var);
        } else {
            echo '<pre>';
            var_dump($var);
            echo '</pre>';
        }

        return ob_get_clean();
    }
}

{'fraction_non_alphanumeric': 0.15428849902534114, 'fraction_numerical': 0.01929824561403509, 'mean_word_length': 1.7635335308375977, 'pattern_counts': {'":': 0, '<': 104, '<?xml version=': 0, '>': 128, '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': '5877642', 'n_tokens_mistral': 3279, 'n_tokens_neox': 3076, 'n_words': 758}