<commit_before><?php

namespace voku\db;

use voku\helper\Bootup;
use voku\helper\UTF8;

/**
 * Debug: this handles debug and error-logging.
 *
 * @package   voku\db
 */
class Debug
{
  /**
   * @var array
   */
  private $_errors = array();

  /**
   * @var bool
   */
  private $exit_on_error = true;

  /**
   * @var bool
   */
  private $echo_on_error = true;

  /**
   * @var string
   */
  private $css_mysql_box_border = '3px solid red';

  /**
   * @var string
   */
  private $css_mysql_box_bg = '#FFCCCC';


  /**
   * @var string
   */
  private $logger_class_name;

  /**
   * @var DB
   */
  private $_db;

  /**
   * @var string
   *
   * 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'
   */
  private $logger_level;

  /**
   * Debug constructor.
   *
   * @param DB $db
   */
  public function __construct(DB $db)
  {
    $this->_db = $db;
  }

  /**
   * Check is the current user is a developer.
   *
   * INFO:
   * By default we will return "true" if the remote-ip-address is localhost or
   * if the script is called via CLI. But you can also overwrite this method or
   * you can implement a global "checkForDev()"-function.
   *
   * @return bool
   */
  public function checkForDev()
  {
    // init
    $return = false;

    if (function_exists('checkForDev')) {
      $return = checkForDev();
    } else {

      // for testing with dev-address
      $noDev = isset($_GET['noDev']) ? (int)$_GET['noDev'] : 0;
      $remoteIpAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;

      if (
          $noDev != 1
          &&
          (
              $remoteIpAddress === '127.0.0.1'
              ||
              $remoteIpAddress === '::1'
              ||
              PHP_SAPI === 'cli'
          )
      ) {
        $return = true;
      }
    }

    return $return;
  }

  /**
   * Clear the errors in "$this->_errors".
   *
   * @return bool
   */
  public function clearErrors()
  {
    $this->_errors = array();

    return true;
  }

  /**
   * Display SQL-Errors or throw Exceptions (for dev).
   *
   * @param string       $error
   * @param null|boolean $force_exception_after_error
   *
   * @throws \Exception
   */
  public function displayError($error, $force_exception_after_error = null)
  {
    $fileInfo = $this->getFileAndLineFromSql();

    $this->logger(
        array(
            'error',
            '<strong>' . date(
                'd. m. Y G:i:s'
            ) . ' (' . $fileInfo['file'] . ' line: ' . $fileInfo['line'] . ') (sql-error):</strong> ' . $error . '<br>',
        )
    );

    $this->_errors[] = $error;

    if ($this->checkForDev() === true) {

      if ($this->echo_on_error) {
        $box_border = $this->css_mysql_box_border;
        $box_bg = $this->css_mysql_box_bg;

        echo '
        <div class="OBJ-mysql-box" style="border:' . $box_border . '; background:' . $box_bg . '; padding:10px; margin:10px;">
          <b style="font-size:14px;">MYSQL Error:</b>
          <code style="display:block;">
            file / line: ' . $fileInfo['file'] . ' / ' . $fileInfo['line'] . '
            ' . $error . '
          </code>
        </div>
        ';
      }

      if ($force_exception_after_error === true) {
        throw new \Exception($error);
      } elseif ($force_exception_after_error === false) {
        // nothing
      } elseif ($force_exception_after_error === null) {
        // default
        if ($this->exit_on_error === true) {
          throw new \Exception($error);
        }
      }
    }<commit_msg>[~]: update php-doc<commit_after><?php

namespace voku\db;

use voku\helper\Bootup;
use voku\helper\UTF8;

/**
 * Debug: this handles debug and error-logging.
 *
 * @package   voku\db
 */
class Debug
{
  /**
   * @var array
   */
  private $_errors = array();

  /**
   * @var bool
   */
  private $exit_on_error = true;

  /**
   * @var bool
   */
  private $echo_on_error = true;

  /**
   * @var string
   */
  private $css_mysql_box_border = '3px solid red';

  /**
   * @var string
   */
  private $css_mysql_box_bg = '#FFCCCC';


  /**
   * @var string
   */
  private $logger_class_name;

  /**
   * @var DB
   */
  private $_db;

  /**
   * @var string
   *
   * 'TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL'
   */
  private $logger_level;

  /**
   * Debug constructor.
   *
   * @param DB $db
   */
  public function __construct(DB $db)
  {
    $this->_db = $db;
  }

  /**
   * Check is the current user is a developer.
   *
   * INFO:
   * By default we will return "true" if the remote-ip-address is localhost or
   * if the script is called via CLI. But you can also overwrite this method or
   * you can implement a global "checkForDev()"-function.
   *
   * @return bool
   */
  public function checkForDev()
  {
    // init
    $return = false;

    if (function_exists('checkForDev')) {
      $return = checkForDev();
    } else {

      // for testing with dev-address
      $noDev = isset($_GET['noDev']) ? (int)$_GET['noDev'] : 0;
      $remoteIpAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;

      if (
          $noDev != 1
          &&
          (
              $remoteIpAddress === '127.0.0.1'
              ||
              $remoteIpAddress === '::1'
              ||
              PHP_SAPI === 'cli'
          )
      ) {
        $return = true;
      }
    }

    return $return;
  }

  /**
   * Clear the errors in "$this->_errors".
   *
   * @return bool
   */
  public function clearErrors()
  {
    $this->_errors = array();

    return true;
  }

  /**
   * Display SQL-Errors or throw Exceptions (for dev).
   *
   * @param string       $error                       <p>The error message.</p>
   * @param null|boolean $force_exception_after_error <p>
   *                                                  If you use default "null" here, then the behavior depends
   *                                                  on "$this->exit_on_error (default: true)".
   *                                                  </p>
   *
   * @throws \Exception
   */
  public function displayError($error, $force_exception_after_error = null)
  {
    $fileInfo = $this->getFileAndLineFromSql();

    $this->logger(
        array(
            'error',
            '<strong>' . date(
                'd. m. Y G:i:s'
            ) . ' (' . $fileInfo['file'] . ' line: ' . $fileInfo['line'] . ') (sql-error):</strong> ' . $error . '<br>',
        )
    );

    $this->_errors[] = $error;

    if ($this->checkForDev() === true) {

      if ($this->echo_on_error) {
        $box_border = $this->css_mysql_box_border;
        $box_bg = $this->css_mysql_box_bg;

        echo '
        <div class="OBJ-mysql-box" style="border:' . $box_border . '; background:' . $box_bg . '; padding:10px; margin:10px;">
          <b style="font-size:14px;">MYSQL Error:</b>
          <code style="display:block;">
            file / line: ' . $fileInfo['file'] . ' / ' . $fileInfo['line'] . '
            ' . $error . '
          </code>
        </div>
        ';
      }

      if ($force_exception_after_error === true) {
        throw new \Exception($error);
      }

      if ($force_exception_after_error === false) {
        // nothing
      } elseif ($force_exception_after_error === null) {
        // default
        if ($this->exit_on_error === true) {
          throw new \Exception($error);
        }
      }
    }
{'fraction_non_alphanumeric': 0.1611855460820138, 'fraction_numerical': 0.004601434564893761, 'mean_word_length': 1.7781954887218046, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4515364', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2398, 'n_words': 533}