Returns method caller as string (for debug output)

@param int $traceIndex
@return string
423001
protected function getFixtureCaller($traceIndex = 6)
    {
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $traceIndex + 1);
        $caller = $trace[$traceIndex];

        if (isset($caller['class'])) {
            $result = @$caller['class'] . '->' . @$caller['function'] . '()';
        } else {
            $result = @$caller['function'] . '()';
        }

        return $result;
    }
756584
public static function caller($level = 0)
    {
        $trace = debug_backtrace(0);

        if ($level < 0) {
            return $trace;
        } elseif (empty($trace[$level])) {
            return null;
        } elseif (empty($trace[$level]['file'])) {
            $level = 1;
        }

        $file = $trace[$level]['file'];
        $line = $trace[$level]['line'];

        $trace = null;

        return array(
            'file' => $file,
            'line' => $line
        );
    }
358798
private function _trace($trace = null)
    {
        $trace = $trace ?: debug_backtrace();
        foreach( $trace as $er) {
            $func = isset($er['function'])? $er['function']: '';
            $file = isset($er['file']) ? $er['file'] : '';

            # last two sections of the path
            if ($file) {
                $file  = $this->limitPath($file);
                $line  = isset($er['line'])? $er['line']: '';
                $class = isset($er['class'])? $er['class']: '';
                if ($class == 'IrfanTOOR\Debug' && $func=='_trace')
                    continue;

                $ftag = ($class != '') ? $class . '=>' . $func . '()' : $func . '()';
                $txt = '-- file: ' . $file . ', line: ' . $line . ', ' . $ftag;
                if ($this->terminal)
                    $this->console->writeln( $txt, 'dark');
                else
                    echo '<code style="color:#999">' . $txt . '</code><br>';
            }
        }
    }
847603
private function formatStackTrace(array $trace, $prefix = '')
    {
        $traceAsString = '';
        foreach ($trace as $line) {
            if (isset($line['class']) && isset($line['type']) && isset($line['function'])) {
                $traceAsString .= sprintf('%s%s%s()', $line['class'], $line['type'], $line['function']).PHP_EOL;
            } elseif (isset($line['class'])) {
                $traceAsString .= sprintf('%s', $line['class']).PHP_EOL;
            } elseif (isset($line['function'])) {
                $traceAsString .= sprintf('%s()', $line['function']).PHP_EOL;
            }

            if (isset($line['file']) && isset($line['line'])) {
                $traceAsString .= sprintf('  > %s:%d', $this->makePathRelative($line['file']), $line['line']).PHP_EOL;
            }
        }

        $traceAsString = $this->formatTextBlock($traceAsString, $prefix, true);

        return $traceAsString;
    }
1189048
public static function detect($index = 0)
    {
        $context = new Context();
        $backtrace = debug_backtrace();
        $position = $backtrace[$index];
        if (isset($position['file'])) {
            $context->filename = $position['file'];
        }
        if (isset($position['line'])) {
            $context->line = $position['line'];
        }
        $caller = isset($backtrace[$index + 1]) ? $backtrace[$index + 1] : null;
        if (isset($caller['function'])) {
            $context->method = $caller['function'];
            if (isset($caller['type']) && $caller['type'] === '::') {
                $context->static = true;
            }
        }
        if (isset($caller['class'])) {
            $fqn = explode('\\', $caller['class']);
            $context->class = array_pop($fqn);
            if (count($fqn)) {
                $context->namespace = implode('\\', $fqn);
            }
        }

        // @todo extract namespaces and use statements
        return $context;
    }
387417
public static function formatTrace(array $trace, $indent = '') {
        $appRoot = self::di('config')['app.dir.root'];
        $result  = '';

        $size = sizeof($trace);
        $callLen = $lineLen = 0;

        for ($i=0; $i < $size; $i++) {               // align FILE and LINE
            $frame = &$trace[$i];

            $call = self::getFQFunctionName($frame, $nsLowerCase=true);

            if ($call!='{main}' && !strEndsWith($call, '{closure}'))
                $call.='()';
            $callLen = max($callLen, strlen($call));
            $frame['call'] = $call;

            $frame['line'] = isset($frame['line']) ? ' # line '.$frame['line'].',' : '';
            $lineLen = max($lineLen, strlen($frame['line']));

            if (isset($frame['file']))                 $frame['file'] = ' file: '.(!$appRoot ? $frame['file'] : strRightFrom($frame['file'], $appRoot.DIRECTORY_SEPARATOR, 1, false, $frame['file']));
            elseif (strStartsWith($call, 'phalcon\\')) $frame['file'] = ' [php-phalcon]';
            else                                       $frame['file'] = ' [php]';
        }
        if ($appRoot) {
            $trace[] = ['call'=>'', 'line'=>'', 'file'=>' file base: '.$appRoot];
            $i++;
        }

        for ($i=0; $i < $size; $i++) {
            $result .= $indent.str_pad($trace[$i]['call'], $callLen).' '.str_pad($trace[$i]['line'], $lineLen).$trace[$i]['file'].NL;
        }

        return $result;
    }
469620
public static function getBacktraceCode(array $trace, bool $includeCode = true, bool $plaintext = false): string
    {
        if ($plaintext) {
            return static::getBacktraceCodePlaintext($trace, $includeCode);
        }
        $backtraceCode = '<ol class="Flow-Debug-Backtrace" reversed>';
        foreach ($trace as $index => $step) {
            $backtraceCode .= '<li class="Flow-Debug-Backtrace-Step">';
            $class = isset($step['class']) ? $step['class'] . '<span class="color-muted">::</span>' : '';

            $arguments = '';
            if (isset($step['args']) && is_array($step['args'])) {
                foreach ($step['args'] as $argument) {
                    $arguments .= (strlen($arguments) === 0) ? '' : '<span class="color-muted">,</span> ';
                    $arguments .= '<span class="color-text-inverted">';
                    if (is_object($argument)) {
                        $arguments .= '<em>' . get_class($argument) . '</em>';
                    } elseif (is_string($argument)) {
                        $preparedArgument = (strlen($argument) < 100) ? $argument : substr($argument, 0, 50) . '…' . substr($argument, -50);
                        $preparedArgument = htmlspecialchars($preparedArgument);
                        $preparedArgument = str_replace('…', '<span class="color-muted">…</span>', $preparedArgument);
                        $preparedArgument = str_replace("\n", '<span class="color-muted">⏎</span>', $preparedArgument);
                        $arguments .= '"<span title="' . htmlspecialchars($argument) . '">' . $preparedArgument . '</span>"';
                    } elseif (is_numeric($argument)) {
                        $arguments .= (string)$argument;
                    } elseif (is_bool($argument)) {
                        $arguments .= ($argument === true ? 'true' : 'false');
                    } elseif (is_array($argument)) {
                        $arguments .= sprintf(
                            '<em title="%s">array|%d|</em>',
                            htmlspecialchars(self::renderArrayDump($argument, 0, true)),
                            count($argument)
                        );
                    } else {
                        $arguments .= '<em>' . gettype($argument) . '</em>';
                    }
                    $arguments .= '</span>';
                }
            }

            $backtraceCode .= '<pre class="Flow-Debug-Backtrace-Step-Function">';
            $backtraceCode .= $class . $step['function'] . '<span class="color-muted">(' . $arguments . ')</span>';
            $backtraceCode .= '</pre>';

            if (isset($step['file']) && $includeCode) {
                $backtraceCode .= self::getCodeSnippet($step['file'], $step['line']);
            }
            $backtraceCode .= '</li>';
        }
        $backtraceCode .= '</ol>';
        return $backtraceCode;
    }
863758
public static function traceToString($trace)
    {
        if (!is_array($trace)) {
            return '';
        }

        $result = array();
        foreach ($trace as $index => $line) {
            if (isset($line['args']) && is_array($line['args'])) {
                $params = static::paramsToString($line['args']);
            } else {
                $params = '';
            }

            if (isset($line['file'])) {
                $result[] = sprintf(
                    "#%d %s:%d - %s(%s)", $index, $line['file'], $line['line'], $line['function'], $params
                );
            } else {
                $result[] = sprintf("#%d - %s(%s)", $index, $line['function'], $params);
            }
        }

        return implode(PHP_EOL, $result);
    }
945897
public static function backtrace($trace = null)
	{
		$trace or $trace = debug_backtrace();

		if (\Fuel::$is_cli) {
			// Special case for CLI since the var_dump of a backtrace is of little use.
			$str = '';
			foreach ($trace as $i => $frame)
			{
				$line = "#$i\t";

				if ( ! isset($frame['file']))
				{
					$line .= "[internal function]";
				}
				else
				{
					$line .= $frame['file'] . ":" . $frame['line'];
				}

				$line .= "\t";

				if (isset($frame['function']))
				{
					if (isset($frame['class']))
					{
						$line .= $frame['class'] . '::';
					}

					$line .= $frame['function'] . "()";
				}

				$str .= $line . "\n";

			}

			return $str;
		}
		else
		{
			return static::dump($trace);
		}
	}
223786
public function showCommand(int $index)
    {
        $routes = $this->router->getRoutes();
        if (isset($routes[$index - 1])) {
            /** @var Route $route */
            $route = $routes[$index - 1];

            $this->outputLine('<b>Information for route ' . $index . ':</b>');
            $this->outputLine('  Name: ' . $route->getName());
            $this->outputLine('  Pattern: ' . $route->getUriPattern());
            $this->outputLine('  Defaults: ');
            foreach ($route->getDefaults() as $defaultKey => $defaultValue) {
                $this->outputLine('    - ' . $defaultKey . ' => ' . $defaultValue);
            }
            $this->outputLine('  Append: ' . ($route->getAppendExceedingArguments() ? 'true' : 'false'));
        } else {
            $this->outputLine('Route ' . $index . ' was not found!');
        }
    }
963764
public function convertTrace($trace, $addObject = false)
    {
        $result = array();
        if (is_array($trace)) {
            foreach ($trace as $key => $info) {
                $oneTrace = self::i()->_getOneTrace($info, $addObject);

                $file = 'undefined';
                if (isset($oneTrace['file'])) {
                    $file = $oneTrace['file'];
                }

                $result['#' . ($key - 1) . ' ' . $oneTrace['func']] = $file;
            }
        }

        return $result;
    }
943916
private static function getCallerInfomation()
    {
        foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, self::STACK_FRAME_LIMIT) as $stack) {
            if (isset($stack['class']) && $stack['class'] !== __CLASS__) {
                $stack['function'] = (new \ReflectionMethod($stack['class'], $stack['function']))->name;
                return $stack;
            }
        }
        
        throw new \BadMethodCallException(sprintf('%s must use inside a method', __CLASS__));
    }
647204
protected function initTrace(int $index, string $trace): void
    {
        $lines = explode(':', $trace, 2);
        $headerKey = sprintf('h%s', $index);
        $callKey = sprintf('c%s', $index);

        $this->templates[] = sprintf('<exception_trace:%s>', $headerKey);
        $this->templates[] = sprintf("<exception_body:%s>\n", $callKey);

        $this->replacements[$headerKey] = $lines[0];
        $this->replacements[$callKey] = $lines[1] ?? '';
    }
939464
public function getBetterTrace() {
        $betterTrace = $this->betterTrace;

        if (!$betterTrace) {
            // transform the original stacktrace into a better one
            $betterTrace = DebugHelper::fixTrace($this->getTrace(), $this->getFile(), $this->getLine());

            /*
            // if the exception was thrown in a magic "__set()" shift frames until we reach the erroneous assignment
            while (strtolower($trace[0]['function']) == '__set') {
                \array_shift($trace);
            }

            // if the exception was thrown in a magic "__call()" shift frames until we reach the erroneous call
            if (strtolower($trace[0]['function']) == '__call') {
                while (strtolower($trace[0]['function']) == '__call') {
                    \array_shift($trace);
                }
                \array_shift($trace);                              // that's one level more than for "__set()"
            }
            */

            // store the new stacktrace
            $this->betterTrace = $betterTrace;
        }
        return $betterTrace;
    }
862020
public static function getCaller($count=1)
    {
        $dbts=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,6+$count);
        if ( ! is_array($dbts) || count($dbts) < 2 ) return null;
        if ( ! isset($dbts[0]['file']) ) return null;
        $myfile = $dbts[0]['file'];
        $retval = '';
        foreach($dbts as $dbt) {
            if ( ! isset($dbt['file']) ) continue;
            if ( ! isset($dbt['line']) ) continue;
            if ( $myfile != $dbt['file'] ) {
                if ( strlen($retval) > 0 ) $retval .= " ";
                $retval.= $dbt['file'].':'.$dbt['line'];
                $count--;
                if ( $count < 1 ) return $retval;
            }
        }
        return $retval;
    }
786291
protected function getErrorDetails(\Exception $e, $trace = false)
    {
        $msg = $e->getMessage();
        if ($e instanceof DfException) {
            $context = $e->getContext();
            if (is_array($context)) {
                $context = print_r($context, true);
            }
            if (!empty($context)) {
                $msg .= "\nContext: " . $context;
            }
        }

        if ($trace === true) {
            $msg .= "\nTrace:\n" . $e->getTraceAsString();
        }

        return $msg;
    }
294855
private function traceHTML(array $trace) {
            ?>
            <table class="table" border="1">
                <thead>
                <tr>
                    <th>#</th>
                    <th>Method</th>
                    <th>Args</th>
                    <th>File</th>
                    <th>Line</th>
                </tr>
                </thead>
                <tbody><?php
                    foreach ($trace as $k => $v) {
                        $func = $loc = $line = '';
                        self::formatTraceLine($v, $func, $loc, $line);

                        if (isset($v['args']) && !empty($v['args'])) {
                            $args = Dump::html($v['args']);
                        } else {
                            $args = '<span class="label label-default">[none]</span>';
                        }

                        ?>
                        <tr>
                            <td><?= $k ?></td>
                            <td><?= $func ?></td>
                            <td class="text-center"><?= $args ?></td>
                            <td><?= $loc ? $loc : '<span class="label label-default">???</label>' ?></td>
                            <td><?= ($line == 0 || trim($line)) ? $line :
                                    '<span class="label label-default">???</span>' ?></td>
                        </tr>

                        <?php
                    }
                ?>
                </tbody>
            </table>
            <?php
        }