Completed
Push — master ( a66420...6394df )
by Steevan
08:32
created

DebugBacktrace   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 0
loc 108
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setRemovePathPrefix() 0 4 1
C getBacktraces() 0 28 8
D getCaller() 0 28 10
A highlightCode() 0 7 1
A getFilePath() 0 16 4
1
<?php
2
3
class DebugBacktrace
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    /** @var bool|string */
6
    protected static $removePathPrefix = true;
7
8
    /** @param bool|string $remove */
9
    public static function setRemovePathPrefix($remove)
10
    {
11
        static::$removePathPrefix = $remove;
12
    }
13
14
    /**
15
     * @param int $offset
16
     * @param int|null $limit
17
     * @return array
18
     */
19
    public static function getBacktraces($offset = 0, $limit = null)
20
    {
21
        if ($limit !== null) {
22
            $limit += $offset;
23
        }
24
25
        $filteredBacktraces = [];
26
        foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $limit) as $dumpIndex => $backtrace) {
27
            if ($dumpIndex > $offset) {
28
                $filteredBacktrace = [
29
                    'file' => isset($backtrace['file']) ? $backtrace['file'] : null,
30
                    'line' => isset($backtrace['line']) ? $backtrace['line'] : null,
31
                ];
32
                if (isset($backtrace['class'])) {
33
                    $filteredBacktrace['call'] =
34
                        $backtrace['class'] . $backtrace['type'] . $backtrace['function'] . '()';
35
                } elseif (isset($backtrace['function'])) {
36
                    $filteredBacktrace['call'] = $backtrace['function'] . '()';
37
                } else {
38
                    $filteredBacktrace['call'] = '\Closure';
39
                }
40
41
                $filteredBacktraces[] = $filteredBacktrace;
42
            }
43
        }
44
45
        return array_reverse($filteredBacktraces);
46
    }
47
48
    /** @return array|null */
49
    public static function getCaller()
50
    {
51
        $backtraces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 7);
52
        $nextIsCaller = false;
53
        $caller = null;
54
        foreach ($backtraces as $backtrace) {
55
            if (
56
                isset($backtrace['file'])
57
                && strpos($backtrace['file'], __FILE__) !== false
58
            ) {
59
                $nextIsCaller = true;
60
            } elseif (
61
                $nextIsCaller
62
                && (
63
                    (isset($backtrace['file']) && strpos($backtrace['file'], __FILE__) === false)
64
                    || isset($backtrace['file']) === false
65
                )
66
            ) {
67
                $caller = $backtrace;
68
                break;
69
            }
70
        }
71
        if ($nextIsCaller === false && count($backtraces) > 0) {
72
            $caller = $backtraces[0];
73
        }
74
75
        return $caller;
76
    }
77
78
    /**
79
     * @param string $code
80
     * @return string
81
     */
82
    protected static function highlightCode($code)
83
    {
84
        $highlight = highlight_string('<?php ' . $code, true);
85
        $highlight = str_replace('>&lt;?php&nbsp;', null, $highlight);
86
87
        return $highlight;
88
    }
89
90
    /**
91
     * @param string $path
92
     * @return string
93
     */
94
    protected static function getFilePath($path)
95
    {
96
        $path = realpath($path);
97
98
        if (static::$removePathPrefix === false) {
99
            $return = $path;
100
        } else {
101
            // assume that we are in vendor/ dir
102
            $prefix = (static::$removePathPrefix === true)
103
                ? realpath(__DIR__ . '/../../../')
104
                : static::$removePathPrefix;
105
            $return = (substr($path, 0, strlen($prefix)) === $prefix) ? substr($path, strlen($prefix) + 1) : $path;
106
        }
107
108
        return $return;
109
    }
110
}
111