Passed
Push — main ( f76850...0882b5 )
by Sammy
01:29
created

Debugger.class.php (3 issues)

1
<?php
2
3
namespace HexMakina\Debugger
4
{
5
  trait Debugger
6
  {
7
    public function __debugInfo()
8
    {
9
      return [json_encode(get_object_vars($this))];
10
    }
11
12
    public static function init()
13
    {
14
      // just to load the class, required to get the shortcuts defined in namespace \
15
    }
16
17
    public static function display_errors($error_message=null)
18
    {
19
      $should_display = ini_get('display_errors') == '1';
20
21
      if($should_display && !empty($error_message))
22
        echo('<pre style="z-index:9999; background-color:#FFF; color:#000; padding:0.5em; font-size:0.7em; margin:0 0 1em 0; font-family:courier;">'.$error_message.'</pre>');
23
    }
24
25
    // ----------------------------------------------------------- visual dump (depends on env)
26
    public static function vd($var, $var_name = null, $full_backtrace = false)
27
    {
28
      self::display_errors(self::dump($var, $var_name, $full_backtrace));
29
    }
30
31
    // ----------------------------------------------------------- visual dump and DIE
32
    public static function dd($var, $var_name = null, $full_backtrace = true)
33
    {
34
      self::vd($var, $var_name, $full_backtrace);
35
      die;
36
    }
37
38
39
    // ----------------------------------------------------------- dump on variable type (Throwables, array, anything else)
40
    public static function dump($var, $var_name = null, $full_backtrace = true)
41
    {
42
      if(is_object($var) && (is_subclass_of($var, 'Error') || is_subclass_of($var, 'Exception')))
43
      {
44
        $backtrace = $var->getTrace();
45
        $full_backtrace = true;
46
        $var_dump  = self::format_throwable_message(get_class($var), $var->getCode(), $var->getFile(), $var->getLine(), $var->getMessage());
47
      }
48
    	else
49
    	{
50
        $backtrace = debug_backtrace();
51
52
    		ob_start();
53
    		var_dump($var);
1 ignored issue
show
Security Debugging Code introduced by
var_dump($var) looks like debug code. Are you sure you do not want to remove it?
Loading history...
54
    		$var_dump = ob_get_clean();
55
    	}
56
57
      return PHP_EOL."*******".(empty($var_name) ? '' : " ($var_name) ")."*******".PHP_EOL.self::format_trace($backtrace, $full_backtrace).PHP_EOL.$var_dump;
58
    }
59
60
    // ----------------------------------------------------------- formatting
61
62
    // ----------------------------------------------------------- formatting : first line of \Throwable-based error
63
    public static function format_throwable_message($class, $code, $file, $line, $message)
64
    {
65
      return sprintf(PHP_EOL.'%s (%d) in file %s:%d'.PHP_EOL.'%s', $class, $code, self::format_file($file), $line, $message);
66
    }
67
68
    // ----------------------------------------------------------- formatting : shorten file path to [self::REDUCE_FILE_PATH_DEPTH_TO] elements
69
    public static function format_file($file,$reduce_file_depth_to = 5)
70
    {
71
      return implode('/', array_slice(explode('/', $file), -$reduce_file_depth_to, $reduce_file_depth_to));
72
    }
73
74
    // ----------------------------------------------------------- formatting : nice backtrace
75
    public static function format_trace($traces, $full_backtrace)
76
    {
77
      $formated_traces = [];
78
79
      foreach($traces as $depth => $trace)
80
      {
81
        $function_name = $trace['function'] ?? '?';
82
83
        $class_name = $trace['class'] ?? '?';
84
85
        if(($function_name === 'dump' || $function_name === 'vd' || $function_name === 'dd') && $class_name === __CLASS__)
86
          continue;
87
88
        if($function_name !== 'vd' && $function_name !== 'dd' && $function_name !== 'vdt' && $function_name !== 'ddt' && isset($trace['args']))
89
        {
90
          $args = [];
91
          foreach($trace['args'] as $arg)
92
          {
93
            if(is_null($arg))
94
              $args[]= 'null';
95
            elseif(is_bool($arg))
96
              $args[]= $arg === true ? 'bool:true' : 'bool:false';
97
            elseif(is_string($arg) || is_numeric($arg))
98
              $args[]= $arg;
99
            elseif(is_object($arg))
100
              $args[]= get_class($arg);
101
            elseif(is_array($arg))
102
              $args[]= 'Array #'.count($arg);
103
            else
104
            {
105
              var_dump($arg);
1 ignored issue
show
Security Debugging Code introduced by
var_dump($arg) looks like debug code. Are you sure you do not want to remove it?
Loading history...
106
              $args[] = '!!OTHER!!';
107
            }
108
          }
109
          $args = implode(', ', $args);
110
        }
111
        else
112
          $args = microtime(true);
113
114
        $call_file = isset($trace['file']) ? pathinfo($trace['file'], PATHINFO_BASENAME) : '?';
115
        $call_line = $trace['line'] ?? '?';
116
117
        $formated_traces []= sprintf('[%-23.23s %3s] %'.($depth*3).'s%s%s(%s)', $call_file, $call_line,' ', "$class_name::", $function_name, $args);
1 ignored issue
show
It seems like $call_file can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
        $formated_traces []= sprintf('[%-23.23s %3s] %'.($depth*3).'s%s%s(%s)', /** @scrutinizer ignore-type */ $call_file, $call_line,' ', "$class_name::", $function_name, $args);
Loading history...
118
119
        if($full_backtrace === false)
120
          break;
121
      }
122
123
      return implode(PHP_EOL, array_reverse($formated_traces));
124
    }
125
  }
126
}
127
128
namespace
129
{
130
  if(!function_exists('vd')) {
131
	  function vd($var, $var_name=null){	return \HexMakina\Debugger\Debugger::vd($var, $var_name, false);}
132
  }
133
  if(!function_exists('dd')) {
134
	  function dd($var, $var_name=null){	return \HexMakina\Debugger\Debugger::dd($var, $var_name, false);}
135
	}
136
  if(!function_exists('vdt')) {
137
	  function vdt($var, $var_name=null){	return \HexMakina\Debugger\Debugger::vd($var, $var_name, true);}
138
	}
139
  if(!function_exists('ddt')) {
140
	  function ddt($var, $var_name=null){	return \HexMakina\Debugger\Debugger::dd($var, $var_name, true);}
141
	}
142
}
143