Completed
Pull Request — master (#234)
by Дмитрий
07:51
created

Debug::backtrace()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 4
dl 0
loc 22
rs 8.9197
eloc 17
nc 2
nop 1
1
<?php
2
namespace PHPDaemon\Core;
3
4
use PHPDaemon\Core\Daemon;
5
6
/**
7
 * Debug static functions
8
 * @package PHPDaemon\Core
9
 * @author  Vasily Zorin <[email protected]>
10
 */
11
class Debug
12
{
13
    use \PHPDaemon\Traits\ClassWatchdog;
14
    use \PHPDaemon\Traits\StaticObjectWatchdog;
15
16
    /**
17
     * Export binary data
18
     * @param  string  $str String
19
     * @param  boolean $all Whether to replace all of chars with escaped sequences
20
     * @return string Escaped string
21
     */
22
    public static function exportBytes($str, $all = false)
23
    {
24
        return preg_replace_callback(
25
            '~' . ($all ? '.' : '[^A-Za-z\d\.\{\}$<>:;\-_/\\\\=+]') . '~s',
26
            function ($m) use ($all) {
27
                if (!$all) {
28
                    if ($m[0] === "\r") {
29
                        return "\n" . '\r';
30
                    }
31
32
                    if ($m[0] === "\n") {
33
                        return '\n';
34
                    }
35
                }
36
37
                return sprintf('\x%02x', ord($m[0]));
38
            }, $str);
39
    }
40
41
    /**
42
     * Returns pretty-printed JSON
43
     * @param  mixed  $m Data
44
     * @return string
45
     */
46
    public static function prettyJson($m)
47
    {
48
        return json_encode($m, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
49
    }
50
51
    /**
52
     * Returns JSON
53
     * @param  mixed  $m Data
54
     * @return string
55
     */
56
    public static function json($m)
57
    {
58
        return json_encode($m, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
59
    }
60
61
    /**
62
     * Returns a proxy callback function with logging for debugging purposes
63
     * @param  callable $cb   Callback
64
     * @param  mixed    $name Data
65
     * @return callable
66
     */
67
    public static function proxy($cb, $name = null)
68
    {
69
        static $i = 0;
70
        $n = ++$i;
71
        Daemon::log('Debug::proxy #'.$n.': SPAWNED ('.json_encode($name). ')');
72
        return function () use ($cb, $name, $n) {
73
            Daemon::log('Debug::proxy #'.$n.': CALLED ('.json_encode($name). ')');
74
            $cb(...func_get_args());
75
        };
76
    }
77
78
    /**
79
     * Wrapper of var_dump
80
     * @return string Result of var_dump()
81
     */
82 View Code Duplication
    public static function dump()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        ob_start();
85
86
        foreach (func_get_args() as $v) {
87
            var_dump($v);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($v); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
88
        }
89
90
        $dump = ob_get_contents();
91
        ob_end_clean();
92
93
        return $dump;
94
    }
95
96
    /**
97
     * Get refcount of the given variable
98
     * @param  mixed &$var Value
99
     * @return integer
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
100
     */
101
    public static function refcount(&$var)
102
    {
103
        ob_start();
104
        debug_zval_dump([&$var]);
105
        $c = preg_replace("/^.+?refcount\((\d+)\).+$/ms", '$1', substr(ob_get_contents(), 24), 1) - 4;
106
        ob_end_clean();
107
        return $c;
108
    }
109
110
    /**
111
     * Wrapper of debug_zval_dump
112
     * @return string Result of debug_zval_dump()
113
     */
114 View Code Duplication
    public static function zdump()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        ob_start();
117
118
        foreach (func_get_args() as $v) {
119
            debug_zval_dump($v);
120
        }
121
122
        $dump = ob_get_contents();
123
        ob_end_clean();
124
125
        return $dump;
126
    }
127
128
    /**
129
     * Returns textual backtrace
130
     * @return string
131
     */
132
    public static function backtrace($bool = false)
133
    {
134
        if (Daemon::$obInStack || $bool) {
135
            try {
136
                throw new \Exception;
137
            } catch (\Exception $e) {
138
                $trace = $e->getTraceAsString();
139
                $e     = explode("\n", $trace);
140
                array_shift($e);
141
                array_shift($e);
142
                return implode("\n", $e);
143
            }
144
        }
145
        ob_start();
146
        debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
147
        $trace = ob_get_contents();
148
        ob_end_clean();
149
150
        $e = explode("\n", $trace);
151
        array_shift($e);
152
        return implode("\n", $e);
153
    }
154
}
155