steevanb /
php-backtrace
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | class DebugBacktrace |
||
|
0 ignored issues
–
show
|
|||
| 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('><?php ', 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 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.