<commit_before><?php

    namespace AloFramework\Handlers;

    use AloFramework\Common\Alo;
    use AloFramework\Config\Configurable;
    use AloFramework\Config\ConfigurableTrait;
    use AloFramework\Handlers\Config\AbstractConfig;
    use AloFramework\Handlers\Output\ConsoleOutput;
    use AloFramework\Log\Log;
    use AloFramework\Handlers\Output\Dump;
    use Psr\Log\LoggerInterface;
    use Symfony\Component\VarDumper\VarDumper;

    /**
     * Abstract error/exception handling things
     * @author Art <<EMAIL>>
     * @since  1.4 Implements Configurable
     * @property AbstractConfig $config Abstract handler configuration
     */
    abstract class AbstractHandler implements Configurable {

        use ConfigurableTrait;

        /**
         * The line ender for __toString()
         * @var string
         */
        const EOL = " \n";

        /**
         * Whether CSS has been injected yet
         *
         * @var bool
         */
        private static $cssInjected = false;

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

        /**
         * Whether we're dealing with a command-line request
         * @var bool
         */
        protected $isCLI;

        /**
         * ConsoleOutput object
         * @var ConsoleOutput
         */
        protected $console;

        /**
         * Constructor
         * @author Art <<EMAIL>>
         *
         * @param LoggerInterface $logger If provided, this will be used to log errors and exceptions.
         * @param AbstractConfig  $cfg    The configuration class. Required.
         *
         * @since  1.4 $cfg added. This will become the first parameter in the constructor in 2.0
         */
        function __construct(LoggerInterface $logger = null, AbstractConfig $cfg = null) {
            if (!$logger) {
                $logger = new Log();
            }

            $this->config = Alo::ifnull($cfg, new AbstractConfig());
            $this->logger = $logger;
            $this->isCLI  = !$this->config->forceHTML && Alo::isCliRequest();

            $this->initSymfony();
        }

        /**
         * Initialises Symfony's components
         * @author Art <<EMAIL>>
         * @return self
         */
        private function initSymfony() {
            if ($this->isCLI) {
                $this->console = new ConsoleOutput();
            }

            return $this;
        }

        /**
         * Returns a string representation of the object
         * @author Art <<EMAIL>>
         * @return string
         */
        function __toString() {
            return 'CSS injected: ' . (self::$cssInjected ? 'Yes' : 'No') . self::EOL . 'Logger: ' .
                   ($this->logger ? get_class($this->logger) : 'Not set') . self::EOL . 'Max stack trace size: ' .
                   ($this->config->traceDepth);
        }

        /**
         * Injects the error handler CSS if it hasn't been injected yet
         * @author Art <<EMAIL>>
         */
        protected function injectCSS() {
            if (!$this->isCLI && !self::$cssInjected) {
                self::$cssInjected = true;
                if (file_exists($this->config->cssPath)) {
                    echo '<style type="text/css">';
                    include $this->config->cssPath;
                    echo '</style>';
                } else {
                    echo 'The AloFramework handlers\' CSS file could not be found: ' . $this->config->cssPath . PHP_EOL;
                }
            }
        }

        /**
         * Returns the formatted debug backtrace
         *
         * @author Art <<EMAIL>>
         *
         * @param array  $trace The debug backtrace
         * @param string $label Trace label style
         *
         * @return string
         */
        protected function getTrace($trace, $label) {
            ob_start();
            $trace = array_slice($trace, $this->config->traceDepth);

            // @codeCoverageIgnoreStart
            if ($this->isCLI) {
                $this->traceCLI($trace, $label);
            } else {
                // @codeCoverageIgnoreEnd
                $this->traceHTML($trace);
            }

            return ob_get_clean();
        }

        /**
         * CLI output of the debug backtrace
         * @author Art <<EMAIL>>
         *<commit_msg>fixed abstract handler tracing<commit_after><?php

    namespace AloFramework\Handlers;

    use AloFramework\Common\Alo;
    use AloFramework\Config\Configurable;
    use AloFramework\Config\ConfigurableTrait;
    use AloFramework\Handlers\Config\AbstractConfig;
    use AloFramework\Handlers\Output\ConsoleOutput;
    use AloFramework\Log\Log;
    use AloFramework\Handlers\Output\Dump;
    use Psr\Log\LoggerInterface;
    use Symfony\Component\VarDumper\VarDumper;

    /**
     * Abstract error/exception handling things
     * @author Art <<EMAIL>>
     * @since  1.4 Implements Configurable
     * @property AbstractConfig $config Abstract handler configuration
     */
    abstract class AbstractHandler implements Configurable {

        use ConfigurableTrait;

        /**
         * The line ender for __toString()
         * @var string
         */
        const EOL = " \n";

        /**
         * Whether CSS has been injected yet
         *
         * @var bool
         */
        private static $cssInjected = false;

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

        /**
         * Whether we're dealing with a command-line request
         * @var bool
         */
        protected $isCLI;

        /**
         * ConsoleOutput object
         * @var ConsoleOutput
         */
        protected $console;

        /**
         * Constructor
         * @author Art <<EMAIL>>
         *
         * @param LoggerInterface $logger If provided, this will be used to log errors and exceptions.
         * @param AbstractConfig  $cfg    The configuration class. Required.
         *
         * @since  1.4 $cfg added. This will become the first parameter in the constructor in 2.0
         */
        function __construct(LoggerInterface $logger = null, AbstractConfig $cfg = null) {
            if (!$logger) {
                $logger = new Log();
            }

            $this->config = Alo::ifnull($cfg, new AbstractConfig());
            $this->logger = $logger;
            $this->isCLI  = !$this->config->forceHTML && Alo::isCliRequest();

            $this->initSymfony();
        }

        /**
         * Initialises Symfony's components
         * @author Art <<EMAIL>>
         * @return self
         */
        private function initSymfony() {
            if ($this->isCLI) {
                $this->console = new ConsoleOutput();
            }

            return $this;
        }

        /**
         * Returns a string representation of the object
         * @author Art <<EMAIL>>
         * @return string
         */
        function __toString() {
            return 'CSS injected: ' . (self::$cssInjected ? 'Yes' : 'No') . self::EOL . 'Logger: ' .
                   ($this->logger ? get_class($this->logger) : 'Not set') . self::EOL . 'Max stack trace size: ' .
                   ($this->config->traceDepth);
        }

        /**
         * Injects the error handler CSS if it hasn't been injected yet
         * @author Art <<EMAIL>>
         */
        protected function injectCSS() {
            if (!$this->isCLI && !self::$cssInjected) {
                self::$cssInjected = true;
                if (file_exists($this->config->cssPath)) {
                    echo '<style type="text/css">';
                    include $this->config->cssPath;
                    echo '</style>';
                } else {
                    echo 'The AloFramework handlers\' CSS file could not be found: ' . $this->config->cssPath . PHP_EOL;
                }
            }
        }

        /**
         * Returns the formatted debug backtrace
         *
         * @author Art <<EMAIL>>
         *
         * @param array  $trace The debug backtrace
         * @param string $label Trace label style
         *
         * @return string
         */
        protected function getTrace($trace, $label) {
            ob_start();
            $trace = array_slice($trace, $this->config->traceDepth * -1);

            // @codeCoverageIgnoreStart
            if ($this->isCLI) {
                $this->traceCLI($trace, $label);
            } else {
                // @codeCoverageIgnoreEnd
                $this->traceHTML($trace);
            }

            return ob_get_clean();
        }

        /**
         * CLI output of the debug backtrace
         * @author Art <<EMAIL>>
         *
{'fraction_non_alphanumeric': 0.11184661036293084, 'fraction_numerical': 0.001483679525222552, 'mean_word_length': 1.5101690060154684, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 83, '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': '336033', 'n_tokens_mistral': 2325, 'n_tokens_neox': 2240, 'n_words': 594}