Passed
Push — master ( a51a38...e61121 )
by 世昌
01:50
created

DumpTrait::dumpTrace()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
c 0
b 0
f 0
nc 6
nop 3
dl 0
loc 14
rs 9.9666
1
<?php
2
namespace nebula\component\debug\attach;
3
4
/**
5
 * 打印
6
 */
7
trait DumpTrait
8
{
9
    protected static $dumpStringLength = 255;
10
    protected static $dumpStringShowLength = 80;
11
12
    protected static function objectToString(object $object, int $deep):string
13
    {
14
        $objectName = get_class($object);
15
        $parameterString = '';
16
        if ($deep > 0) {
17
            $vars = get_class_vars($objectName);
18
            foreach ($vars as $key => $value) {
19
                $parameterString.=static::valueToString($key, $value, $deep);
20
            }
21
        } else {
22
            $parameterString = '...';
23
        }
24
        return $objectName.' {'.trim($parameterString, ',').'}';
25
    }
26
    
27
    protected static function arrayToString(array $array, int $deep)
0 ignored issues
show
Unused Code introduced by
The parameter $array is not used and could be removed. ( Ignorable by Annotation )

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

27
    protected static function arrayToString(/** @scrutinizer ignore-unused */ array $array, int $deep)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
    {
29
        $parameterString = '';
30
        if ($deep > 0) {
31
            foreach ($object as $key => $value) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $object seems to be never defined.
Loading history...
32
                $parameterString.=static::valueToString($key, $value, $deep);
33
            }
34
        } else {
35
            $parameterString = '...';
36
        }
37
        return '['.trim($parameterString, ',').']';
38
    }
39
40
    /**
41
     * 属性转换成字符串
42
     *
43
     * @param string $key
44
     * @param mixed $value
45
     * @param integer $deep
46
     * @return string
47
     */
48
    protected static function valueToString(string $key, $value, int $deep):string
49
    {
50
        if (is_string($value) && strlen($value) > static::$attachStringLength) {
51
            return  $key.'='.json_encode(substr($value, 0, static::$attachStringShowLength), JSON_UNESCAPED_UNICODE) .'...,';
52
        } else {
53
            return $key.'='.self::parameterToString($value, $deep-1) .',';
54
        }
55
    }
56
57
    public static function parameterToString($object, int $deep=2)
58
    {
59
        if (is_null($object)) {
60
            return 'NULL';
61
        } elseif (is_object($object)) {
62
            return static::objectToString($object, $deep);
63
        } elseif (is_array($object)) {
64
            return static::arrayToString($object, $deep);
65
        }
66
        return $object;
67
    }
68
69
    public static function dumpTrace(array $backtrace, bool $str=true, string $perfix='')
70
    {
71
        $tracesConsole=[];
72
        foreach ($backtrace as $trace) {
73
            $tracesConsole[]=static::buildTraceLine($trace);
74
        }
75
        if ($str) {
76
            $str='';
77
            foreach ($tracesConsole as $trace_info) {
78
                $str.=$perfix.preg_replace('/\n/', "\n".$perfix."\t", $trace_info).PHP_EOL;
79
            }
80
            return $str;
81
        }
82
        return  $tracesConsole;
83
    }
84
85
    protected static function buildTraceLine(array $trace):string
86
    {
87
        $line = '';
88
        if (isset($trace['file'])) {
89
            $line = $trace['file'].':'.$trace['line'];
90
        }
91
        if (isset($trace['class'])) {
92
            $function = $trace['class'].$trace['type'].$trace['function'];
93
        } else {
94
            $function = $trace['function'];
95
        }
96
        $argsDump='';
97
        if (!empty($trace['args'])) {
98
            foreach ($trace['args'] as $arg) {
99
                $argsDump.= self::parameterToString($arg) .',';
100
            }
101
            $argsDump = rtrim($argsDump, ',');
102
        }
103
        $line.=' '.$function.'('.$argsDump.')';
104
        return $line;
105
    }
106
107
    public static function dumpException(\Exception $e)
108
    {
109
        $dump = $e->getMessage() .PHP_EOL;
110
        $dump.= 'At: ' . $e->getFile().':'.$e->getLine().PHP_EOL;
111
        $dump.= static::printTrace($e->getTrace());
112
        return $dump;
113
    }
114
}
115